This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to catch exception from Servlet in error.jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How to catch exception from Servlet in error.jsp " Watch "How to catch exception from Servlet in error.jsp " New topic
Author

How to catch exception from Servlet in error.jsp

James Long
Greenhorn

Joined: Feb 02, 2001
Posts: 14
Can any one tell me:
How to catch exception from Servlet in error.jsp page in order to show an error message to user if there is something wrong when execute the Servlet.
In Servlet I use:
if(something wrong){
throw new Exception("Something wrong");
}
The error message always not show in error.jsp page
instead it show in a new page with system generate error
message, which is not user friendly.
How can catch it using error.jsp?

Thank you in advance!!
Long
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
First of all you cannot use a generic Exception class as in your code :

Create your Exception class (eg YourException.class) that extends the Exception class. Then change the above code to YourException instead of Exception.
I think you have defined error.jsp to solve this problem. But in the case of JSPs even through it converts to a class file at runtime, you do not know the same of the .class file it generates. Thus error.jsp will not solve this problem.
Alternately you can use the <jsp:forward> with <jsp arams> to pass the error-message with your error.jsp.
HTH
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
I do not know how that smiley came. It is params. Sorry if any one misinterprets.
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
Instead of <jsp:forward> you can use RequestDispatcher forward() method to forward control to another jsp, servlet from a servlet.
HTH
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
Just an example :

James Long
Greenhorn

Joined: Feb 02, 2001
Posts: 14
Bharatesh H Kakamari:
Sorry to respond late to you!
Your help is very valuable. Thank you very much!!!
I have another question is that:
I have a DoSomethingServlet to created a MyBean object and put it into session object, then DoSomethingServlet foward request to showMyBean.jsp page the show the result.
At the beginning of showMyBean.jsp I coded the following:
============
<jsp:useBean id="myBean" class="package.MyBean"
cope="session"/>

<%if(session.getAttribute("myBean") != null){
myBean = (MyBean)session.getAttribute("myBean");

if(myBean == null){
System.out.println("The sessin object is null now")
%>
<jsp:forward page="/MyApp/servlet/DoSomethingServlet" />
============
I check the session myBean if there is one,
then I get the MyBean object from the session.
Then check if the MyBean object is null
then I foward request to the DoSomethingServlet to
do something and create MyBean object and put into session
object again.
======
The problem occurs when I have not touched the showBean.jsp page in the browser for certain minutes (30? 20? minutes), the session may be time out, and I click a link in the jsp page for
this showMyBean.jsp page.
The result is a blank page show in the broswer instead of the new showMyBean.jsp page and I check console which show "The sessin object is null now" message.
My question is why
<jsp:forward page="/MyApp/servlet/DoSomethingServlet" />
doesn't work for me at this situation???
How can I get the new showMyBean.jsp page,
i.e. how can I invoke DoSomethingServlet again?
Anything wrong???
If any one, can tell me how to solve this problem. It would be very appriciated!
Thank you in advance!
J. Long
 
GeeCON Prague 2014
 
subject: How to catch exception from Servlet in error.jsp