Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

jsp access exception

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi - with struts can I actaully access the exception that was thrown from the action in the jsp? ie I want to access one of the exceptions fields to retrieve some information to display to the user.
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Store the exception as a request or session attribute and access the attributes in your jsp.
 
rob harvey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does it not get stored automatically? i dont want to handle it in my action - it is declared in the config file and thrown from the service layer
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Store the exception as a request or session attribute and access the attributes in your jsp.



Its not the right way of handling exception. Is it some transfer object which you are adding it in request or session scope.

Use struts declarative exception handling mechanism of to handle the exception.

Read this article...

Exception Handling

Naseem
 
rob harvey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I know how to handle exceptions - I just want to know if I can access a property of one of the ones thrown in the jsp - ie ive declared it in the struts config - its thrown - then in the jsp i forward to i want to access it.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I come to a solution. I personally tried your case by writing a simple program. What I have done is I caught the exception in Action and gets its wrapped exception message, created ActionError sets the exception message in error, put that error in request scope and forward to an error.jsp page.

Here is my TestException class...



2)
Here is execute() method of Action class...



3)
error.jsp page




Naseem
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I'd suggest:

1-Write a Java class that extends org.apache.struts.action.ExceptionHandler

2-In your struts-config.xml file when you define a global exception, specify your class name in the "handler" attribute of the <exception> tag.

3-In your Exception Handler class, override the execute() method so that it takes the exception that is passed as a parameter and puts it in request scope. Make sure you call the original method first, though. Something like this:



4-In your JSP, you can then retrieve the exception from request scope and do whatever you want with it.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill,

Is it correct to put exception objects in request scope? If its corect, then why we need a seperate handler for that. In Action itself, we can catch the same exception and put it in request scope and we will get the same result.

Thanks & Regards

Naseem
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The advantage to using the ExceptionHandler over simply catching the exception in the Action class is this:

One of the main reasons for using the Struts Global Exception feature is to avoid having to put try/catch blocks in every Action class. If we use an ExceptionHandler, we can still avoid try/catch blocks because the ExceptionHandler gets called by Struts when it catches an exception. If we catch the exception in every action class, we really don't need to use the global exception feature, because we've already caught the exception ourselves and can handle it. We don't need Struts to catch it for us.

Regarding your question about whether it is correct to put an Exception in the request: As far as I'm concerned, it's correct to put anything in the request that you want to put there. Generally, you want to put JavaBeans in the request, since most of the custom tags only work with JavaBeans. Even though the Exception class is not a JavaBean, the getMessage method follows the JavaBean convention, making it usable by a tag.

For example, if you implemented the code I showed above for the ExceptionHandler, there would be an Exception object in the request with the name "theException". In your JSP, you could display the exception's message with the code:

<bean:write name="theException" property="message" />

If you'd rather put an ActionMessages object into the session rather than the raw Exception class, you can still do that in the ExceptionHandler class if you want.
[ July 25, 2006: Message edited by: Merrill Higginson ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Exactly. Thanks Merrill for your suggestions.

Regards

Naseem
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic