How to specify the default error page in web.xml?
On Servlet 3.0 or newer you could just specify
<web-app ...>
<error-page>
<location>/general-error.html</location>
</error-page>
</web-app>
But as you're still on Servlet 2.5, there's no other way than specifying every common HTTP error individually. You need to figure which HTTP errors the enduser could possibly face. On a barebones webapp with for example the usage of HTTP authentication, having a disabled directory listing, using custom servlets and code which can possibly throw unhandled exceptions or does not have all methods implemented, then you'd like to set it for HTTP errors 401, 403, 500 and 503 respectively.
<error-page>
<!-- Missing login -->
<error-code>401</error-code>
<location>/general-error.html</location>
</error-page>
<error-page>
<!-- Forbidden directory listing -->
<error-code>403</error-code>
<location>/general-error.html</location>
</error-page>
<error-page>
<!-- Missing resource -->
<error-code>404</error-code>
<location>/Error404.html</location>
</error-page>
<error-page>
<!-- Uncaught exception -->
<error-code>500</error-code>
<location>/general-error.html</location>
</error-page>
<error-page>
<!-- Unsupported servlet method -->
<error-code>503</error-code>
<location>/general-error.html</location>
</error-page>
That should cover the most common ones.
How to specify common error page excluding 404 and 500 in web.xml
This should be sufficient, along with few customer error related pages, you can add default page. So all error other than 404, 500 , will be redirected to generic page error.html
<error-page>
<location>/error.html</location>
</error-page>
or more customized, using Exception class, can be your custom exception class as well.
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.html</location>
</error-page>
So code becomes,
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/error/500.jsp</location>
</error-page>
<error-page>
<location>/error.html</location>
</error-page>
Error page in web.xml
You will get a blank page in such situation when the response is already committed by the servlet or the JSP causing the exception. In other words, when the HTTP response headers are already been sent to the webbrowser. This is a point of no return.
To prevent this, you need to ensure that you start writing to the response only when all business code has finished its job (better, just don't do that in the servlet, but just forward to a JSP) and also that your JSPs does not contain any single line of business code (often represented by scriptlets <% %>
).
So if you do for example this in a servlet
response.getWriter().write("blah");
throw new ServletException("epic fail");
or when the exception is been thrown "halfway" a JSP
<p>Some HTML</p>
<% throw new ServletException("epic fail"); %>
<p>Some more HTML</p>
then the risk is big that you get a blank page.
error-page defined in web.xml always comes back with HTTP status 200
What I ended up with was writing a small web service (instead of a static page) that would give me a JSON response and the correct HTTP status code, plus relevant headers:
<error-page>
<error-code>401</error-code>
<location>/error/401</location>
</error-page>
Which calls the service
@Path("/error")
public class ErrorService {
private static final Map<Integer, String> statusMsg;
static
{
statusMsg = new HashMap<Integer, String>();
statusMsg.put(401, "Resource requires authentication");
statusMsg.put(403, "Access denied");
statusMsg.put(404, "Resource not found");
statusMsg.put(500, "Internal server error");
}
@GET
@Path("{httpStatus}")
public Response error(@PathParam("httpStatus") Integer httpStatus) {
String msg = statusMsg.get(httpStatus);
if (msg == null)
msg = "Unexpected error";
throw new MyWebApplicationException.Builder()
.status(httpStatus)
.addError(msg)
.build();
}
}
I have an exception class MyWebApplicationException
with its own builder pattern that I've already previously used to format all sorts of application errors as JSON using a jax-rs ExceptionMapper
.
So now I'm just manualle feeding externally caught errors (like the 401 that happens outside of JAX-RS) through the same channel as well.
Error Page configuration in web.xml for JSF application
You can add error pages for Exception
as follows
<error-page>
<error-code>400</error-code>
<location>/400.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<exception-type>your.company.YourException</exception-type>
<location>/errorPage.html</location>
</error-page>
Recheck the Exception
type if you want to show error page on all type of exception then use java.lang.Exception
Related Topics
Any Way to Invoke a Private Method
How to Use Raw SQL Within a Spring Repository
How to Obtain Method Parameter Name Using Java Reflection
Java Reverse an Int Value Without Using Array
How Come Invoking a (Static) Method on a Null Reference Doesn't Throw Nullpointerexception
How Does Auto Boxing/Unboxing Work in Java
Cannot Parse String in Iso 8601 Format, Lacking Colon in Offset, to Java 8 Date
Java Random Always Returns the Same Number When I Set the Seed
Returning Null as an Int Permitted with Ternary Operator But Not If Statement
Convert Java.Util.Date to What "Java.Time" Type
How to Open the Default Webbrowser Using Java
Multiple Line Code Example in Javadoc Comment