关于IllegalStateException!!!
在做分页处理的时候,遇到一个奇怪的问题:
--------------- 我的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,只是因为请求方式不同而已!
麻烦大家:)