java.lang.objectIllegalStateException异常是什么问题

关于java.lang.IllegalStateException异常的个人理解-Java/Web开发-java-电脑编程网关于java.lang.IllegalStateException异常的个人理解-Java/Web开发作者:CharmScorpioMan 和相关&&源码:&%@ page language=&& import=&.util.*& pageEncoding=&GB2312&%&&%@ include file=&inc.& %&&%String path = request.getContextPath();String basePath = request.getScheme()+&://&+request.getServerName()+&:&+request.getServerPort()+path+&/&;String name = request.getParameter(&name&);//注册用户名String pwd = request.getParameter(&pwd&);//密码String pwd1 = request.getParameter(&pwd1&);//密码确认String email = request.getParameter(&mail&);//邮箱if(name==&&||pwd==&&||pwd1==&&||email==&&||!pwd.equals(pwd1)){response.sendRedirect(path+&//regedit.jsp&);//信息不完整返回到注册页面[color=#FFFF00]//增加后解决}[/color]%&&!DOCTYPE HTML PUBLIC &-//W3C//DTD HTML 4.01 Transitional//EN&&&html&& &head&&
&base href=&&%=basePath%&&&&
&title&注册验证页面&/title&& &/head&& &%&
String uname = &scorpioman&;&
String upwd = &xuehu1104&;&
String sql = &select * from user_info_t where name='&+name+&'&;&
boolean success =//是否注册成功&
Class.forName(&.jdbc.driver.OracleDriver&);//加载驱动&
Connection conn = DriverManager.getConnection(&jdbc::thin:@localhost:1521:CHARM&,uname,upwd);//获得连接&
Statement stm = conn.createStatement();&
ResultSet rst = stm.executeQuery(sql);&
if(rst.next()) {&
response.sendRedirect(&regedit.jsp&);//用户名已经存在&
//System.out.println(&用户名称已经存在&);&
if(!rst.next()){&
sql = &insert into user_info_t(name,pwd,email) values('&+name+&','&+pwd+&','&+email+&')&;&
success = stm.execute(sql);&
//success =if(success){response.sendRedirect(&login.jsp&);}else{response.sendRedirect(&regedit.jsp&);}&
}catch(Exception e){&
e.printStackTrace();&
%&& &body&& &/body&&/html&异常:严重: Servlet.service() for servlet jsp threw exceptionjava.lang.IllegalStateExceptionat org..catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)at org..jsp.jsp.regedit_005faction_jsp._jspService(regedit_005faction_jsp.java:88)at org.apache.jer.runtime.HttpJspBase.service(HttpJspBase.java:70)at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)在自己做练习的时候发现以上的异常,百度好多次没找到其产生的原因,偶遇看到回帖说 在“response.sendRedirect(path+&/jsp/regedit.jsp&);”后加上&return&语句即可解决,尝试后果然问题解决。仔细阅读代码后发现 标注红色的 代码有可能同时执行,如果不加return,虽然页面重定向,但是代码依然继续执行。遇到第二个&response.sendRedirect&。如果同一个页面由于程序逻辑造成 两个“response.sendRedirect”同时执行会造成以上的错误。个人理解,但是具体原因不清楚,还请高人指点------回答---------------其他回答(40分)---------
的连接没有断开
相关资料:|||||||关于java.lang.IllegalStateException异常的个人理解-Java/Web开发来源网络,如有侵权请告知,即处理!编程Tags:                &                    struts2 下载时报java.lang.IllegalStateException_Java123社区微信号:java123msg |||[][]当前位置: &
& struts2 下载时报java.lang.IllegalStateException-----------------------------------------struts2 下载时报java.lang.IllegalStateException------------------------------------------------------------- 抛出java.lang.IllegalStateException异常,终于发现原因之所在: 我们在做文-----------------------------------------struts2 下载时报.lang.IllegalStateException------------------------------------------------------------- 抛出java.lang.IllegalStateException异常,终于发现原因之所在:我们在做文件上传或者下载,用到页面的输出流. 在action中使用下载的方法:&//文件下载&public String downFile() throws IOException{ &&String msg= &&&&response.setCharacterEncoding("gb2312"); &&response.setContentType("text/");&&javax.servlet.ServletOutputStream ou = response.getOutputStream();&&&//文件名 &&String filename=new String(request.getParameter("filename").getBytes("ISO8859_1"),"utf-8").toString();&&&//路径 &&&String filepath=ServletActionContext.getServletContext().getRealPath("/upload/"+filename); &&java.io.File file = new java.io.File(filepath); &&if (!file.exists()) { &&System.out.println(file.getAbsolutePath() + " 文件不能存在!"); &&&msg="抱歉,文件可能过期了!"; &&&super.addActionError(msg); &&& &&} &&&// 读取文件流 &&java.io.FileInputStream fileInputStream = new java.io.FileInputStream(file); &&// 下载文件 &&// 设置响应头和下载保存的文件名&&if (filename != null && filename.length() & 0) {&&response.setContentType("application/x-msdownload");//弹出下载的框&& &&response.setContentLength((int) file.length());//下载统计文件大小的进度 &&response.setHeader("Content-Disposition", " filename=" + new String(filename.getBytes("gb2312"),"iso8859-1") + "");&&//response.setHeader("Content-Length", file.length()); &&//下载框的信息&&if (fileInputStream != null) { &&int filelen = fileInputStream.available(); &&//文件太大时内存不能一次读出,要循环&& &&byte a[] = new byte[filelen]; & &&fileInputStream.read(a); && &&ou.write(a); &&}&&fileInputStream.close(); &&ou.close(); &&} && &&return SUCCESS; && &}抛出异常:java.lang.IllegalStateException&原因分析:这是容器天生的servlet代码中有out.write(&&),这个和JSP中调用的response.getOutputStream()产生冲突.即Servlet规范说明,不能既调用 response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出 IllegalStateException,解决: &action方法:&public String downTest(){ &&try { &&&name =new String(getFilename().getBytes("iso-8859-1"),"utf-8"); &&&tname=java.net.URLEncoder.encode(name,"utf-8"); &&& &&&String path=ServletActionContext.getServletContext().getRealPath("/upload/"+name);&&&File file=new File(path); &&&inputStream=new FileInputStream(file); &&&response.setContentLength((int) file.length());//下载统计文件大小的进度 &&& &&} catch (Exception e) { &&&e.printStackTrace();&&} &&return SUCCESS; &}&&struts2.xml: &&!-- 下载中心控制器 --& &&&action name="download" class="downloadAction"&&&&&result name="success" type="stream"& &&&&!-- 设置输入流 --&&&&&param name="inputstream"&inputStream&/param&&&&&&&&&!-- 设置下载的方式及文件名 --&&& &&&&param name="contentDisposition"&filename=${tname}&/param&&&&&& &/result& &&&&interceptor-ref name="defaultStack"&&/interceptor-ref&&&&/action&顶一下(0)0%踩一下(0)0%------分隔线------上一篇: 下一篇: 栏目列表推荐内容热点内容25769人阅读
最近在使用response.sendRedirect()时出现如下错误:
&&java.lang.IllegalStateException
&&org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:423)
经过分析、查看jdk文档终于找到解决的办法,在response.sendRedirect()方法后加return语句即可,如下:
&&response.sendRedirect(&login.jsp&);
原因是:在程序中两次调用了response.sendRedirect()方法。
jdk5.0文档中很清楚地介绍了出现IllegalStateException异常的可能情况:
1)同一个页面中再次调用response.sendRedirect()方法。
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&interdivts&it&as&relative&to&the&current&request&URI.&If&the&location&is&relative&with&a&leading&&/&&the&container&interdivts&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&
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
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:173653次
积分:1338
积分:1338
排名:第19367名
原创:11篇
评论:42条
(1)(2)(4)(3)(7)(1)(1)
淘宝-林昊的技术博客 / 放翁(文初)的一亩三分地 http://blog.csdn.net/cenwenchu79/

我要回帖

更多关于 java.lang 的文章

 

随机推荐