• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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 ]
 
Mellon Sun
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way,I use tomcat 4.1 as my servlet container.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic