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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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. NullPointe rException </exceptio n-type>
<location>myPage.html</loc ation>
</error-page>
Or
<error-page>
<error-code>500</error-cod e>
<location>myPage.html</loc ation>
</error-page>
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.
<location>myPage.html</loc
</error-page>
Or
<error-page>
<error-code>500</error-cod
<location>myPage.html</loc
</error-page>
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?
Is there any way?
ASKER
<error-page>
<exception-type>java.lang. NullPointe rException </exceptio n-type>
<location>/myPage.html</lo cation>
</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
<exception-type>java.lang.
<location>/myPage.html</lo
</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...')
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...')
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
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)
<!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)
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.
<!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.
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.IO Exception< /exception -type>
<location>/errorPage.jsp</ location>
</error-page>
<error-page>
<exception-type>javax.serv let.jsp.Js pException </exceptio n-type>
<location>/errorPage.jsp</ location>
</error-page>
<error-page>
<exception-type>java.sql.S QLExceptio n</excepti on-type>
<location>/errorPage.jsp</ location>
</error-page>
<error-page>
<exception-type>java.lang. NullPointe rException </exceptio n-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
I incorporated the followin code in web.xml
<error-page>
<exception-type>java.lang.
<location>/errorPage.jsp</
</error-page>
<error-page>
<exception-type>java.io.IO
<location>/errorPage.jsp</
</error-page>
<error-page>
<exception-type>javax.serv
<location>/errorPage.jsp</
</error-page>
<error-page>
<exception-type>java.sql.S
<location>/errorPage.jsp</
</error-page>
<error-page>
<exception-type>java.lang.
<location>/errorPage.jsp</
</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?
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
Do i need to thow it instead of catch?
Thanks
ASKER
Here is the exception
2004-05-06 13:54:46,815-Servlet.Engin e.Transpor ts : 3 (ERROR) MsgWarehouseRptAction.exec ute(670) - java.lang.NullPointerExcep tion
java.lang.NullPointerExcep tion
at com.cfg.ia.mwr.db.ConnHand ler.<init> (ConnHandl er.java:39 )
at com.cfg.ia.mwr.db.SmwSqlHa ndler.getM yConnectio n(SmwSqlHa ndler.java :89)
at com.cfg.ia.mwr.db.SmwSqlHa ndler.serv iceErrCoun tDoubles(S mwSqlHandl er.java:62 9)
at com.cfg.ia.mwr.action.MsgW arehouseRp tAction.pr ocessServi ceErrorCou nts(MsgWar ehouseRptA ction.java :263)
at com.cfg.ia.mwr.action.MsgW arehouseRp tAction.ev aluateTarg et(MsgWare houseRptAc tion.java: 73)
at com.cfg.ia.mwr.action.MsgW arehouseRp tAction.ex ecute(MsgW arehouseRp tAction.ja va:646)
at org.apache.struts.action.R equestProc essor.proc essActionP erform(Req uestProces sor.java:4 84)
at org.apache.struts.action.R equestProc essor.proc ess(Reques tProcessor .java:274)
at org.apache.struts.action.A ctionServl et.process (ActionSer vlet.java: 1482)
at org.apache.struts.action.A ctionServl et.doPost( ActionServ let.java:5 25)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 760)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
2004-05-06 13:54:46,815-Servlet.Engin
java.lang.NullPointerExcep
at com.cfg.ia.mwr.db.ConnHand
at com.cfg.ia.mwr.db.SmwSqlHa
at com.cfg.ia.mwr.db.SmwSqlHa
at com.cfg.ia.mwr.action.MsgW
at com.cfg.ia.mwr.action.MsgW
at com.cfg.ia.mwr.action.MsgW
at org.apache.struts.action.R
at org.apache.struts.action.R
at org.apache.struts.action.A
at org.apache.struts.action.A
at javax.servlet.http.HttpSer
at javax.servlet.http.HttpSer
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>
<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(
out.print(ostr);
%>
</pre>
<p>
<hr width=80%>
<p>
<a href="/index.jsp">click here to start</a>
</body>
</html>
ASKER
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.
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.
ASKER
Thanks