java.lang.IllegalStateException异常解决办法

最近在使用response.sendRedirect()时出现如下错误:
  java.lang.IllegalStateException
  org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:423)

经过分析、查看jdk文档终于找到解决的办法,在response.sendRedirect()方法后加return语句即可,如下:
  response.sendRedirect("login.jsp");
  return;

原因是:在程序中两次调用了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 
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`java.lang.IllegalStateException` 是一个运行时异常,通常发生在代码尝试在不合适的时间调用某个方法或操作。以下是几种可能导致该异常的情况和解决方法: 1. 操作状态错误:当尝试在未准备好的状态下执行某个操作时,将抛出 `IllegalStateException`。例如,在视图还没有完成布局之前尝试访问视图的大小或位置。解决方法是确保在执行任何操作之前,所有必要的资源都已准备好,如使用 `View.post()` 方法在视图完成布局后再进行操作。 2. 资源已释放:当尝试使用已经被释放的资源或对象时,将抛出 `IllegalStateException`。例如,在使用已经关闭的数据库连接或已经释放的线程池执行任务时,都会导致该异常。解决方法是确保资源在使用之前都处于可用状态,并且在使用后及时释放资源。 3. 状态转换错误:当尝试从一个状态转换到另一个状态时,如果状态转换不合法,则会抛出 `IllegalStateException`。例如,在 `MediaPlayer` 对象还没有准备好时尝试播放音频文件,将抛出该异常。解决方法是确保在进行状态转换之前,所有必要的操作和条件都已满足。 4. 数据格式错误:当尝试使用不正确的数据格式或类型时,将抛出 `IllegalStateException`。例如,在尝试将一个非数字字符串转换为数字时,将抛出该异常。解决方法是确保使用正确的数据格式和类型进行操作,并进行必要的数据类型转换。 总之,要避免 `IllegalStateException` 异常,需要仔细检查代码中所有操作的前提条件和后置条件,并确保在进行任何操作之前,所有必要的资源和条件都已准备好。同时,需要合理地处理异常情况,以避免程序崩溃或出现不可预期的行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值