• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to catch exception from Servlet in error.jsp

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not know how that smiley came. It is params. Sorry if any one misinterprets.
 
Bharatesh H Kakamari
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just an example :

 
James Long
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic