我做了一个过滤器来捕获HttpServletRequest
所有请求的 servlet 路径
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
// debug to see the output
String path = request.getServletPath();
filterChain.doFilter(request, response);
}
jsp 中有一个 URL 没有映射到它的控制器或视图
<div>
<spring:url value="/app" var="app_url" htmlEscape="true"/>
<a href="${app_url}"><spring:message code="label_3rd_app" /></a>
</div>
但是,当在过滤器上调试时单击 url 时,我看到request.getServletPath()
两个请求的值:
/null
/null/app
我的问题是为什么request.getServletPath()
一去不复返/app
反而?
你变得空了,因为
request.getServletPath();
用于 Servlet,并且您在 Filter 内执行此操作。要将其放入过滤器中,您必须像这样手动构建它:
HttpServletRequest request = (HttpServletRequest) req;
String path = request.getRequestURI().substring(request.getContextPath().length());
更多信息为什么:
如何在没有上下文路径的情况下获取请求 URI? https://stackoverflow.com/questions/4278083/how-to-get-request-uri-without-context-path
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)