How to handle exceptions in JSP?

Showing Answers 1 - 2 of 2 Answers

Vikas Singh

  • Jan 10th, 2007
 

-->Runtime exceptions are easy to handle in a JSP application, because they are stored one at a time in the implicit object named exception. You can use the exception object in a special type of JSP page called an error page, where you display the exception's name and class, its stack trace, and an informative message for your user.-->A JSP page specifies the error page with the page directive and errorPage attribute. When an unhandled exception occurs, any unflushed output in the output stream is discarded and the error page is immediately executed. Here is an example of JSP page specifying erropage.jsp to be the error page: <%@ page errorPage="errorpage.jsp" %> <% if (request.getParameter("param").equals("value")) { // ... } // The test above will throw a NullPointerException if param is // not part of the query string. A better implementation would be: if ("value".equals(request.getParameter("param"))) { // ... } %>The error page indicates that it is an error page with the page directive and isErrorPage attribute. This makes the unhandled exception available in a variable called exception. Here is an example of an error page that simply prints the name of the unhandled exception: <%@ page isErrorPage="true" %> An unexcepted error occurred. The name of the exception is: <%= exception %>

  Was this answer useful?  Yes

Vikas Singh

  • Jan 10th, 2007
 

A JSP page specifies the error page with the page directive and errorPage attribute. When an unhandled exception occurs, any unflushed output in the output stream is discarded and the error page is immediately executed. Here is an example of JSP page specifying erropage.jsp to be the error page:

<%@ page errorPage="errorpage.jsp" %>
    <%
        if (request.getParameter("param").equals("value")) {
            // ...
        }
         // The test above will throw a NullPointerException if param is
        // not part of the query string. A better implementation would be:
        if ("value".equals(request.getParameter("param"))) {
            // ...
        }
    %>
The error page indicates that it is an error page with the page directive and isErrorPage attribute. This makes the unhandled exception available in a variable called exception. Here is an example of an error page that simply prints the name of the unhandled exception:

<%@ page isErrorPage="true" %>
        An unexcepted error occurred. The name of the exception is:
<%= exception %>

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions