How to handle servlet errors with JSP

R

Rob

Hello, I'm looking for an easier way to handle errors in a
servlet and pass a meaningful String to a JSP.

Here's my current setup:

public void myServlet extends HttpServlet throws
ServletException{

public void showError(HttpServletRequest req,
HttpServletResponse resp, String errorMessage){

RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("errorPage.jsp");
if(dispatcher != null){
req.setAttribute("error",errorMessage);
try{
dispatcher.forward(req,resp);
}catch(ServletException se){}
catch(IOException ioe){}
}
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{

try{
do_something();
}catch (SomeException se){
showError(request, response, "Some Error occured");
}
}

If do_something() causes an exception, I can catch it and
forward to the JSP directy. But what do I do if do_something()
calls method2() which in turn calls method3()? I'd like
method3() to be able to forward the error to the errorPage, so
I'm passing the HttpServletRequest and HttpServletResponse
variables, obtained in doPos(), from method to method. This is
becoming a pain. There must be an easier way to grab an
exception in any method and have a clean way to show an error in
errorPage.jsp.

This is for a personal project.

TIA,
Rob
 
V

VisionSet

Rob said:
Hello, I'm looking for an easier way to handle errors in a
servlet and pass a meaningful String to a JSP.

All this is handled automatically by the container exceptions are stored in
request attributes and jsps can indicate there interest in knowing about
them by an errorpage attribute in the page tag <%@ page
errorPage="/ErrorDisplay" %>
You can make ErrorDisplay a servlet that looks at these request attributes
and do whatever you like. Look at the pdf servlet spec (2.3 is it still?)
(google for servlet-2_3-fcs-spec.pdf)
 
R

Rob

All this is handled automatically by the container exceptions are stored in
request attributes and jsps can indicate there interest in knowing about
them by an errorpage attribute in the page tag <%@ page
errorPage="/ErrorDisplay" %>

I don't know if I understand you correctly.
I saw that a JSP can automatically forward to an error page, but
I didn't know a servlet could. So I could define <%@ page
errorPage="/ErrorDisplay" %> in any JSP, but I'm looking for
this sort of mechanism in servlets. I've looked at Hall's "Core
Servlets and JSP", and there the examples use a dispatcher to
forward to a JSP. They pass HttpServletResponse as an argument
to the method which dispatches the error to the error page jsp.

-Rob
 
V

VisionSet

Rob said:
I don't know if I understand you correctly.
I saw that a JSP can automatically forward to an error page, but
I didn't know a servlet could. So I could define <%@ page
errorPage="/ErrorDisplay" %> in any JSP, but I'm looking for
this sort of mechanism in servlets. I've looked at Hall's "Core
Servlets and JSP", and there the examples use a dispatcher to
forward to a JSP. They pass HttpServletResponse as an argument
to the method which dispatches the error to the error page jsp.

-Rob

The servlet I mentioned *is the error page*
Entries like this in your web.xml do the job that the errorpage tag in the
jsp does.
Really - read the servlet spec, it is all in there.

- <error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
- <error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/ErrorDisplay</location>
</error-page>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top