Ces*_*sar 2 java servlets jetty
我有一个在嵌入式jetty服务器上运行的应用程序.我已经定义了上下文路径:
ServletContextHandler context =...
context.setContextPath("/dev");
Run Code Online (Sandbox Code Playgroud)
我可以正确访问我的应用程序http://application.com:8080/dev
当我使用HttpServletResponse的sendRedirect函数时:
resp.sendRedirect("/login");
Run Code Online (Sandbox Code Playgroud)
形成的URL未使用应用程序上下文.它返回http://application.com:8080/login的insetad http://application.com:8080/dev/login
我如何解决这条道路?
当您sendRedirect()使用具有前导"/"的位置进行调用时,它始终相对于服务器根目录,而不是相对于应用程序上下文.要实现您想要的,您必须自己附加上下文路径,例如:
response.sendRedirect(request.getContextPath() + "/login");
Run Code Online (Sandbox Code Playgroud)
为了使它在所有上下文中工作,最好对其进行编码:
response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/login"));
Run Code Online (Sandbox Code Playgroud)