1

Here is my code.

try {
    RequestDispatcher d = request
            .getRequestDispatcher("messages/error.jsp");
    request.setAttribute("message", "Error Occurred !!!");
    d.forward(request, response);
} catch (ServletException se) {
    se.printStackTrace();
} catch (IOException ioe) {
    ioe.printStackTrace();
}

I need to know how to handle ServletException and IOException in given code. I wish to redirect user to a error page and tell user to error occurred when getting above exceptions.

How could i do this ?

Bishan
  • 15,211
  • 52
  • 164
  • 258
  • Refer http://stackoverflow.com/questions/12257928/what-is-the-good-approach-to-forward-the-exception-from-servlets-to-a-jsp-page – SparkOn Sep 01 '14 at 15:54

2 Answers2

1

You can configure you server to handle specific exception classes or status codes, in your case you must add the next lines to your web.xml

<error-page>
   <exception-type>javax.servlet.ServletException</exception-type>
   <location>/error.jsp</location>
</error-page>

And the same for the IOException, with this configuration when the application server catches a not handled ServletException will show the error.jsp. The error.jsp page should have the attribute isErrorPage="true", with this you can have access to the exception variable containing all the info related to the exception thrown. I put an example that shows the stacktrace.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true" %>
<html>
<head>
   <title>Error Handling Example</title>
</head>
<body>
     <%=exception.getMessage()%>
     <% exception.printStackTrace(response.getWriter()); %>
</body>
</html>

Also you need to remove the catch block code.

Cesar Loachamin
  • 2,740
  • 4
  • 25
  • 33
  • remove the code for handling the exception, remove the } catch (ServletException se) { } block, the server AS only takes you to the error.jsp if the exceptions is not handled by your code, so you should remove the try catch block – Cesar Loachamin Sep 02 '14 at 14:30
0

You can redirect once the Exception is encountered in your catch Block: example:

try {
    //Do whatever you need here
} catch (ServletException se) {
request.setAttribute("message", "Error Occurred !!! + \n"+ se.getMessage());
RequestDispatcher d = request
            .getRequestDispatcher("messages/error.jsp");
    d.forward(request, response);
    se.printStackTrace();
} catch (IOException ioe) {
 request.setAttribute("message", "Error Occurred !!! + \n"+ se.getMessage());
RequestDispatcher d = request
            .getRequestDispatcher("messages/error.jsp");
    d.forward(request, response);
    ioe.printStackTrace();
}

And then in you error.jsp you need to get the Attribute;

${message}//recommended

or if you are using scriplets which is not recommended, just do:

request.getAttribute("message");
Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
  • `d.forward(request, response);` in catch block also given message to handle exceptions. how should i handle them ? – Bishan Sep 01 '14 at 15:39