//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 ]