简体   繁体   中英

IllegalStateException for response.sendRedirect

I have a jsp that will produce a table as its output. In each row of the output there are going to be two dropdown lists for each record of output. I have two servlets that build these two dropdown lists. My problem is as follows. If I do this inside the jsp:

response.sendRedirect(deptURL);

return;

response.sendRedirect(locURL);

return;

Then the complaint is of course that the first return causes code "that will never be reached". But if I remove it, then I'm left with:

[9/10/14 17:24:39:372 EDT] 00000023 SystemErr     R java.lang.IllegalStateException
[9/10/14 17:24:39:372 EDT] 00000023 SystemErr     R     at com.ibm.ws.webcontainer.webapp.WebAppDispatcherContext.sendRedirectWithStatusCode(WebAppDispatcherContext.java:571)

as my error message. Though the above is watered down from my initial code. The problem is the same. The reality of my code is that there are two if statements, and whether or not the conditions are true, I will call upon the servlets via response.sendRedirect(url) followed by a return. So this combination does appear 4 times in the code. For the if and for its else. And for the second if and for its else. But I always receive the error for the second if (or its else) depending upon the parameters I pass to the jsp. So I hope to either see a specific Department name as the selected choice and all Department names below it, same with Location, or just the word "Select a Department" and all the Department names below it, same with Location.

But that second response.sendRedirect is just killing me. I thought these things needed a return for the commit. Any help is appreciated. I tried this out with just one return at the very end of the jsp, but the call to the first servlet did not even seem to occur.

Nelson

As stated in the comments, having 2 times response.sendRedirect(...) is a nonsense. By issuing response.sendRedirect(...) you're actually sending response to the client like this:

HTTP/1.1 302 Found
Location: http://redirectmehere.com

The client's browser then makes a new request to the url which is passed in Location http header.

GET / HTTP/1.1
Host: redirectmehere.com

Browser couldn't choose where to really redirect, if you would redirect 2 times in a single request.

However you could make a redirect chain. So that redirectmehere.com would redirect you further to redirectmehere-b.com. So your user would end on redirectmehere-b.com.

I resolved my problem by doing it this way:

response.sendRedirect(deptURL);
return;

Which called the servlet that built the dropdown list and returned control back to the jsp and then

<script language="JavaScript">
 window.location.replace("<%=locURL%>");
</script>

which called another servlet which built the other dropdown list and returned control back to the jsp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM