{"id":585,"date":"2004-04-06T23:20:01","date_gmt":"2004-04-07T03:20:01","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=585"},"modified":"2004-04-06T23:20:01","modified_gmt":"2004-04-07T03:20:01","slug":"upgrading-from-tomcat-4x-to-5019-javalangillegalstateexception","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/04\/06\/upgrading-from-tomcat-4x-to-5019-javalangillegalstateexception\/","title":{"rendered":"Upgrading from Tomcat 4.x to 5.0.19: java.lang.IllegalStateException"},"content":{"rendered":"<p>Seems like this new &#8216;feature&#8217; of Tomcat 5 is buried in Google, bringing it to the top for anyone interested. If you port a servlet from Tomcat 4.x to Tomcat 5 and you use (pseudo) code like this:<br \/>\n<code><br \/>\npublic void doPost(HttpServletRequest req,HttpServletResponse res) {<br \/>\n&nbsp;&nbsp; \/\/ some sort of logic<br \/>\n&nbsp;&nbsp;if (something) {<br \/>\n&nbsp;&nbsp;&nbsp;forward(\"\/somewhere\", req, res);<br \/>\n&nbsp;&nbsp;} else {<br \/>\n&nbsp;&nbsp;&nbsp;\/\/ do this<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp; \/\/ continue on with the page<br \/>\n}<\/p>\n<p>public void forward(String url, HttpServletRequest req, HttpServletResponse res)<br \/>\nthrows IOException, ServletException {<br \/>\n&nbsp;&nbsp;RequestDispatcher rd = req.getRequestDispatcher(url);<br \/>\n&nbsp;&nbsp;rd.forward(req, res);<br \/>\n}<br \/>\n<\/code><br \/>\nyou&#8217;ll find that the servlet will throw a <code><a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/lang\/IllegalStateException.html\">java.lang.IllegalStateException<\/a><\/code> with the message: <code>Cannot forward after response has been committed<\/code>. This condition is easily fixed by adding a &#8216;return&#8217; statement  after you call the forward method. So instead, you&#8217;d have this:<br \/>\n<code><br \/>\n&nbsp;&nbsp;if (something) {<br \/>\n&nbsp;&nbsp;&nbsp;forward(\"\/somewhere\", req, res);<br \/>\n&nbsp;&nbsp;&nbsp;return;<br \/>\n&nbsp;&nbsp;} else {<br \/>\n&nbsp;&nbsp;&nbsp;\/\/ do this<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp; \/\/ continue on with the page<br \/>\n<\/code><br \/>\nI bet this one catches alot of people coming from a scripting background where it&#8217;s fine and dandy to do a response.redirect() (ASP) or a &lt;cflocation&gt; and then continue on as if nothing had happened.   <\/p>\n<p>Read it on jakarta.apache.org <a href=\"http:\/\/jakarta.apache.org\/tomcat\/faq\/misc.html#illegalstate\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Seems like this new &#8216;feature&#8217; of Tomcat 5 is buried in Google, bringing it to the top for anyone interested. If you port a servlet from Tomcat 4.x to Tomcat 5 and you use (pseudo) code like this: public void doPost(HttpServletRequest req,HttpServletResponse res) { &nbsp;&nbsp; \/\/ some sort of logic &nbsp;&nbsp;if (something) { &nbsp;&nbsp;&nbsp;forward(&#8220;\/somewhere&#8221;, req, &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/04\/06\/upgrading-from-tomcat-4x-to-5019-javalangillegalstateexception\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Upgrading from Tomcat 4.x to 5.0.19: java.lang.IllegalStateException<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/585"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=585"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/585\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}