简体   繁体   English

IllegalStateException用于response.sendRedirect

[英]IllegalStateException for response.sendRedirect

I have a jsp that will produce a table as its output. 我有一个jsp,它将产生一个表作为其输出。 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. 我有两个构建这两个下拉列表的servlet。 My problem is as follows. 我的问题如下。 If I do this inside the jsp: 如果我在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. 我的代码的现实是有两个if语句,以及条件是否为真,我将通过response.sendRedirect(url)调用servlet,然后返回。 So this combination does appear 4 times in the code. 因此,此组合确实在代码中出现了4次。 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. 但是我总是收到第二个if(或其其他)错误,具体取决于我传递给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. 但是第二个response.sendRedirect只是杀死了我。 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. 我在jsp的结尾尝试了一次返回,但是似乎没有发生对第一个servlet的调用。

Nelson 纳尔逊

As stated in the comments, having 2 times response.sendRedirect(...) is a nonsense. 如评论中所述,具有2次response.sendRedirect(...)是胡说八道。 By issuing response.sendRedirect(...) you're actually sending response to the client like this: 通过发出response.sendRedirect(...)您实际上是在向客户端发送响应,如下所示:

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. 然后,客户端的浏览器会对位置HTTP标头中传递的网址提出新请求。

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. 这样redirectmehere.com会将您进一步重定向到redirectmehere-b.com。 So your user would end on redirectmehere-b.com. 因此,您的用户将以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 哪个调用了servlet,该servlet构建了下拉列表并将控制权返回给jsp,然后

<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. 它调用了另一个servlet,该servlet构建了另一个下拉列表,并将控制权返回给jsp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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