如何使用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!
答案 0 :(得分:265)
javax.servlet.Filter
。doFilter()
方法中,将传入的ServletRequest
投放到HttpServletRequest
。HttpServletRequest#getRequestURI()
抓住路径。java.lang.String
,substring()
,split()
之类的简单concat()
方法来提取感兴趣的部分并撰写新路径。ServletRequest#getRequestDispatcher()
然后RequestDispatcher#forward()
将请求/响应转发到新网址(服务器端重定向,未反映在浏览器地址栏中),或强制转换传入的ServletResponse
到HttpServletResponse
然后HttpServletResponse#sendRedirect()
将响应重定向到新的URL(客户端重定向,反映在浏览器地址栏中)。web.xml
url-pattern
或/*
上/Check_License/*
注册过滤器,具体取决于上下文路径,或者如果您已经使用Servlet 3.0,请使用相反,@WebFilter
注释。如果要更改URL 并且不,请不要忘记在代码中添加一个检查,然后只需调用FilterChain#doFilter()
,否则将自己称为无限循环。
或者您也可以使用现有的第三方API为您完成所有工作,例如Tuckey's UrlRewriteFilter,可以按照Apache mod_rewrite
的方式进行配置。
答案 1 :(得分:20)
您可以使用准备使用Url Rewrite Filter的规则,例如:
<rule>
<from>^/Check_License/Dir_My_App/Dir_ABC/My_Obj_([0-9]+)$</from>
<to>/Check_License?Contact_Id=My_Obj_$1</to>
</rule>
查看Examples了解更多......示例。
答案 2 :(得分:5)
基于BalusC's answer步骤的简单JSF Url Prettyfier过滤器。过滤器将以/ ui路径开头的所有请求(假设您已将所有xhtml文件存储在那里)转发到同一路径,但添加了xhtml后缀。
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 {
}
}