使用Filter 对URL进行重写
Filter类:
[cc lang="java" nowrap="false"]import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* 实现对请求URL重写
* @author chenlei
* @date Sep 22, 2010
* @time 10:46:55 PM
* @link email
*/
public class URLRewriteFilter implements Filter {
private final static Log log = LogFactory.getLog(URLRewriteFilter.class);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = null;
Document document = null;
public void destroy() {
}
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
getRewriteUrl(filterChain, request, response);
}
private void getRewriteUrl(FilterChain filterChain,
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String url = request.getServletPath();
NodeList childNodes = (NodeList) document.getElementsByTagName("rule");
for (int i = 0; i < childNodes.getLength(); i++) {
Element elemnt = (Element) childNodes.item(i);
NodeList list2 = elemnt.getElementsByTagName("from");
Element element = (Element) list2.item(0);
String formValue = element.getFirstChild().getNodeValue();
NodeList list3 = elemnt.getElementsByTagName("to");
Element element2 = (Element) list3.item(0);
String type = element2.getAttribute("type");
String toValue = element2.getFirstChild().getNodeValue();
String rewriteUrl = url.replaceAll(formValue, toValue);
if (url != null && !"".equals(url.trim())
&& !url.equals(rewriteUrl)) {
if (null != rewriteUrl && type.equals("forward")) {
request.getRequestDispatcher(rewriteUrl).forward(request,
response);
return;
} else if (null != rewriteUrl && type.equals("redirect")) {
response.sendRedirect(rewriteUrl);
return;
}
}
}
filterChain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
try {
builder = builderFactory.newDocumentBuilder();
String realPath = config.getServletContext().getRealPath("/");
document = (Document) builder.parse(realPath
+ "WEB-INF\\urlrewrite.xml");
log.info("loading ["+URLRewriteFilter.class+"] config file: [->"
+ document.getDocumentURI() + "]");
} catch (Exception e) {
e.printStackTrace();
}
}
}[/cc]
在WEB-INF目录下新建urlrewrite.xml文件,使用正则表达式匹配请求URL,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<urlrewrite>
<rule>
<from>^/view/(.*)1.html$</from>
<to type="forward">/test/$1.jsp</to>
</rule>
<rule>
<from>^/test/(.*).html$</from>
<to type="redirect">/test/$1.jsp</to>
</rule>
</urlrewrite>
在web.xml中加入如下代码:
<filter>
<filter-name>urlFilter</filter-name>
<filter-class>
com.wixs.core.util.urlfilter.URLRewriteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>urlFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 陈大雷的 Blog