关于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,只是因为请求方式不同而已!
麻烦大家:)
--------------- 我的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,只是因为请求方式不同而已!
麻烦大家:)
...全文
相关推荐
Android异常 java.lang.IllegalStateException解决方法
"本文主要讨论了在Android开发中遇到的两个特定类型的IllegalStateException异常:'No wrapped connection'和'Adapter is detached',这
java.lang.IllegalStateException: getOutputStream() has already解决办法
纠结了半天的 java.lang.IllegalStateException: getOutputStream() has already。这个问题困扰了半天,在网上查阅了大量资料出这个错误一般就是下
java.lang.IllegalStateException: OutputStream already obtain
**捕获并处理异常**:在可能出现异常的地方添加适当的try-catch块,捕获并处理IllegalStateException。
IllegalStateException如何解决.md
throws IllegalStateException { // 这里是一些操作,可能导致IllegalStateException // 如果当前状态不允许执行此操作,则抛出IllegalStateException
action-buffer:提供了在处理后台任务时避免片段过渡期间出现 IllegalStateException 的示例
该项目提供了一种防止在Fragment转换过程中因Activity状态保存后更新UI而引发IllegalStateException的方法。通过引入ActionBuffer机制,将界面操作暂存并在合适


Web 开发
