關於java.lang.IllegalStateException


今天調試程序時遇到了java.lang.IllegalStateException org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:423)這個異常,百度了一下引起這個異常的原因是如下:

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

經分析我這邊的情況是第一種即在同一個方法中一次以上的連續調用了response.sendRedirect()方法,知道根源解決方法就比較簡單了,避免多次連續的調用response.sendRedirect()方法。

這個問題在我們的系統中,是因為判斷是否有注入式攻擊字符時產生的,因為CR、LF在不同的系統中表示不同的回車換行的意義,在判斷是否為注入攻擊的字符時他倆也在其列,不過從前台返回的字符中難免不包含這兩個字符的,當然,我們現在關心是產生上面的異常的原因,就是上面的1)2)兩個原因,而根源和判斷是否有注入攻擊字符相關

擴展鏈接:

1:深入理解ServletRequest與ServletResponse

http://lavasoft.blog.51cto.com/62575/275586

2:HttpServletRequest、 ServletRequest、Request 

http://blog.163.com/magicc_love/blog/static/185853662201201023521297/

3:關於linux和windows的CR, LF, CR/LF 回車 換行問題

http://www.cnblogs.com/lihong/archive/2011/02/19/1958349.html

http://www.360doc.com/content/11/0113/20/3508740_86319358.shtml

當然,這個問題的解決過程是一個很好的鍛煉過程,同時也證明對於問題本身的求解必須具備打破沙鍋問到底、不達目的不罷休的態度,才能夠對問題本身有比較深入的了解,也才能找到問題的根源,從而從根本上將問題徹底解決!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM