0

Currently, I am developing a web project by means of web components (servlets and JSP), Jsp component use 'scripting elements' to redirect the request when a exception is produced (especially the directive element and errorPage attribute, like the following example):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" errorPage="Error.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
    <form name="formulario" method="get" action="http://localhost:8080/Pruebas/LanzaError">
    User: <input type="text" name="user">
    <input type="submit" value="Send">
</form>
</body>
</html>

The above JSP calls to LanzaError Servlet, which contains the follow code in doGet Method:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
    if (true) {
            throw new ServletException("Error de Servlet");
    }

}

How you can see, intentionally I produced a ServletException.

Like I used errorPage attribute in directive element, I think the JSP should redirect to Error.jsp page, which contains the follows code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
    <h1><%= exception.getLocalizedMessage() %></h1>
</body>
</html>

However, it did not happen when a exception was produced from servlet.

Does Someone know how can I do for redirect to Error.jsp page when an exception has been produced in the servlet?. I would not like to do it with RequestDispatcher code .I would like to throws the exception from the method (like the method signature says: throws ServletException, IOException`).

Thanks for your help.

Edited --------------:

In the web site link I read the follow comment:

One way to handle it in a generic way is to use web.xml like below:

<error-page>
    <exception-type>java.io.IOException</exception-type >
    <location>/ErrorHandler</location>
</error-page>

I have just included IO exception but you might have say SQLException you could very well add another error-page and another location for the same. Similarly you could say java.lang.Exception type and one handler handling everything.

¡It works!, however if I add the xml error-page element, then the control of exception will be absolute, i.e., Every types of exceptions that they share among servlets (or with equal status code) should have the same error page (because of this way was declared in the descriptor web.xml). Please correct me if I'm wrong.

For this reason I think that in any cases, when only one servlet throws a certain error page, the most appropiate is use `RequestDispatche' (and of this way you do not have to add any other new element to web.xml).

But, now I have a new problem, In this code I added the RequestDispatcher:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
        try {
            if (true) {
                throw new ServletException("Error de Servlet");
            }
        }catch(Exception e) {
            RequestDispatcher rd = request.getRequestDispatcher("/Error.jsp");
            rd.forward(request, response);
        }
    }

When the request has finally arrived to Error.jsp, it does not detect the request like a Error, for this reason it does not detect the implicit object exception and the Error.jsp cause a nullPointerException. Does someone knows how can I proceed?

Other way to doint could be add a attribute the response object from the servlet catch, with the error message, but I do not believe that that will be the correct way to do it (this would make the little one lose of isErrorPage attribute in the jsp web component).

Thanks for you time.

J. Abel
  • 890
  • 3
  • 17
  • 38

1 Answers1

0

I met this issue recently. The cause of this issue is web container has its own mechanism (the web.xml configuration way)to handle error for the servlets. JSP can also use this way, and additionally it can use errorPage attribute in directive element to handle error in this page, which is the JSP's special way. You kind of want to mix them together. Thus, it is better to consider which case you really want and use it separately in different error pages. Of course, you can mix them together, but it can be ugly. As you mentioned, you can set the error message in the attribute or pass them as query parameters. Also, you can add some logic in the error page to identify different sources.

Shawn
  • 49
  • 2