将 Apache 重定向到 Tomcat - 在应用程序中导航时出现 IllegalStateException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/840054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:34:40  来源:igfitidea点击:

Redirect Apache to Tomcat - IllegalStateException when navigating in the app

javaapachejsptomcatredirect

提问by Magnus Lassi

I'm using Apache 2.2 and Tomcat 6.0.18 on Windows XP. I've enabled the mod_proxy module to redirect the traffic from my Apache web server to Tomcat. I only updated the httpd.conf file to have the redirection like this:

我在 Windows XP 上使用 Apache 2.2 和 Tomcat 6.0.18。我已启用 mod_proxy 模块将流量从我的 Apache Web 服务器重定向到 Tomcat。我只更新了 httpd.conf 文件以进行如下重定向:

ProxyPass         /myapp  http://MYMACHINENAME:8080/MyApp/Start
ProxyPassReverse  /myapp  http://MYMACHINENAME:8080/MyApp/Start

The problem I'm experiencing is that the initial redirect works fine, the JSP page renders correctly. When I try to navigate to a different JSP page by clicking on a menu on the page, I get the exception:

我遇到的问题是初始重定向工作正常,JSP 页面呈现正确。当我尝试通过单击页面上的菜单导航到不同的 JSP 页面时,出现异常:

SEVERE: Servlet.service() for servlet StartIntro threw exception
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
    at StartIntro.doPost(StartIntro.java:103)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:595)

If I don't do any redirection from Apache, the navigation works fine.

如果我不从 Apache 进行任何重定向,导航就可以正常工作。

Any ideas what I should look into?

任何想法我应该研究什么?

TIA, Magnus Lassi

TIA,马格努斯·拉西

回答by BalusC

Although this is an old topic which was been poked by community, I'll post my thoughts:

虽然这是一个被社区戳到的老话题,但我会发表我的想法:

java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
    at StartIntro.doPost(StartIntro.java:103)

This can happen if something in StartIntro#doPost()has already committed the response. A response is commited when one of the following cases is met:

如果某些StartIntro#doPost()内容已经提交了响应,则可能会发生这种情况。当满足以下情况之一时,将提交响应:

  1. A response header has been set before.
  2. A forward()or include()has been invoked on the same response before.
  3. More than 2KB of data is been written to response.
  4. Less than 2KB is been written and flush()is invoked.
  1. 之前已经设置了响应头。
  2. A forward()or 之前include()已在同一响应上调用过。
  3. 超过 2KB 的数据被写入响应。
  4. 写入和flush()调用不到 2KB 。

I would doublecheck what the StartIntro#doPost()is all doing. The mentioned 2KB is appserver-dependent though, in case of Tomcat it's configureable as buffer size of the HTTP connector.

我会仔细检查一下这StartIntro#doPost()一切在做什么。提到的 2KB 是依赖于应用程序服务器的,在 Tomcat 的情况下,它可以配置为 HTTP 连接器的缓冲区大小。

I would add, a common mistake among starters is that they think that the call of a forward()or a sendRedirect()would magically exit and "jump" out of the method block, hereby ignoring the remnant of the code. For example:

我想补充一点,初学者的一个常见错误是他们认为 aforward()或 a的调用sendRedirect()会神奇地退出并“跳”出方法块,从而忽略了代码的残余部分。例如:

protected void doPost() {
    if (someCondition) {
        forward();
    }
    redirect(); // This is STILL invoked when someCondition is true!
}

This is thus actually not true. To fix this you would need to add a return;statement to the end of the ifblock, or to introduce an elseblock for the redirect()call.

因此,这实际上是不正确的。要解决此问题,您需要return;if块的末尾添加一条语句,或者elseredirect()调用引入一个块。

Hope this information helps in nailing down the root cause.

希望这些信息有助于确定根本原因。

回答by David Rabinowitz

You can use the ProxySet directive:

您可以使用 ProxySet 指令:

ProxySet ajp://mymachine:7001 <parameters>

See more at the Apache Documentation

Apache 文档中查看更多信息

回答by Jaime Hablutzel

Use AJP Proxy, that is supported by Apache 2.2 and Tomcat 6.0.18.

使用 Apache 2.2 和 Tomcat 6.0.18 支持的 AJP 代理。

You'll need to install mod_proxy_ajp for apache if it isn't installed already, and make sure tomcat is configured to listen for ajp connections (check server.xml).

如果尚未安装,您需要为 apache 安装 mod_proxy_ajp,并确保将 tomcat 配置为侦听 ajp 连接(检查 server.xml)。

By default Tomcat 6.0.18 listens for AJP connections in port 8009.

默认情况下,Tomcat 6.0.18 在端口 8009 中侦听 AJP 连接。

Then you need to add something like this in your httpd.conf

然后你需要在你的 httpd.conf 中添加这样的东西

 <Proxy *>
     AddDefaultCharset Off
     Order deny,allow
     Allow from all
   </Proxy>

   ProxyPass /yoururl ajp://ip.for.tomcat.server:8009/yoururl
   ProxyPassReverse /yoururl ajp://ip.for.tomcat.server:8009/yoururl

Then you can go to http://apachehost/yoururland all redirection should be made transparent. Furthermore it is more efficient that html proxy because ajp is a more low level protocol.

然后你可以去http://apachehost/yoururl并且所有的重定向都应该是透明的。此外,它比 html 代理更有效,因为 ajp 是一个更底层的协议。

回答by MeeT

You have commited your Response and then probably you are writing Data to JSP might be through List or JSON. If you are redirecting to the Same JSP and you are returning STRING as "success" then this exception occurs.Because Returning SUCCESS through STRUTS@ commits Your Response and it would not allow to post any further data to your JSP.

您已经提交了您的响应,然后您可能正在通过 List 或 JSON 将数据写入 JSP。如果您重定向到相同的 JSP 并且您将 STRING 返回为“成功”,那么就会发生此异常。因为通过 STRUTS@ 返回 SUCCESS 提交您的响应,并且它不允许向您的 JSP 发布任何进一步的数据。

Solution : All you need to do is RETURN NULL and Redirect to same JSP page.. Hope It helps You

解决方案:您需要做的就是 RETURN NULL 并重定向到同一个 JSP 页面.. 希望对您有所帮助