【问题标题】:Why does getContextPath() under a proxy return the internal path inside HttpServlet but not inside Filter?为什么代理下的getContextPath()返回的是HttpServlet内部的内部路径,而不是Filter内部的路径?
【发布时间】:2012-04-20 11:43:33
【问题描述】:

一直在打扰我。

所以,我正在使用 JSP,在我实现的过滤器和扩展的 HttpServlet 中分别有这些方法(简单示例):

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    ((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/foo");
}

 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    response.sendRedirect(request.getContextPath() + "/foo");
}

localhost:8080/app,Filter 和 HttpServlet 都正确地将我重定向到 localhost:8080/app/foo

但是在www.mysite.com/app,它在Apache的代理下隐藏了一个tomcatserver:8080/app如下,

RedirectPermanent /app /app/
ProxyPass /app/ http://tomcatserver:8080/app/
ProxyPassReverse /app/ http://tomcatserver:8080/app/

过滤器将我重定向到www.mysite.com/app/foo,而 HttpServlet 要么:

  1. (来自同一域) 显示 Tomcat 的服务器地址,将我重定向到 tomcatserver:8080/app/foo
  2. (来自域外) 只是卡住了加载。

那么...是什么原因造成的?

P.S.:我知道从 HttpServlet 中删除 request.getContextPath()"/" 部分可以解决问题,我不是在问这个问题。

【问题讨论】:

    标签: apache tomcat servlets servlet-filters response.redirect


    【解决方案1】:

    因为 Tomcat 不知道反向代理的主机名。 Tomcat 看到的主机头是为它自己的。因此,这是 Tomcat 在为重定向等生成绝对 URL 时使用的主机。

    您可以通过使用来更改此设置

    ProxyPreserveHost On
    

    在您的 httpd 配置中。

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多