java.lang.IllegalStateException异常解决

java.lang.IllegalStateException异常解决
最近在调试xwiki的时候遇到java.lang.IllegalStateException异常,在google上baidu了一把
经过分析查看jdk文档终于找到了解决方案,在response.sendRedirect("")方法后加return语句即可:
原因是在程序中两次调用response.sendRedirect("")方法

j2ee5.0中的介绍:

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
posted on 2010-01-26 10:18  雪山飞鹄 阅读(104207)  评论(12)   编辑   收藏 所属分类:  javase

Feedback

#  re: java.lang.IllegalStateException异常解决...  2010-10-25 16:38  King_Admins
呵呵,不错!   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决...  2010-10-25 16:39  King_wangyao
IllegalComponentStateException 
在不合理或不正确时间内唤醒一方法时出现的异常信息。换句话说,即 Java 环境或 Java 应用不满足请求操作。   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决  2010-12-29 16:19  yk
请不要粘来粘去好不好 都在很多地方看到了这一个说法   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决  2011-01-07 16:22  李云武
在response.sendRedirect()方法后加return语句即可,如下: 
response.sendRedirect("index.jsp"); 
return; 

原因是:在程序中两次调用了response.sendRedirect()方法。 

jdk5.0文档中很清楚地介绍了出现IllegalStateException异常的可能情况: 

1)同一个页面中再次调用response.sendRedirect()方法。 
2)提交的URL错误,即不是个有效的URL。 

   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决  2011-03-29 13:12  wysjian
public ModelAndView downloadPrint(HttpServletRequest request,HttpServletResponse response, DatabaseForm databaseForm) { 
HashMap model = new HashMap(); 
try { 
//String doc_nme = (String) request.getParameter("doc_nme"); 
//String bfr_nme = (String) request.getParameter("bfr_nme");
response.setContentType("text/html"); 
javax.servlet.ServletOutputStream out = response.getOutputStream(); 
// 取得文件在服务器上的文件路径 
String filePatch = getServletContext().getRealPath("");//获得编译后的文件路径 
String allPathStr = filePatch + "//applets//jre-1_5_0_18-windows-i586-p.rar" ; 
File file = new File(allPathStr); 
// 读取文件流 
FileInputStream fileInputStream = new FileInputStream(file);
String bfr_nme = "jre-1_5_0_18-windows-i586-p.rar";//this.getDatabaseMgr().queryBfrUserDocNm(doc_nme);
// 下载文件 
// 设置响应头和下载保存的文件名 
response.setContentType("application/x-msdownload"); 
response.setHeader("Content-Disposition", "attachment; filename="+ new String(bfr_nme.getBytes("gb2312"),"iso8859-1") + ""); 
if (fileInputStream != null) { 
int filelen = fileInputStream.available(); 
// 文件太大时内存不能一次读出,要循环 
byte a[] = new byte[filelen]; 
fileInputStream.read(a); 
out.write(a); 

fileInputStream.close(); 
out.close(); 
return null; 
//return new ModelAndView("downLoadView", model); 
} catch (Exception e) { 
e.printStackTrace(); 
model.put("errorInfo", e.getMessage()); 
return new ModelAndView("errorView", model); 


   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决[未登录]  2012-06-05 16:04  lyy
但是我整个文档里就没有那个response.sendRedirect()语句啊,也有这个异常啊,大侠给看看: 
<% 
String filename = "D:/Program Files/Genuitec/workspaces/zoology/WebRoot/jianfang.txt"; 
//if (request.getAttribute("url") != null) { 
// filename = (String)request.getAttribute("url"); 
//} 

System.out.print("filename "+filename); 
response.setContentType("application/msword"); 
response.setHeader("Content-disposition", "inline; filename=" 
+ filename); 
BufferedInputStream bis = null; 
BufferedOutputStream bos = null; 
try { 
bis = new BufferedInputStream(new FileInputStream(new File(filename))); 
//getServletContext().getRealPath("" + filename))); 
bos = new BufferedOutputStream(response.getOutputStream()); 
byte[] buff = new byte[2048]; 
char[] c =new char[2048]; 
int bytesRead; 
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { 
bos.write(buff, 0, bytesRead); 


} catch (final IOException e) { 
System.out.println("出现IOException." + e); 
} finally { 
if (bis != null) 
bis.close(); 
if (bos != null) 
bos.close(); 

%>   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决  2012-06-07 11:26  puliang
我只是增加一个文件,为什么也报这个错误呢   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决[未登录]  2012-06-30 09:39  lucky
现在关键的是,我的项目使用struts2写的,response.sendRedirect()都是在struts2框架里面定义了的,我怎么处理呢?   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决[未登录]  2012-12-01 18:16  张三
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
at org.apache.taglibs.standard.tag.common.core.RedirectSupport.doEndTag(RedirectSupport.java:119)
at org.apache.jsp.MovieShopping_jsp._jspx_meth_c_005fredirect_005f0(MovieShopping_jsp.java:686)
at org.apache.jsp.MovieShopping_jsp._jspx_meth_c_005fif_005f2(MovieShopping_jsp.java:654)
at org.apache.jsp.MovieShopping_jsp._jspService(MovieShopping_jsp.java:369)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.apache.struts.chain.commands.servlet.PerformForward.handleAsForward(PerformForward.java:113)
at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:96)
at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决  2013-01-21 22:01  sundabing

java.lang.IllegalStateException 
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407) 
at com.dyjy.mybook.web.action.BookAction.service(BookAction.java:65) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) 
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) 
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
at java.lang.Thread.run(Thread.java:619) 
2013-1-21 22:00:34 org.apache.catalina.core.StandardWrapperValve invoke 
严重: Servlet.service() for servlet BookAction threw exception 
java.lang.IllegalStateException 
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435) 
at com.dyjy.mybook.web.action.BookAction.service(BookAction.java:74) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) 
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) 
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
at java.lang.Thread.run(Thread.java:619) 
   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决  2013-06-06 09:13  33
@yk 
程序员通病—复制粘贴   回复   更多评论 
  

#  re: java.lang.IllegalStateException异常解决   2015-08-15 09:48  xiaohei
@lucky 你的问题解决了吗?怎么解决的?我也遇到相同的问题了。sturts2的
   回复   更多评论 

你可能感兴趣的:(java,web,Exception)