如何在 Java 中使用 servlet 过滤器来更改传入的 servlet 请求 url? [英] How to use a servlet filter in Java to change an incoming servlet request url?
问题描述
如何使用 servlet 过滤器更改传入的 servlet 请求 url 来自
How can I use a servlet filter to change an incoming servlet request url from
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下面的步骤,我想出了以下代码:
Update: according to BalusC's steps below, I came up with the following code:
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
中的相关条目如下所示:
The relevant entry in web.xml
look like this:
<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!
I tried both server-side and client-side redirect with the expected results. It worked, thanks 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(客户端重定向,反映在浏览器地址栏中). - 在
/*
或/Check_License/*
的url-pattern
上的web.xml
中注册过滤器>,取决于上下文路径,或者如果您已经使用 Servlet 3.0,请使用@WebFilter
注释.
- Implement
javax.servlet.Filter
. - In
doFilter()
method, cast the incomingServletRequest
toHttpServletRequest
. - Use
HttpServletRequest#getRequestURI()
to grab the path. - Use straightforward
java.lang.String
methods likesubstring()
,split()
,concat()
and so on to extract the part of interest and compose the new path. - Use either
ServletRequest#getRequestDispatcher()
and thenRequestDispatcher#forward()
to forward the request/response to the new URL (server-side redirect, not reflected in browser address bar), or cast the incomingServletResponse
toHttpServletResponse
and thenHttpServletResponse#sendRedirect()
to redirect the response to the new URL (client side redirect, reflected in browser address bar). - Register the filter in
web.xml
on anurl-pattern
of/*
or/Check_License/*
, depending on the context path, or if you're on Servlet 3.0 already, use the@WebFilter
annotation for that instead.
如果 URL 需要 更改,请不要忘记在代码中添加检查,如果不需要,则只需调用 FilterChain#doFilter()
,否则会无限循环调用自己.
Don't forget to add a check in the code if the URL needs to be changed and if not, then just call FilterChain#doFilter()
, else it will call itself in an infinite loop.
或者,您也可以使用现有的 3rd 方 API 为您完成所有工作,例如 Tuckey 的 UrlRewriteFilter 可以像使用 Apache 的 mod_rewrite
一样进行配置.
Alternatively you can also just use an existing 3rd party API to do all the work for you, such as Tuckey's UrlRewriteFilter which can be configured the way as you would do with Apache's mod_rewrite
.
这篇关于如何在 Java 中使用 servlet 过滤器来更改传入的 servlet 请求 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!