How to handle servlet errors with JSP

Discussion in 'Java' started by Rob, Aug 15, 2003.

  1. Rob

    Rob Guest

    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
     
    Rob, Aug 15, 2003
    #1
    1. Advertising

  2. Rob

    VisionSet Guest

    "Rob" <> wrote in message
    news:...
    > 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)

    --
    Mike W
     
    VisionSet, Aug 15, 2003
    #2
    1. Advertising

  3. Rob

    Rob Guest

    On Fri, 15 Aug 2003 11:16:07 +0100, "VisionSet"
    <> wrote:

    >"Rob" <> wrote in message
    >news:...
    >> 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" %>


    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
     
    Rob, Aug 15, 2003
    #3
  4. Rob

    VisionSet Guest

    "Rob" <> wrote in message
    news:...
    > On Fri, 15 Aug 2003 11:16:07 +0100, "VisionSet"
    > <> wrote:
    >
    > >"Rob" <> wrote in message
    > >news:...
    > >> 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" %>

    >
    > 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>


    --
    Mike W
     
    VisionSet, Aug 15, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,027
    Mark Goldin
    Jan 17, 2004
  2. circuit_breaker
    Replies:
    2
    Views:
    2,081
    Jack Jia
    Apr 4, 2004
  3. Replies:
    0
    Views:
    4,499
  4. javadev
    Replies:
    5
    Views:
    13,086
    javadev
    Nov 16, 2006
  5. Replies:
    4
    Views:
    838
Loading...

Share This Page