Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP exception implicit object

I have a little Java web application. It uses MVC, with a HttpServlet as controller and several JSPs for view.

The Servlet captures several exceptions like SQLExceptions, and I need process these exceptions in an "ErrorPage" JSP. (JSP with directive <%@page isErrorPage="true" %>.) The problem is, How can I set "exception" JSP implicit object from the Servlet?

(I'm using a RequestDispatcher object to pass control to error page.)

like image 478
Adrián E. Córdoba Avatar asked Apr 22 '26 04:04

Adrián E. Córdoba


1 Answers

This is the usage,

page1.jsp

<%@page errorPage="errorpage.jsp" %>
<%
  //this has your code that throws some exception

%>

The exception thrown in jsp page will be caught based on your errorPage configuration.

errorpage.jsp

<%@ page isErrorPage='true' %>
<%
out.print("Error Message : ");  
out.print(exception.getMessage());
%>

The exception object represents all errors and exceptions. The exception implicit object is of type java.langThrowable. You can access the exception object on a page that you declare to be an error page using the isErrorPage attribute of the page directive.

The exception object is created only if the JSP uses the page directive to set isErrorPage set to true. When a JSP generates an error and forwards that error to the error page, the container sets the JSP exception object of the error page to the generated error

Apart from exception.getMessage(), you can also invoke printStackTrace() and toString() on exception object.

Is this what you were asking? or you want to handle exception in your Servlet class?

Also, If you configure your web.xml like this ,

<error-page>  
   <exception-type>java.lang.Throwable</exception-type>  
   <location>/errorpage.jsp</location>  
</error-page>

then for all exceptions this error page is invoked, and you can use the exception object as mentioned above and invoke its methods for the exception details. If you want to configure a specific error page for a specific exception, change the <exception-type> accordingly

Note that the exception is set by container when it encounters an exception. If you just forward the control to your error page, it just displays the page.

updated:

try {
//sample code
    data= dao.find(something);
}
catch (SQLException e) {
    throw new ServletException("The query got failed", e);
}

The Servlet container handles the ServletException . When the container catch it while executing the servlet, then its cause will be unwrapped and compared against any of the specific entries in web.xml and the closest match will be displayed.

<error-page>
    <exception-type>java.sql.SQLException</exception-type>
    <location>/errorpage.jsp</location>
</error-page>

Update 2

To give another example:

web.xml

<servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>com.examples.example.Servlet1</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<error-page>
    <exception-type>javax.servlet.ServletException</exception-type>
    <location>/errorpage.jsp</location>
</error-page>
<error-page>
    <exception-type>java.sql.SQLException</exception-type>
    <location>/errorpage.jsp</location>
</error-page>
<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/errorpage.jsp</location>
</error-page>

Servlet1.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String s = null;
            if (s.equals("")) {

            }
        } catch (Exception ex) {
            throw new ServletException("my custom exception message");
        }
    }

errorpage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isErrorPage='true'%>
<html>
<head>
<title>Error Page</title>
</head>
<body>
    <%
        out.print("Error Message : ");
        out.print(exception.getMessage());
    %>
</body>
</html>
like image 129
spiderman Avatar answered Apr 23 '26 18:04

spiderman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!