Servlets 2.x allowed to create a mapping between an HTTP error code or an exception type to the path of a resource in the Web application. This is achieved by specifying an "error-page" element in the "web.xml". The element definition looks like:

So any HTTP error code or an exception thrown within the application can be mapped to a resource bundled with the application. Here is a sample:
<error-page>
<error-code>404</error-code>
<location>/error-404.jsp</location>
</error-page>
Adding the above fragment in "web.xml" of an application will display "error-404.jsp" page to the client if a non-existing resource is accessed. This mapping can be easily done for other HTTP status codes as well by adding other <error-page> elements.
Similarly, <exception-type> element can be used to map an exception to a resource in the web application. This allows fine-grained mapping of errors from your web application to custom pages.
Starting with Servlets 3.0, <error-code> and <exception-type> elements are optional. An <error-page> without any <exception-type> and <error-code> will be considered as the webapp’s default error page, and will act as a "catch-all" for any error codes or exception types. It will be an error if a web.xml contains more than one such default error page.
A default error page may be overridden for specific exception types and error codes. For example:
<error-page>
<location>/error-default.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error-404.jsp</location>
</error-page>
Any response with a status code other than 404 will be error-dispatched to /default.jsp, while a 404 response will be error-dispatched to /error-404.jsp.
So if the Servlet code looks like:
@WebServlet(name="HelloServlet", urlPatterns={"/HelloServlet"})
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String type = (String)request.getParameter("type");
if (type == null) {
response.getWriter().print("hello world");
return;
}
if (type.equals("helloex")) {
throw new HelloException();
} else if (type.equals("ncdfe")) {
throw new NoClassDefFoundError();
} else {
throw new NullPointerException();
}
}
}
And the "web.xml" looks like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<error-page>
<exception-type>java.lang.NoClassDefFoundError</exception-type>
<location>/error-ncdfe.jsp</location>
</error-page>
<error-page>
<exception-type>server.HelloException</exception-type>
<location>/error-helloex.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error-404.jsp</location>
</error-page>
<error-page>
<location>/error-default.jsp</location>
</error-page>
</web-app>
Lets say the directory structure looks like:
WEB-INF/classes/
WEB-INF/classes/server/
WEB-INF/classes/server/HelloException.class
WEB-INF/classes/server/HelloServlet.class
WEB-INF/web.xml
error-404.jsp
error-default.jsp
error-helloex.jsp
error-ncdfe.jsp
and this application is deployed as "DefaultErrorPage.war". Then here is a table of the page that gets displayed when the URL mentioned in the first column is accessed:
| URL | Response | Comment |
| http://localhost:8080/DefaultErrorPage/HelloServlet | "hello world" | Expected result |
| http://localhost:8080/DefaultErrorPage/HelloServlet2 | error-404.jsp | HTTP 404 |
| http://localhost:8080/DefaultErrorPage/HelloServlet?type=ncdfe | error-ncdfe.jsp | System exception |
| http://localhost:8080/DefaultErrorPage/HelloServlet?type=helloex | error-helloex.jsp | User exception |
| http://localhost:8080/DefaultErrorPage/HelloServlet?type | error-default.jsp | Catch-all exception |
Try this and other Java EE 6 features in GlassFish Server Open Source Edition 3 or Oracle GlassFish Server today!
The complete source code used in this blog can be downloaded here.
The default setting in Chrome is to show suggestions to navigate to other parts of the website or search with Google. This can be easily disabled by Chrome -> Preferences -> Under the Hood and deselecting "Show suggestions for navigation errors" in Privacy section. This is explained in detail here.
Technorati: totd javaee glassfish v3 servlet default error
Related posts:- TOTD #161: Java EE 6 CDI Qualifiers explained – @Default, @Any, @New, @Named
- TOTD #120: Deployment Descriptor-free Java EE 6 application using JSF 2.0 + EJB 3.1 + Servlets 3.0
- TOTD #132: Servlets 3.0 in Embedded GlassFish Reloaded – lightweight Java EE 6
- TOTD #139: Asynchronous Request Processing using Servlets 3.0 and Java EE 6
- TOTD #102: Java EE 6 (Servlet 3.0 and EJB 3.1) wizards in Eclipse
I’ve tried this. and another example
every thing is going as expected, But, The default Page….!
i’m running Netbeans 7.0, Tomcat 7.0.11, JDK 6
Is it a must to have Java 7 ? i don’t think so
Mahmoud Darweash.
Comment by MDArweash — August 9, 2011 @ 11:31 pm