首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

JSP错误页面exception总是为空的有关问题

2011-12-10 
JSP错误页面exception总是为空的问题?之前在其它的项目中使用过这种方法一直好使,但这次不知道为啥会出现

JSP错误页面exception总是为空的问题?
之前在其它的项目中使用过这种方法一直好使,但这次不知道为啥会出现这种情况:

第一步:
自定义了异常体系为:
-com.swc.se.exception
  +SeDAOException(继承自SeException)
        +SeException(继承自RuntimeException)

SeException:
public   class   SeException   extends   RuntimeException  
{
public   SeException(String   message   )
{
super(message);
}
}

SeDAOException:
public   class   SeDAOException   extends   SeException  
{
  public   SeDAOException(String   message)  
{
super(message);
}
}
第二步:

错误页面为:

<%@   page   contentType= "text/html;charset=gb2312 "   %>  
<%@   page   isErrorPage= "true "   %>
<html>
<head>
<title> 错误信息! </title>
<meta   http-equiv= "content-type "   content= "text/html;charset=gb2312 ">
</head>
<body>
<center>
<h1> 错误信息! </h1>
<br>
<table   width= "500 ">
<%
if(exception!=null)
{
%>
<tr>
<td   colspan= "2 "   class= "warnning ">
<%=exception.getMessage()%>
</td>
</tr>
<%
}
out.println( "exception为null ");
%>
<tr>
<td   align= "center "   colspan= "2 ">
&nbsp; <p>
<input   type= "button "   name= "register "   value= "返回 "   onclick= "javascript:history.go(-1); ">
</td>
</tr>
<tr>
<td   align= "right "   colspan= "2 ">
<img   height= "200 "   src= "images/welcome.gif ">
</td>
</tr>
</table>
</center>
</body>
</html>

第三步:
修改web.xml:

<?xml   version= "1.0 "   encoding= "UTF-8 "?>

<web-app   version= "2.4 "   xmlns= "http://java.sun.com/xml/ns/j2ee "
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee  
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">

            <servlet>
<description>
</description>
<display-name> login </display-name>
<servlet-name> ConsumerLoginServlet </servlet-name>
<servlet-class>
com.swc.se.controller.ConsumerLoginServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name> ConsumerLoginServlet </servlet-name>
<url-pattern> /ConsumerLoginServlet </url-pattern>
</servlet-mapping>
           
                  ...


                <error-page>
<exception-type>
com.swc.se.exception.SeAuthorityException
</exception-type>
<location> /error.jsp </location>
</error-page>

</web-app>

最后一步:


代码中抛出自定义异常:
...
public   void   login(Consumer   consumer){
IConsumerDAO   consumerDAO=(IConsumerDAO)DaoFactory.getDao( "consumerDAO ");
if(!consumerDAO.verify(consumer)){
throw   new   SeDAOException( "您输入的用户名跟密码不正确,请重新输入! ");
}
}
...

在这里出现的情况是,目前程序执行到验证用户身份信息时,经过判断得出用户名跟密码确实不正确,也就是说程序执行到if(!consumerDAO.verify(consumer)){这里时会转到错误页面,但是在错误页面里:

<%
if(exception!=null)
{
%>
<tr>
<td   colspan= "2 "   class= "warnning ">
<%=exception.getMessage()%>
</td>
</tr>
<%
}
out.println( "exception为null ");
%>

并没有打印异常信息,而是打印exception为null,也就是说exception对象为空的,这是为什么呢?

[解决办法]
exception 是jsp隐含对象 所以if(exception!=null) exception是不会为null的

热点排行