aspose file tools*
The moose likes Servlets and the fly likes Override the exception handlers tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Override the exception handlers tutorial" Watch "Override the exception handlers tutorial" New topic
Author

Override the exception handlers tutorial

James Greenberg
Greenhorn

Joined: Jul 09, 2008
Posts: 19
Hi,
I'd like to create and use my own exception handler in GlassFish 2.
By default GlassFish uses the one predefined in web.xml:
<servlet-mapping>
<servlet-name>ExceptionHandlerServlet</servlet-name>
<url-pattern>/error/ExceptionHandler</url-pattern>
</servlet-mapping>

Does anyone know any tutorial on creating and applying my own exception handler (to GlassFish)?
This might be considered a bad decision but I really need it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61429
    
  67

It's easy to write your own and set up in the deployment descriptor. Everything you need to know is covered in the Servlet Specification.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Greenberg
Greenhorn

Joined: Jul 09, 2008
Posts: 19
Thanks a lot,
I thought I would need to explicitly implement some kind of exception interface or method(s) with custom parameters, but in reality it turns out much simpler, you just create an usual servlet and change the web.xml file for errors to be sent to this servlet and what you do further is up to you.

thanks again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61429
    
  67

Yes, the Servlet Specification will outline how you obtain information about the nature of the error.
James Greenberg
Greenhorn

Joined: Jul 09, 2008
Posts: 19
Yeah I had a look at it but I don't need it, I only needed to catch the hideous Exception I get using Sun's JSF: when the session times out and the user clicks a submit-like button in a (JSF) form - the user gets a screen of errors and doesn't know what to do, I needed a way to catch that error and send the user to the main page in a user-friendly fashion. I tried to catch that Exception from "usual Java code" and nothing worked since the error raises inside the JSF implementation. I started thinking dropping JSF altogether or implementing my own exception handler. Luckily implementing it turned out to be much easier than I expected.
James Greenberg
Greenhorn

Joined: Jul 09, 2008
Posts: 19
By "I had a look at it but I don't need it" I mean I don't need to know the nature of the error since I know exactly why it happens (in my scenario).
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Originally posted by James Greenberg:
Yeah I had a look at it but I don't need it, I only needed to catch the hideous Exception I get using Sun's JSF: when the session times out and the user clicks a submit-like button in a (JSF) form - the user gets a screen of errors and doesn't know what to do, I needed a way to catch that error and send the user to the main page in a user-friendly fashion. I tried to catch that Exception from "usual Java code" and nothing worked since the error raises inside the JSF implementation. I started thinking dropping JSF altogether or implementing my own exception handler. Luckily implementing it turned out to be much easier than I expected.

You want to handle ViewExpiredException? Just use the error-page directive of the web.xml for that.

Example:

James Greenberg
Greenhorn

Joined: Jul 09, 2008
Posts: 19
thanks, I implemented your suggestion but the exception doesn't get caught by that page, but the servlet mentioned above does, so I'm happy that at least that one works.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Which JSF implementation/version are you using? There was a bug related to this some years ago. Consider upgrading.
James Greenberg
Greenhorn

Joined: Jul 09, 2008
Posts: 19
I have the latest one from Sun that comes with GlassFish v2u2 on Linux (Ubuntu 8.10) with all updates. I actually earlier posted about this issue on another thread (the last reply):
http://www.nabble.com/JSP-FORM-based-authentication-redirects-to-expired-session-context-td19499341.html#a20785271
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
It just works fine here with Mojarra 1.2_10 at Tomcat 6.0.18. If I recall correctly, this was been fixed somewhere around 1.2_05.
James Greenberg
Greenhorn

Joined: Jul 09, 2008
Posts: 19
It is the "production" version that Sun is pushing. I only know that it's not fixed for this version of GlassFish. As GlassFish 3 gets final I'm gonna move to that one, or perhaps use Tomcat. Is tomcat better than GlassFish when it comes to JSF.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You can just upgrade or replace the JSF version to whatever whenever you want. Mojarra even provides a Glassfish updater tool. Check their homepage: http://javaserverfaces.dev.java.net

About Tomcat v.s. Glassfish: I myself like Tomcat when it comes into development without the need for EJB's. Glassfish starts/restarts and debugs very slow, incredibly slow. Tomcat is small, lightweight and very quick. It starts and restarts within three seconds. But when EJB comes into picture, you may better consider JBoss AS, which is based on Tomcat, but has an EJB container on its top. Not as fast as Tomcat due to the EJB thing, but it's still faster than Glassfish.
James Greenberg
Greenhorn

Joined: Jul 09, 2008
Posts: 19
Thanks for the info!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Override the exception handlers tutorial