File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why the exception in my error page is null when error comes from a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why the exception in my error page is null when error comes from a servlet?" Watch "Why the exception in my error page is null when error comes from a servlet?" New topic
Author

Why the exception in my error page is null when error comes from a servlet?

Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
//Servlet1.java
package scwcd;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
if(true){
//throw new java.sql.SQLException("sqlException!");
throw new java.io.IOException("mellon!");
}
}catch(Exception e){
throw new ServletException(e.getMessage(),e);
}
}
//Clean up resources
public void destroy() {
}
}
//test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
test
</title>
</head>
<body>
<h1>
<%
if(true){
throw new java.io.IOException("mellon io excpeiton ");
}
%>
</h1>
</body>
</html>
//IOExceptionError.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page isErrorPage="true" %>
<html>
<head>
<title>
errorpage/IOExceptionError
</title>
</head>
<body>
<h1>
java.io.IOException error page!
<%
out.println("exception="+exception);
out.println("javax.servlet.error.exception="+request.getAttribute("javax.servlet.error.exception"));
%>
</h1>
</body>
</html>
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>scwcd.Servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.io.IOException</exception-type>
<location>/errorpage/IOExceptionError.jsp</location>
</error-page>
<error-page>
<exception-type>java.sql.SQLException</exception-type>
<location>/errorpage/SQLExceptionError.jsp</location>
</error-page>
</web-app>
The response of http://localhost:8080/servlet1 :
java.io.IOException error page!
exception=null
javax.servlet.error.exception=javax.servlet.ServletException: mellon!
The response of http://localhost:8080/test.jsp
java.io.IOException error page!
exception=java.io.IOException: mellon io excpeiton
javax.servlet.error.exception=java.io.IOException: mellon io excpeiton
I throw the same exception and the container dispatch the request to the same error page.
Why the implicit variable exception of the error page present different value?
[ February 26, 2003: Message edited by: sun mellon ]

SCJP,SCWCD1.3,SCWCD1.4,SCJD,SCBCD5,SCEA5
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
By the way,I use tomcat 4.1 as my servlet container.
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Hi sun ,
If i understand you have 2 difference output , that's it ?
In your code by declaring throws new IOException,you're creating an instance of the IOException. --->throw new IOException("mellon")
your variable 'exception' is null because you not create an instance of IOException class . You just have a variable of type IOException and the value is null.
Let me know if it is not you expected.
Thanks.
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Hi sun ,
If i understand you have 2 difference output , that's it ?
In your code by declaring throws new IOException,you're creating an instance of the IOException. --->throw new IOException("mellon")
your variable 'exception' is null because you not create an instance of IOException class . You just have a variable of type IOException and the value is null.
Let me know if it is not you expected.
Thanks.
Calina Cazangiu
Ranch Hand

Joined: Feb 27, 2003
Posts: 30
sun,
I don't know how things are supposed to work, but if you look in the servlet created for the exception handler jsp page, the exception that's retrieved is:
Throwable exception = (Throwable) request.getAttribute("javax.servlet.jsp.jspException");
while the container will set an attribute for "javax.servlet.error.exception" for an exception thrown by a servlet.
Again, I don't know if this is not implemented properly, or this is how it's supposed to work and in a JSP error page we need to handle differently the exceptions coming from jsp and servlets.
Hopefully a guru will clarify this for us.
calina
Arthur Clarke
Greenhorn

Joined: Aug 22, 2002
Posts: 9
Sun please try to do so, it will be even more interesting:
add this line into your test.jsp file:

and The response of http://localhost:8080/test.jsp will be
java.io.IOException error page!
exception=javax.servlet.ServletException: mellon io excpeiton javax.servlet.error.exception=null
My answer is this:
request.getAttribute("javax.servlet.error.exception") differs from
request.getAttribute("javax.servlet.jsp.jspException");/*exeption in jsp*/.
Good luck!
----------------
Arthur Clarke
SCJP2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why the exception in my error page is null when error comes from a servlet?