关于IllegalStateException!!!

chinaemin 2003-11-26 07:00:13
在做分页处理的时候,遇到一个奇怪的问题:

--------------- 我的jsp页面 ---------------
......
// 略去了对表单输入内容的判断部分,保留了提交代码
<Script Language="JavaScript">
function checkPage(form){
var code;
code = window.event.keyCode;
if ( code == 13 ){
form.submit();
}
}
</Script>
........
//略去次要部分,只留下主要部分
//提交对象为一个Servlet
<form name="paginateForm" action="<%=request.getContextPath()%>/product/productManager" method="post">
......
<a href="<%=request.getContextPath()%>/product/productManager?thisPage=1" >首页</a>
<a href="<%=request.getContextPath()%>/product/productManager?thisPage=<%=prePage%>">上页</a>
<input name="thisPage" type="text" size="4" value="<%=curPage%>" onkeydown="checkPage(this.form)"">
<a href="<%=request.getContextPath()%>/product/productManager?thisPage=<%=nextPage%>">下页</a>
<a href="<%=request.getContextPath()%>/product/productManager?thisPage=<%=totalPage%>">尾页</a>
共<%=totalPage%>页
......
</form>


-------------------- 我的Servlet -------------------
/*主要思路和处理方法如下:
*将要显示的当前页的数据,放入Vector中,然后通过session传至JSP页面;
*showURL为要显示的JSP页面的地址,即上面的那个JSP页面;
*用sendRedirect()进行跳转;
*sendRedirect()处一直报错,加入try-catch捕获IllegalStateException验证
*/
......
Vector resultVector = null;
resultVector = getResult(sql);
session.setAttribute("resultVector",resultVector);
......
try{
response.sendRedirect(showURL);
}catch( IllegalStateException h){
System.out.println("Catch IllegalStateException in Method senRedirect()!"+ h.getMessage());
h.printStackTrace();
}
return;
......


--------------------- 运行情况 ----------------------
1、在用上页、下页、首页和尾页链接模式进行提交时,无论如何测试,均不会有异常出现。JSP与Servlet均工作正常;
2、在用<input ...>输入数字,进行action-post请求模式时:
a) 如果没有加入try-catch语句块,会不定时的出现IllegalStateException异常,并在页面打出,程序中断。异常跟踪指向response.sendRedirect(showURL)语句;而且出错频率高--基本实验这种action-post请求模式两三次就会出错一次;
b) 如果加上try-catch语句块,会在后台打印出如a)页面打印的错误,异常跟踪也指向response.sendRedirect(showURL)语句,但,程序不中断,会继续执行,在客户端无法感知异常。但是,偶尔也会出现白屏,无异常信息。

-------------------- 我的怀疑 ----------------------
以下是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. ”
我想,应该与“链接”和“action-post”这两种请求模式有关。因为,对应的是同一个JSP和Servlet,只是因为请求方式不同而已!

麻烦大家:)




...全文
354 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinaemin 2003-11-27
  • 打赏
  • 举报
回复
谢了!
希望有人能够解决!
tianyayixi 2003-11-27
  • 打赏
  • 举报
回复
不懂,替你顶一下!
chinaemin 2003-11-27
  • 打赏
  • 举报
回复
再顶一下!
chinaemin 2003-11-27
  • 打赏
  • 举报
回复
:)
chinaemin 2003-11-27
  • 打赏
  • 举报
回复
again:)
chinaemin 2003-11-27
  • 打赏
  • 举报
回复
谢谢了!
可是,为什么同样的代码,在以链接方式请求时,没有这个异常,可以action-post方式请求时就有呢?
kaibinsj 2003-11-27
  • 打赏
  • 举报
回复
太长,没时间看.
给你个参考!
sendRedirect ()错误例子
Note:在servlet里面,如果已经有一个response返回给客户端,在这个response里就不能再跟sendRedirect(),不然会产生一个IllegalStateException错误,比如:
PrintWriter pw = res.getPrintWriter();
pw.println("<html><body>Hello World!</body></html>");
pw.flush(); //Sends the response
res.sendRedirect("http://www.cnn.com"); //Tries to redirect
这样就会引发错误。
chinaemin 2003-11-26
  • 打赏
  • 举报
回复
自己来一下喽:)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧