我知道在客户端(javascript)你可以使用windows.location.hash但无论如何都无法从服务器端访问.
我正在尝试实现一个获取原始请求的servlet,并决定处理它们,还是将它们转发到另一个后端服务器.它类似于负载均衡器,其中收到的请求被转发到(在我的情况下为2个)目的地之一.其中一个目标是远程(在另一台主机上).此外,请求可以到根(http://mycompany.com/).
因为我想获得原始请求,所以我实现了自己的servlet(子类化HttpServlet),这非常有用.我的servlet看起来像:
public class MyProxyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processOrForward(req, resp);
}
// also doGet(), doHead(), ...
}
Run Code Online (Sandbox Code Playgroud)
由于我想要处理的服务可以向root发送请求,我想将我的servlet映射为默认的servlet,从而接收任何没有显式servlet映射的请求.假设我的servlet名称是"myservlet",并且正在另一个servlet"foo"的一侧运行,我希望所有以http://mycompany.com/foo/ ... 形式发出的请求都被传递给foo,以及一切else(例如/,/ bar/...,/ myservlet/...)到"myservlet".看一下之前的帖子(例如,这里和这里的根映射,或者这里的 url重写),我以为我想出来了,但它不起作用.
这是我的web.xml:
<web-app>
<servlet>
<servlet-name>ProxyServlet</servlet-name>
<servlet-class>com.mycompany.MyProxyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ProxyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
在上面的web.xml中,对于url-pattern我试过了
"/" and "/*" and empty (i.e., <url-pattern></url-pattern>), all behave the same -->
Requests to root (/)goes to tomcat's default …Run Code Online (Sandbox Code Playgroud)