You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by sb...@maerskdata.dk on 2001/07/24 10:55:23 UTC

Handling exceptions thrown from Action.perform in the errorpage?

Hey there!

I have a question for you guys.

When an exception is thrown in my Action derivative I want that exception
to
be handled by the errorpage (<%@ page isErrorPage="true %>). After fooling
around
with the web.xml file adding entries such as:

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

the result is that when I throw a ServletException from the Action.perform
method
I'm directed to my login page(?!). I've tried using the
"mapping.findForward" approach,
as in:

public final ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
{
     ActionForward af = null;

     try {
          af = doPerform(...); // throws exception
     } catch(Throwable t) {
          request.setAttribute("javax.servlet.jsp.jspException", new
ServletException(t));
          af = mapping.findForward("error");
     }


    return af;
}

ofcourse, I added:

  <!-- ========== Global Forward Definitions
============================== -->
  <global-forwards>
    <forward   name="error"                path="/errorpage.jsp"/>
  </global-forwards>

to the struts-config.xml file.


That didn't work either. The only approach that seems to work for me is:



public final ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
{
     try {
          return doPerform(...); // throws exception
     } catch(Throwable t) {
          request.setAttribute("javax.servlet.jsp.jspException", t);
          getServlet().getServletContext().getRequestDispatcher
("/errorpage.jsp").forward(request, response);
     }
}


Now my questions are:

     Is this last approach safe to use with Struts?
     How do you guys accomplish this? Do you use a different approach
altogether?

TIA,
S. Bro





Re: Handling exceptions thrown from Action.perform in the errorpage?

Posted by Ted Husted <hu...@apache.org>.
>   <global-forwards>
>     <forward   name="error"                path="/errorpage.jsp"/>
>   </global-forwards>

> return mapping.findForward("error")

Will forward to the file named "errorpage.jsp" in the root of your Web
application. 

If it doesn't, something else is wrong, like doPerform is not throwing
the exception (Try logging it too.), or the page is in the root of your
container instead, et cetera.

Meanwhile, when saving exceptions for display on a JSP, you might use 

Action.EXCEPTION_KEY

for the attribute key. The custom tags use this, and so your page would
also display any exceptions the tags throw. (See saveException() in
util.RequestUtils)

sbt@maerskdata.dk wrote:
> 
> Hey there!
> 
> I have a question for you guys.
> 
> When an exception is thrown in my Action derivative I want that exception
> to
> be handled by the errorpage (<%@ page isErrorPage="true %>). After fooling
> around
> with the web.xml file adding entries such as:
> 
>      <error-page>
>           <exception-type>javax.servlet.ServletException</exception-type>
>           <location>/errorpage.jsp</location>
>      </error-page>
> 
> the result is that when I throw a ServletException from the Action.perform
> method
> I'm directed to my login page(?!). I've tried using the
> "mapping.findForward" approach,
> as in:
> 
> public final ActionForward perform(ActionMapping mapping, ActionForm form,
> HttpServletRequest request, HttpServletResponse response)
>     throws IOException, ServletException
> {
>      ActionForward af = null;
> 
>      try {
>           af = doPerform(...); // throws exception
>      } catch(Throwable t) {
>           request.setAttribute("javax.servlet.jsp.jspException", new
> ServletException(t));
>           af = mapping.findForward("error");
>      }
> 
>     return af;
> }
> 
> ofcourse, I added:
> 
>   <!-- ========== Global Forward Definitions
> ============================== -->
>   <global-forwards>
>     <forward   name="error"                path="/errorpage.jsp"/>
>   </global-forwards>
> 
> to the struts-config.xml file.
> 
> That didn't work either. The only approach that seems to work for me is:
> 
> public final ActionForward perform(ActionMapping mapping, ActionForm form,
> HttpServletRequest request, HttpServletResponse response)
>     throws IOException, ServletException
> {
>      try {
>           return doPerform(...); // throws exception
>      } catch(Throwable t) {
>           request.setAttribute("javax.servlet.jsp.jspException", t);
>           getServlet().getServletContext().getRequestDispatcher
> ("/errorpage.jsp").forward(request, response);
>      }
> }
> 
> Now my questions are:
> 
>      Is this last approach safe to use with Struts?
>      How do you guys accomplish this? Do you use a different approach
> altogether?
> 
> TIA,
> S. Bro

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel 716 737-3463.
-- http://www.husted.com/about/struts/