Solved

how to handle Exceptions in Struts?

Posted on 2004-04-07
16
237 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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
 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iterator example remove 8 74
Adjusting width and height of dropdown produced using select n option HTML tags 4 78
thymeleaf natural templating vs JSP 2 101
login jsp example 24 50
If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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