试了1/0这种情况,可以正确显示
项目里碰到java.lang.IllegalStateException就直接在页面上抛了一堆异常信息,没有正确转到errorpage
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorPage.jsp</location>
</error-page>
谁碰到过或者知道这个?
项目里碰到java.lang.IllegalStateException就直接在页面上抛了一堆异常信息,没有正确转到errorpage
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorPage.jsp</location>
</error-page>
谁碰到过或者知道这个?
既然是业务手段,则要求该异常需要从业务代码中抛出(也就是我们自己开发的代码),
容器只是帮我们转到目标页面而已。而java.lang.IllegalStateException,我们看看这个类的说明:
Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation. 这个异常是由jvm或者是由容器抛出的异常(容器判断我们的应用处在不合适的状态,比如:session已经失效,或者已经向客户端返回数据却要求forward等),
所以当容器认为自身的某些规则都被违背的时候,不应该转向处理业务异常的页面,而是要转向容器自己的出错页面。
In other words, the Java environment or Java application is not in an appropriate
state for the requested operation.信号的一种方法援引了一个非法或不适当的时间。换句话说, Java环境或Java应用程序是不
是在一个适当的状态所请求的操作。
不过java.lang.Exception 和java.lang.IllegalStateException不是一类吧
没有捕捉java.lang.IllegalStateException的处理,不会跳到errorPage.jsp吧
原来JAVA我还有那么多要学的!
2)提交的URL错误,即不是个有效的URL。 sendRedirect
void sendRedirect(java.lang.String location)
throws java.io.IOException
Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to. Parameters:
location - the redirect location URL
Throws:
java.io.IOException - If an input or output exception occurs
java.lang.IllegalStateException - If the response was committed or if a partial URL is given and cannot be converted into a valid URL
异常,也就是说不是在处理你页面内容时引发的异常,所以不会重定向到你指定的错误页面。
这个异常的原因有可能是你在处理多种情况下的转发或者重定向时,调用了转发或者重定向方法后
有对request或者response进行了操作的缘故。
建议楼主检查下转发部分代码,在重定向语句后加一句return。
原来JAVA我还有那么多要学的!
没有捕捉java.lang.IllegalStateException的处理,不会跳到errorPage.jsp吧
<exception-type>java.lang.Exception</exception-type>
<location>/errorPage.jsp</location>
</error-page>
是由容器业处理异常,然后跳到指定的错误页面,切记,不要在业务代码中用try {}catch{}代码手动捕捉异常,
而是用throw抛出异常让容器来处理,否则就不能达到预定的效果。
我不这么认为!!IndexOutOfBoundsException 也是运行时异常,jsp页面也可以显示。JVM异常通过包装是可以通过友好方式显示的。
<xsd:choice>
<xsd:element name="error-code"
type="javaee:error-codeType"/> <xsd:element name="exception-type"
type="javaee:fully-qualified-classType">
<xsd:annotation>
<xsd:documentation> The exception-type contains a fully qualified class
name of a Java exception type. </xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:choice> <xsd:element name="location"
type="javaee:war-pathType">
<xsd:annotation>
<xsd:documentation> The location element contains the location of the
resource in the web application relative to the root of
the web application. The value of the location must have
a leading `/'. </xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>如果按照上述方式配置,那你的代码逻辑可能不是以此及彼这种方式写的。慢慢研究吧!采用工具观察方法栈,是否向上一层越进!
也就是说只能在jsp 页面内部发生 Exception 时,才会做相应的跳转,
如果是在数据访问层,业务层,或是在action层....里面发生Exception ,上面的配置就没有用。
最好的做法是把所有的Exception 抛到 mvc 的控制层里,然后在类似struts mvc 、 spring mvc 的配置文件中,配置一个异常页面,就能达到目的了。