jsp运行时抛出异常和捕获异常的异常,帮忙分析一下是什么错误

&&&jsp显示java后台的异常工作期间遇到的问题:将java后台捕获到的错误显示在jsp上.这样运行期间可以提前看到错误原因,提高之后的工作效率.比如说如下图:servlet中的i=0/0;java肯定会报错误:/by zero. 即除数不能为零.有时候我们开发时可能没有测试出这个问题.上线之后,测试出来的这一bug.但是正式环境中无法调试,甚至查看log也很麻烦,这时候我们完全可以让异常在页面中显示出来.如下图.事例代码在下面,是一个简单的servlet做的.&需要注意的是:这种异常是500错误,要在web.xml配置error-page.&由编辑于 10:26:40由编辑于 23:48:01猜你喜欢3个牛币请下载代码后再发表评论//error/error/.classpath/error/.mymetadata/error/.project/error/.settings/error/.settings/.jsdtscope/error/.settings/org.eclipse.jdt.core.prefs/error/.settings/org.eclipse.wst.jsdt.ui.superType.container/error/.settings/org.eclipse.wst.jsdt.ui.superType.name/error/WebRoot/error/WebRoot/error.jsp/error/WebRoot/index.jsp/error/src/error/src/servlet精精精精原精精精原精原精原原精原原精精原精原相关分享精原原精原原最近下载&LV33&LV2&LV40&LV32&LV30&LV135最近浏览&LV4&LV6&LV3&LV15&LV7&LV10&LV2&LV27暂无贡献等级&LV1扫描二维码关注最代码为好友"/>扫描二维码关注最代码为好友孤独的追梦人
java.lang.IllegalStateException异常产生的原因及解决办法
问题描述:
错误类型大致为以下几种:
java.lang.IllegalStateException:Cannot
IllegalStateException:response already commited
IllegalStateException:getOutputStream() has already been called for this request…………
错误原因:
该异常表示,当前对客户端的响应已经结束,不能在响应已经结束(或说消亡)后再向
客户端(实际上是缓冲区)输出任何内容。
具体分析:
首先解释下flush(),我们知道在使用读写流的时候数据先被读入内存这个缓冲区中,
然后再写入文件,但是当数据读完时不代表数据已经写入文件完毕,因为可能还有
一部分仍未写入文件而留在内存中,这时调用flush()方法就会把缓冲区的数据强行
清空输出,因此flush()的作用就是保证缓存清空输出。
response是服务端对客户端请求的一个响应,其中封装了响应头、状态码、内容等,
服务端在把response提交到客户端之前,会向缓冲区内写入响应头和状态码,然后
将所有内容flush。这就标志着该次响应已经committed(提交)。对于当前页面中
已经committed(提交)的response,就不能再使用这个response向缓冲区写任何东西
(注:同一个页面中的response.XXX()是同一个response的不同方法,只要其中一个
已经导致了committed,那么其它类似方式的调用都会导致 IllegalStateException异常)。
【注意】能够导致响应已经committed的操作包括:forward,
redirect, flushBuffer。
flushBuffer
public void flushBuffer()throws
Forces any content in the buffer to be written to the client. A call to this method automatically
commits the response, meaning the status code and headers will be written.
sendRedirect
public void sendRedirect( location)throws
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.
public void forward( request, response)
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary
processing of a request and another resource to generate the response.
For a RequestDispatcher obtained
via getRequestDispatcher(), the ServletRequestobject
has its path elements and parameters adjusted to match the path of the target resource.
forward should
be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws anIllegalStateException. Uncommitted
output in the response buffer is automatically cleared before the forward.
The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the
or classes
that wrap them.
注:在一次响应commit之前,所有的内容输出都将写入servlet引擎的缓冲区(tomcat或
weblogic的内容空间), 而在commit之后,上一次response向缓冲区写入的内容,将清空。
由于servlet在没有设置单线程的情况下(使用Single-Threaded
Model,servlet实现
SingleThreadModel接口,jsp使用&%@ page isThreadSafe="false"
%&),是多线程的,所以
上面所说的缓冲区,都将是该response所属的线程私有的内存空间。有了这个概念,
将可以分析碰到的关于servlet多线程的很多问题。
如果不能确认response是否已经committed. 可以调用response.isCommitted()来判断。
导致这个错误最普遍的原因是,jsp有编译错误。
常见解决办法:
①在response.sendRedirect()方法后加return语句即可,如下:
response.sendRedirect("login.jsp");
②检查提交的url是否有误。
③如果你的页面中用了清缓存代码response.flushbuffer();又用到了response.sendRedirect(url);
你可以把response.flushbuffer();去掉,或者用JS的window.location.href="url";来做转向。
④如果你用了OutputStream,而web容器生成的servlet代码中有out.write(””),这个和JSP中调用的
response.getOutputStream()冲突。out.write()这个是字符流,而response.getOutputStream()
是字节流,你不能在同一个页面中调用多个输出流。无论先调用哪一个,在调用第二个时都会抛出
IllegalStateException,因为在jsp中,out变量是通过response.getWriter得到的。在多个使用了
outputStream的<%%>语句之间不能有空格及多余的字符。也就是页面中除了使用了
outputStream的<%%>之外不能有空格或其它任何字符,在之内的语句可以有空格及回车。
在JSP页面做输出的时候有两种方式.一是通过JspWriter,另一个是通过
OutputStream,但二者互相排斥.如果并存的话就会报告以上异常.
在不得不使用OutputStream的时候.我们必须要把JspWriter舍弃掉了。找到
请求异常的页面所对应的Servlet..把其中所有使用JspWriter的语句全部去掉.
或者是到你的JSP文件里把动态输出的代码注释掉.这里注意换行和空格制表符均
为JspWriter输出.应该一起去掉.保存文件重新启动服务器你会发现上述异常
由于jsp container在处理完成请求后会调用releasePageContet方法释放
所用的PageContext object,并且同时调用getWriter方法,由于getWriter方法
与在jsp页面中使用流相关的getOutputStream方法冲突,所以会造成这种异常,
解决办法是:只需要在jsp页面的最后加上两条语句:
out.clear();
out=pageContext.pushBody();
即可(其中out,pageContext均为jsp内置对象!) 。
java.lang.IllegalStateException 解决办法
java.lang.IllegalStateException问题的发现与解决
Cannot set header. Response already committed及response.sendRedirect报java.lang.IllegalStateException的一次解决
Java.lang.IllegalStateException: commit already called
java.lang.IllegalStateException异常:简单分析和简单解决方案
java.lang.IllegalStateException异常处理
java.lang.IllegalStateException错误原因以及解决方法
java.lang.IllegalStateException异常解决办法
《IllegalStateException异常》
java.lang.IllegalStateException异常产生的原因及解决办法
Offending field: mSpans
没有更多推荐了,请问struts2 中 action抛出的异常,在jsp错误页中如何显示 - ITeye问答
在struts2 中 action在调用的过程中发生异常,抛出exception ,
后会跳转到配置好的jsp错误页。但是jsp的内置对象exception却取不到抛出异常的堆栈信息
,麻烦知道的能够告诉我愿意。感谢!
问题补充:很感谢,但是我需要的是在action中抛出异常,这个异常应该是保存好的,在跳转到jsp错误页的时候,怎么通过jsp内置对象exception显示错误信息,因为我现在用的是struts2,错误页使用jsp内置对象exception但是什么信息都没有。说明action中抛出的异常并没有保存到jsp内置对象exception中。
问题补充:页面就是使用:&s:property value="exception.message" /&
但是什么也没有
问题补充:很感谢,但是你没理解我的意思,我指的是jsp页面中的exception拿不到,所有拟给我的错误页显示exception,对我没有帮组啊。
&%@ page contentType="text/charset=UTF-8" isErrorPage="true" %&
&%@ page import="org.apache.commons.logging.LogFactory" %&
&%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&
&title&Error Page&/title&
&script language="javascript"&
function showDetail()
var elm = document.getElementById('detail_system_error_msg');
if(elm.style.display == '') {
elm.style.display = 'none';
elm.style.display = '';
&div id="content"&
//Exception from JSP didn't log yet ,should log it here.
String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
LogFactory.getLog(requestUri).error(exception.getMessage(), exception);
&img alt="system internal error" src=images/error.gif" /&
对不起,发生系统内部错误,不能处理你的请求&br /&
&b&错误信息:&/b& &%=exception.getMessage()%&
&button onclick="history.back();"&返回&/button&
&p&&a href="#" onclick="showDetail();"&点击这里查看具体错误消息&/a&,报告以下错误消息给系统管理员,可以更加快速的解决问题&/p&
&div id="detail_system_error_msg" style="display:none"&
&pre&&%exception.printStackTrace(new java.io.PrintWriter(out));%&&/pre&
这是我们的错误页面希望对你有帮助
你是在页面用&s:property value="exception.message" /&没得到吗
把我们的实现方式给你简单说一下
一、自定义异常
public class PpmsException extends Exception {
private static final long serialVersionUID = 1L;
public PpmsException(){
&&&
super();
&&& }
&&&
&&& private S
&&& public PpmsException(String message){
&&&
this.message =
&&& }
&&&
public String getMessage() {
public PpmsException(String message, Throwable cause) {
super(message, cause);
二、struts.xml添加配置
&package name="pkg" extends="struts-default" abstract="true"&
&global-results&
&result name="unhandledException"&../error.jsp&/result&
&/global-results&
&global-exception-mappings&
&exception-mapping
exception="net.northking.ppms.exception.PpmsException"
result="unhandledException" /&
&exception-mapping result="unhandledException"
exception="java.sql.SQLException"&
&/exception-mapping&
&/global-exception-mappings&
&/package&
很好理解估计不用解释
已解决问题
未解决问题servlet抛出空指针异常
servlet的空指针是由于jsp页面应用了null对象导致,里面的service方法抛出的异常。  遇到这种情况要及从错误里面捕捉异常抛出的原因,跟踪代码调试修复。  处理方式 1) 检查到对象为空时,设置对象值为空字符串或一个默认值;
  处理方式 2) 检测到对象为空时,根本不执行某操作,直接跳转到其他处理中;
处理方式 3) 检查到对象为空时,提示用户操作有错误。
tomcat发布web项目JSP页面打开报空指针异常解决方法。
关于servlet连接mysql数据库的空指针问题
servlet转发时报错空指针异常解决
request.getParameter(&参数名字&) 获取的值判空报空指针异常
servlet的init()方法抛出空指针异常
JSP空指针异常问题
Jsp的空指针异常
极其简单的一个JSP小功能+空指针异常Bug解析(内容更新)
java异常处理之空指针异常
没有更多推荐了,在这一章。 我们将讨论如何处理JSP中的异常。当编写JSP代码时,可能会导致代码错误发生在代码的任何部分。JSP代码中可能会出现以下类型的错误 -
检查异常是一个例外,通常是用户错误或程序员无法预见的问题。 例如,如果要打开文件,但找不到文件,则会发生异常。编译时不能简单地忽略这些异常。
运行时异常
运行时异常是程序员可能避免的一种例外。与检查异常相反,运行时异常在并发时被忽略。
错误并不是例外,而是出现在用户或程序员控制之外的问题。代码中通常会忽略错误,因为很少会对错误做任何事情。例如,如果发生堆栈溢出,则会出现错误。编译时也会被忽略。
下面我们将进一步讨论如何处理JSP代码中出现的运行时异常/错误。
使用Exception对象Exception对象是Throwable子类的一个实例(例如,java.lang.NullPointerException),仅在错误页面中可用。下表列出了Throwable类中可用的重要方法。
public String getMessage()
返回有关发生的异常的详细消息。此消息在Throwable构造函数中初始化。
public Throwable getCause()
返回由Throwable对象表示的异常的原因。
public String toString()
返回与getMessage()的结果连接的类的名称。
public void printStackTrace()
将toString()的结果与堆栈跟踪一起打印到System.err错误输出流。
public StackTraceElement [] getStackTrace()
返回一个包含堆栈跟踪中每个元素的数组。索引0处的元素表示调用堆栈的顶部,数组中的最后一个元素表示调用堆栈底部的方法。
public Throwable fillInStackTrace()
使用当前堆栈跟踪填充Throwable对象的堆栈跟踪,添加堆栈跟踪中的任何先前信息。
JSP提供了一个选项用来为每个JSP指定错误页面。每当页面引发异常时,JSP容器将自动调用错误页面。
以下是一个示例来指定index.jsp的错误页面。要设置错误页面,请使用&%@ page errorPage =“xxx”%&指令。
&%@ page language=&java& contentType=&text/ charset=UTF-8&
pageEncoding=&UTF-8&%&
&%@ page errorPage=&ErrorPage.jsp&%&
&!DOCTYPE html PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN& &http://www.w3.org/TR/html4/loose.dtd&&
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&&
&title&错误页面示例&/title&
// Throw an exception to invoke the error page
int x = 1;
if (x == 1) {
throw new RuntimeException(&Error condition!!!&);
现在编写一个错误处理ErrorPage.jsp,如下所示。 请注意,错误处理页面包含指令&%@ page isErrorPage =“true”%&。 该指令使JSP编译器生成异常实例变量。
&%@ page language=&java& contentType=&text/ charset=UTF-8&
pageEncoding=&UTF-8&%&
&%@ page isErrorPage=&true&%&
&!DOCTYPE html PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN& &http://www.w3.org/TR/html4/loose.dtd&&
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&&
&title&错误显示页面&/title&
&h2&错误显示页面...&/h2&
&p&Sorry, an error occurred.&/p&
&p&Here is the exception stack trace:&/p&
exception.printStackTrace(response.getWriter());
访问index.jsp,将收到一个类似于以下内容的输出 -
使用JSTL标签编写错误页面可以使用JSTL标签来编写错误页面ErrorPage2.jsp。 该页面具有与上述示例中具有差不多相同的逻辑,具有更好的结构和更多信息 -
&%@ page language=&java& contentType=&text/ charset=UTF-8&
pageEncoding=&UTF-8&%&
&%@ taglib prefix=&c& uri=&http://java.sun.com/jsp/jstl/core&%&
&%@page isErrorPage=&true&%&
&!DOCTYPE html PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN& &http://www.w3.org/TR/html4/loose.dtd&&
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&&
&title&错误显示页面&/title&
&h2&错误显示页面...&/h2&
&table width=&100%& border=&1&&
&tr valign=&top&&
&td width=&40%&&&b&Error:&/b&&/td&
&td&${pageContext.exception}&/td&
&tr valign=&top&&
&td&&b&URI:&/b&&/td&
&td&${pageContext.errorData.requestURI}&/td&
&tr valign=&top&&
&td&&b&Status code:&/b&&/td&
&td&${pageContext.errorData.statusCode}&/td&
&tr valign=&top&&
&td&&b&Stack trace:&/b&&/td&
&td&&c:forEach var=&trace&
items=&${pageContext.exception.stackTrace}&&
&p&${trace}&/p&
&/c:forEach&&/td&
访问index.jsp,将收到一个类似于以下内容的输出 -
使用try…catch块如果要处理同一页面中的错误,并希望采取一些措施,而不是触发错误页面,则可以使用try....catch块。
以下是一个简单的例子,显示如何使用try...catch块。把下面的代码放在try-catch.jsp中 -
&%@ page language=&java& contentType=&text/ charset=UTF-8&
pageEncoding=&UTF-8&%&
&!DOCTYPE html PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN& &http://www.w3.org/TR/html4/loose.dtd&&
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&&
&title&Try...Catch示例&/title&
&div style=&margin: width: 90%&&
int i = 1;
i = i / 0;
out.println(&The answer is & + i);
} catch (Exception e) {
out.println(&An exception occurred: & + e.getMessage());
访问http://localhost:8080/ExceptionHandling/try-catch.jsp,它应该生成一个类似于以下内容的输出 -
An exception occurred: / by zero
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
上一篇:下一篇:
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加3个群。
Java技术群:
(人数:2000,等级:LV5,免费:否)
MySQL/SQL群:
(人数:2000,等级:LV5,免费:否)
大数据开发群:
(人数:2000,等级:LV5,免费:否)
Python技术群:
(人数:2000,等级:LV5,免费:否)
人工智能深度学习:
(人数:2000,等级:LV5,免费:否)
测试工程师(新群):
(人数:1000,等级:LV1,免费:是)
前端技术群(新群):
(人数:1000,等级:LV1,免费:是)
C/C++技术(新群):
(人数:1000,等级:LV1,免费:是)
Node.js技术(新群):
(人数:1000,等级:LV1,免费:是)
PostgreSQL数据库(新群):
(人数:1000,等级:LV1,免费:否)
Linux技术:
(人数:2000,等级:LV5,免费:否)
PHP开发者:
(人数:2000,等级:LV5,免费:是)
Oracle数据库:
(人数:2000,等级:LV5,免费:是)
C#/ASP.Net开发者:
(人数:2000,等级:LV5,免费:是)
数据分析师:
(人数:1000,等级:LV1,免费:是)R语言,Matlab语言等技术}

我要回帖

更多关于 抛出异常 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信