Java EE 具有在每个请求中运行的过滤器。所有请求都firstwebsite.com
将首先在代码中由过滤器处理,然后重定向到secodnwebsite.com
.
public class RedirectionFilter implements Filter {
/**
* Default constructor.
*/
public RedirectionFilter () {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String servletPath = request.getServletPath();
if (servletPath.endsWith(".html")) {
response.sendRedirect("http://secondwebsite.com" + servletPath);
}
else {
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
不要忘记将此过滤器添加到部署描述符 (web.xml)。如果您有其他过滤器,则将其放在首位,以便此过滤器首先运行。
<filter>
<display-name>RedirectionFilter</display-name>
<filter-name>RedirectionFilter</filter-name>
<filter-class>your.package.RedirectionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RedirectionFilter</filter-name>
<servlet-name>Servlet which firstwebsite.com run</servlet-name>
</filter-mapping>
也可以看看
Servlet 过滤器
过滤器接口