如何使用 servlet 过滤器来更改传入的 servlet 请求 url
http://nm-java.appspot.com/Check_License/Dir_My_App/Dir_ABC/My_Obj_123
到
http://nm-java.appspot.com/Check_License?Contact_Id=My_Obj_123
?
更新:根据下面BalusC的步骤,我想出了以下代码:
public class UrlRewriteFilter implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
//
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
String requestURI = request.getRequestURI();
if (requestURI.startsWith("/Check_License/Dir_My_App/")) {
String toReplace = requestURI.substring(requestURI.indexOf("/Dir_My_App"), requestURI.lastIndexOf("/") + 1);
String newURI = requestURI.replace(toReplace, "?Contact_Id=");
req.getRequestDispatcher(newURI).forward(req, res);
} else {
chain.doFilter(req, res);
}
}
@Override
public void destroy() {
//
}
}
web.xml
中的相关条目如下所示:
<filter>
<filter-name>urlRewriteFilter</filter-name>
<filter-class>com.example.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>urlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我尝试了服务器端和客户端重定向,得到了预期的结果。成功了,谢谢 BalusC!
javax.servlet.Filter
。doFilter()
方法中,将传入的 ServletRequest
转换为 HttpServletRequest
。HttpServletRequest#getRequestURI()
抓取路径。java.lang.String
方法,如substring()
、split()
、concat()
等来提取感兴趣的部分并组成新路径。ServletRequest#getRequestDispatcher()
然后使用 RequestDispatcher#forward()
将请求/响应转发到新 URL(服务器端重定向,不会反映在浏览器地址栏中),或 投射传入的 ServletResponse
到 HttpServletResponse
,然后 HttpServletResponse#sendRedirect()
将响应重定向到新 URL(客户端重定向,反映在浏览器地址栏中)。web.xml
或 url-pattern
的 /*
上注册过滤器,具体取决于上下文路径,或者如果您已经使用 Servlet 3.0,请改用 /Check_License/*
注释。
,否则它将在无限循环中调用自身。或者,您也可以使用现有的第 3 方 API 来为您完成所有工作,例如 Tuckey 的 UrlRewriteFilter
,它可以像使用 Apache 的 FilterChain#doFilter()
那样进行配置。
public class UrlPrettyfierFilter implements Filter {
private static final String JSF_VIEW_ROOT_PATH = "/ui";
private static final String JSF_VIEW_SUFFIX = ".xhtml";
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = ((HttpServletRequest) request);
String requestURI = httpServletRequest.getRequestURI();
//Only process the paths starting with /ui, so as other requests get unprocessed.
//You can register the filter itself for /ui/* only, too
if (requestURI.startsWith(JSF_VIEW_ROOT_PATH)
&& !requestURI.contains(JSF_VIEW_SUFFIX)) {
request.getRequestDispatcher(requestURI.concat(JSF_VIEW_SUFFIX))
.forward(request,response);
} else {
chain.doFilter(httpServletRequest, response);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
不适用于我,所以我做了以下操作:
forward
确保链接修改后的请求
public class OldApiVersionFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
if (httpServletRequest.getRequestURI().contains("/api/v3/")) {
HttpServletRequest modifiedRequest = new HttpServletRequestWrapper((httpServletRequest)) {
@Override
public String getRequestURI() {
return httpServletRequest.getRequestURI().replaceAll("/api/v3/", "/api/");
}
};
chain.doFilter(modifiedRequest, response);
} else {
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void destroy() {}
}