Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to catch exceptions in JSP

 
Brad Ward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a custom tag library and I am looking for information on how to trap the JspTagExceptions and JspExceptions in a global manner (i.e. where can I put a single catch(JspException) statement to catch everything on any JSP page.
The Javadocs for the javax.servlet.jsp.JspException class has the following statement: "A generic exception known to the JSP engine; uncaught JspExceptions will result in an invocation of the errorpage machinery".
What I am looking for are some online resources or pointers on where to learn more about this vague reference to "the errorpage machinery".
Anybody got any pointers?
Thanks,
Brad
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried this before, but it seems like it should work. You could use the error page tag in the web.xml to map all the exceptions you want to catch to either a url that will handle the exceptions, similar to the way you would handle http error codes.
I refreshed myself on the information in the web.xml dtd, but I don't recall where I read it to begin with.
 
Brad Ward
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Matthew! Your suggestion did the trick, very easily.
Others sent some links on how to handle errors via the page level specification (i.e. the <%@ page errorPage="error.jsp" %> statement), but I was looking for something I could set once and all errors on all .jsp pages would get trapped and redirected to the same error page.
Here is what I did:
In the web.xml file, I added the
<web-app>
... stuff...
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/yourpath/error.jsp</location>
</error-page>
... stuff...
</web-app>
Note you can catch any exception you like; I'm using java.lang.Exception to catch all errors.
Next, I created the error.jsp file (which I found on one of the tutorial pages on the sun site, I think). The error.jsp file looks like this:
<%@ page isErrorPage="true" %>
<html>
<body>
<h1>
Error Page
</h1>
<hr>
<h2>
Received the exception:<br>
<font color=red>
<%= exception.toString() %>
</font>
</h2>
</body>
</html>
That's it. Worked like a champ.
Thanks,
Brad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic