In JSPs, you may use response.sendError(int code, String message) to return a particular error code (eg 404 for not found) and a message as well.
These messages
The thing you want looks weird to me :). That said, I would do the following:
Implement HttpResponseWrapper to wrap any other HttpResponse in this way:
public class HttpResponseWrapper implements HttpResponse {
private String errorMessage;
...
@Override
public void sendError(...) {
}
...
}
Create a Filter and wrap any response in this
Put filter on all requests and first in the chain
In your error page check if response is instanceof HttpResponseWrapper
Get your message