Error mappings
Servlet technologies provide a good way to redirect the errors in the web pages. In this section, we will show some cases to do it.
File configuration
An alternative to the annotations is file configuration. Actually, file configuration allows a more complete set of configurations compared to annotations. Firstly, we will see how to configure a servlet through the web.xml
file, the default configurator
file available in the Java web applications. Take this servlet sample:
public class ErrorMappingServlet extends HttpServlet {
...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
throw new RuntimeException();
}
...
}
This servlet doesn't declare the servlet annotation seen in the preceding paragraphs, so it needs to be configured in the web.xml
. This is a simple web.xml
with the declaration of the servlet:
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns...