How to get the real requested URI from inside the error page?
When we develop a web application, we usually create a set of error pages, one for each error return code, or exception type. These error pages provides user friendly error message, more inportantly, we can use these error pages to log what caused the error.
The section of the web.xml may look like these:
<web-app>
...
<error-page>
<error-code>
404
</error-code>
<location>
/Error404.jsp
</location>
</error-page>
<error-page>
<exception-type>
javax.servlet.ServletException
</exception-type>
<location>
/GeneralError.jsp
</location>
</error-page>
...
</web-app>
Inside the Error404.jsp and the GeneralError.jsp, we can get the following information from the request object:
javax.servlet.error.status_code: An Integer telling the error status code, if any
javax.servlet.error.exception_type: A Class instance indicating the type of exception that caused the error, if any
javax.servlet.error.message: A String telling the exception message, passed to the exception constructor
These are the attributes of the request object, you can get them like these:
<%
String statusCocde = (String) request.getAttribute("javax.servlet.error.status_code");
String exceptionType = (String) request.getAttribute("javax.servlet.error.exception_type");
String errorMsg = (String) request.getAttribute("javax.servlet.error.message");
%>
These information can help us to customize the error page. For the logging purpose, can we find out the real requested URI that caused the error? Using
<%
String requestedURI = request.getRequestURI();
%>
returns the uri of the Errror page, which doesn't help.
With Servlet API 2.3, two new attributes are added to the request object, they are:
javax.servlet.error.request_uri: A String telling the URI of the resource causing problems
javax.servlet.error.exception: A Throwable object that is the actual exception thrown
The "javax.servlet.error.request_uri" attribute returns the real requested url which get redirected to the error page; the "javax.servlet.error.exception" attribute contains the whole throwable object, that you can log the stack trace.
<%
String uri = (String) req.getAttribute("javax.servlet.error.request_uri");
%>
Most Recent jsp Faqs
- How to get the real requested URI from inside the error page?
- Can I define a method in a JSP page?
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
Most Viewed jsp Faqs
- How to config a JSP file in web.xml?
- What is difference between page and pageContext in JSP pages?
- How to disable browser caching for a specific JSP?
- How to retrieve values from HTML form input elements in JSP?
- What is the difference between jsp:forward and response.sendRedirect?
- What is the difference between request.getParameter() and request.getAttribute()?
- How to get the current JSP page name?