• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot catch Exception in jsp

 
newworld kingdom
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in WEB-INF/web.xml,
<error-page>
<exception-type>
moreservlets.DumbDeveloperException
</exception-type>
<location>/DDE.jsp</location>
</error-page>
when DumbDeveloperException happens,cannot invoke dde.jsp. Tomcat4.1.24,anything esle is ok.
 
Coco Lee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several possible problems:
1.) It's case sensitive. Try..
<location>/dde.jsp</location>
2.) What exception are you throwing? You can only throw ServletException and IOException (and RuntimeException + subclasses of). So if you're trying to throw a DDException, you need to wrap it inside a ServletException.

The container will know to call ServletException.getRootCause(), see it as a DDException, match it with what you have in the web.xml and send the appropriate error page.
 
newworld kingdom
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,Coco
RiskyPage.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Risky JSP Page</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<H2>Risky Calculations</H2>
<%@ page import="moreservlets.*" %>
<% int n = ((int)(10 * Math.random())); %>
<UL>
<LI>n: <%= n %>
<LI>dangerousComputation(n):
<%= DumbDeveloperException.dangerousComputation(n) %>
</UL>
</BODY></HTML>
DumbDeveloperException.java
package moreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Exception used to flag particularly onerous
programmer blunders. Used to illustrate the
exception-type web.xml element.
* <P>
* Taken from More Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.moreservlets.com/.
* © 2002 Marty Hall; may be freely used or adapted.
*/
public class DumbDeveloperException extends ServletException {
public DumbDeveloperException() {
super("Duh. What was I *thinking*?");
}
public static int dangerousComputation(int n)
throws DumbDeveloperException {
if (n < 5) {
return(n + 10);
} else {
throw(new DumbDeveloperException());
}
}
}
DDE.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Dumb</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<H2>Dumb Developer</H2>
We're brain dead. Consider using our competitors.
</BODY></HTML>

when RiskyPage.jsp doesn't throw a exception,it's OK; when it throws a exception, the result is
org.apache.jasper.JasperException: Duh. What was I *thinking*?
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
................
What is the reason?

web.xml
<error-page>
<exception-type>
moreservlets.DumbDeveloperException
</exception-type>
<location>/DDE.jsp</location>
</error-page>
 
Coco Lee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
Replace this: <%= DumbDeveloperException.dangerousComputation(n) %>
With this:

[ July 13, 2003: Message edited by: Coco Lee ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic