Link to home
Start Free TrialLog in
Avatar of mvkraju
mvkraju

asked on

how to handle Exceptions in Struts?

Hi,

  I have got an exception in the Action servlet. Say an Exception is thrown in execute() method of a Action servlet. I want to show it in a JSP.

How do we do this?
Please give me some info..

Thanks
ASKER CERTIFIED SOLUTION
Avatar of searlas
searlas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mvkraju
mvkraju

ASKER

can you provide me sample code from errorPage.jsp

Thanks
Avatar of mvkraju

ASKER

searls,

  When i copy the code you mentioned above in my web.xml file, it says exception tag is not defined

when i give the follwoing code it works

error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>myPage.html</location>
</error-page>
Or

<error-page>
<error-code>500</error-code>
<location>myPage.html</location>
</error-page>


 
   
 
Avatar of mvkraju

ASKER

I am using WebSphere application server, it's not allowing to give any one of the above mentioned errorpage tags.

Is there any way?
Avatar of mvkraju

ASKER

<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/myPage.html</location>
</error-page>

works perfectly, the only thing you need to do when you are using WebSphere
Open the deployment descriptor by double clicking on the web.xml file. Click on the Pages tab. In the Error Pages section click on Add. Enter an HTTP error code and URI or select them from a list.

Thanks
Glad you worked it out... I think I must have typed in my proposed solution from memory... obviously not perfect.

You say in http://#10942174 that you have two forms of error-page that work, then immediately after that WebSphere is not allowing any of them.  Just wondering (for the benefit of people reading this in future) what did you have it working on, and how did it fail in WebSphere?  Do you have to use a GUI to edit this in WebSphere? ('click on the Page tab...')
Avatar of mvkraju

ASKER

I don't know some how when i try to copy the code directoy in web.xml file it's giving showing error in WSAD.
May be i might be copying in wrong place, that time i was not aware of where exactly i suppose to copy the code.
Then i could figure it out how to use GUI to enter error page information.

Thanks
That could be it, the order is important.  What I'd do is check where WSAD put it and remember it for future.  Your web.xml should conform to the order definied in the dtd.  i.e. at the top of your web.xml you (should) have a DOCTYPE, something like:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
                         "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

Now, the dtd file referenced tells you the order that things need to appear in:
http://java.sun.com/j2ee/dtds/web-app_2_2.dtd

(you may have a different url for the dtd - I'm using quite an old version of standards here)

From that dtd you can see the order of elements by finding the web-app definition:
<!ELEMENT web-app (icon?, display-name?, description?, distributable?,
context-param*, servlet*, servlet-mapping*, session-config?,
mime-mapping*, welcome-file-list?, error-page*, taglib*,
resource-ref*, security-constraint*, login-config?, security-role*,
env-entry*, ejb-ref*)>


So, finding error-page in that list, it comes directly after welcome-file-list.
If you don't have a welcome-file-list, you'd place the error-page after mime-mapping, if you don't have mime-mapping you'd place it after session-config... etc etc... (work your way backwards from error-page)


Avatar of mvkraju

ASKER

i am using the fllowing one
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

What you said could be correct. It could be the order of the DTD.
Avatar of mvkraju

ASKER

searlas,

I incorporated the followin code in web.xml

      <error-page>
            <exception-type>java.lang.Exception</exception-type>
            <location>/errorPage.jsp</location>
      </error-page>
      <error-page>
            <exception-type>java.io.IOException</exception-type>
            <location>/errorPage.jsp</location>
      </error-page>
      <error-page>
            <exception-type>javax.servlet.jsp.JspException</exception-type>
            <location>/errorPage.jsp</location>
      </error-page>
      <error-page>
            <exception-type>java.sql.SQLException</exception-type>
            <location>/errorPage.jsp</location>
      </error-page>
      <error-page>
            <exception-type>java.lang.NullPointerException</exception-type>
            <location>/errorPage.jsp</location>
      </error-page>

but when i get NullPointerException it's not displaying errorPage.jsp

What could be the reason? What else do i need to do?

Thanks
That all looks fine (to me).  Is the error page displaying for any other types of error?  Are you sure the errorPage.jsp itself isn't throwing any errors?
Avatar of mvkraju

ASKER

I am sure that errorPage.jsp is not throwing any error. But i am catching that NullPointerException in execute method of Action servlet.
Do i need to thow it instead of catch?

Thanks
Avatar of mvkraju

ASKER

Here is the exception
2004-05-06 13:54:46,815-Servlet.Engine.Transports : 3 (ERROR) MsgWarehouseRptAction.execute(670) - java.lang.NullPointerException
java.lang.NullPointerException
      at com.cfg.ia.mwr.db.ConnHandler.<init>(ConnHandler.java:39)
      at com.cfg.ia.mwr.db.SmwSqlHandler.getMyConnection(SmwSqlHandler.java:89)
      at com.cfg.ia.mwr.db.SmwSqlHandler.serviceErrCountDoubles(SmwSqlHandler.java:629)
      at com.cfg.ia.mwr.action.MsgWarehouseRptAction.processServiceErrorCounts(MsgWarehouseRptAction.java:263)
      at com.cfg.ia.mwr.action.MsgWarehouseRptAction.evaluateTarget(MsgWarehouseRptAction.java:73)
      at com.cfg.ia.mwr.action.MsgWarehouseRptAction.execute(MsgWarehouseRptAction.java:646)
      at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
      at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
      at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
      at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
Avatar of mvkraju

ASKER

Here is my errorPage.jsp code

<html>
<head><title>JSP Error Page</title></head>

<body bgcolor=#ffffff>

<font face="Helvetica">

<h2><font color=#DB1260>JSP Error Page</font></h2>

<p> An exception was thrown: <b> <%= exception %>

<p> With the following stack trace:
<pre>

<%
    ByteArrayOutputStream ostr = new ByteArrayOutputStream();
    exception.printStackTrace(new PrintStream(ostr));
    out.print(ostr);
%>
</pre>

<p>
<hr width=80%>
<p>
<a href="/index.jsp">click here to start</a>
</body>
</html>
Avatar of mvkraju

ASKER

o.k. it's working
This link helped me a lot

http://weblogs.java.net/pub/wlg/987

Thanks
Hi mvkraju,

Could you add which bit of that link helped you; it seems like a general article on exception handling.

My reaction to: "But i am catching that NullPointerException in execute method of Action servlet.
Do i need to thow it instead of catch?", is 'yes', you should throw the exception out of your Action servlet (or simply not catch it in the first place.  The exception handling in <error-page> directives can only kick in if the Exception has been allowed to propogate outside of the servlet servicing the current request.

At least, that's how I understand it... please comment if you've found something different.