Solved

how to handle Exceptions in Struts?

Posted on 2004-04-07
16
235 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:mvkraju
  • 11
  • 5
16 Comments
 
LVL 7

Accepted Solution

by:
searlas earned 40 total points
ID: 10878165
Surprised noone answered this...

You need to configure this in web.xml.

Example:

<error-page>
 <exception>java.lang.Exception</exception>
 <location>/errorPage.jsp</location>
</error-page>

If any action throws an Exception the request will be forwarded on to jsp named in the location element (in this case errorPage.jsp.)

In the JSP you can retrieve the exception from the request using the attribute "javax.servlet.error.exception".

You may also find the request uri useful, stored under the request attribute "javax.servlet.error.request_uri"
0
 

Author Comment

by:mvkraju
ID: 10941917
can you provide me sample code from errorPage.jsp

Thanks
0
 

Author Comment

by:mvkraju
ID: 10942174
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>


 
   
 
0
 

Author Comment

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

Is there any way?
0
 

Author Comment

by:mvkraju
ID: 10942494
<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
0
 
LVL 7

Expert Comment

by:searlas
ID: 10947154
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...')
0
 

Author Comment

by:mvkraju
ID: 10949641
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
0
 
LVL 7

Expert Comment

by:searlas
ID: 10950318
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)


0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:mvkraju
ID: 10960195
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.
0
 

Author Comment

by:mvkraju
ID: 10998106
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
0
 
LVL 7

Expert Comment

by:searlas
ID: 11005809
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?
0
 

Author Comment

by:mvkraju
ID: 11008186
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
0
 

Author Comment

by:mvkraju
ID: 11008208
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)
0
 

Author Comment

by:mvkraju
ID: 11008231
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>
0
 

Author Comment

by:mvkraju
ID: 11009651
o.k. it's working
This link helped me a lot

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

Thanks
0
 
LVL 7

Expert Comment

by:searlas
ID: 11012785
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now