URL重写
这几天蛋疼.看看别人url重写是怎么搞的..
1.解释下什么事url重写,以及它的优缺点:
URL重写,其实就是把带一大堆参数的url,变成一个看上去很规矩的url。
例:/viewthread.jsp?id=1234
重写后,可以用/viewthread/1234.html
一、自定义过滤器示例:
1.配置过滤器一般在web.xml中
<filter>
<filter-name>urlFilter</filter-name>
<filter-class>com.url.filter.URLFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>urlFilter</filter-name>
<url-pattern>*.shtml</url-pattern>
</filter-mapping>
2.新建解析规则配置文件 urlrewrite.xml
<?xml version="1.0" encoding="utf-8"?>
<urlrewrite>
<rule>
<from>^/(\w+)_(\w+)_(\d+)\.shtml$</from> 匹配表达式-正则表达式
<to type="forward">/$1.do?$2=$3</to> 解析后URL
</rule>
</urlrewrite>
3.过滤器处理类 URLFilter.java
public class URLFilter implements Filter {
public void doFilter(ServletRequest servletRequest, ServletResponse
servletResponse, FilterChain filterChain) throws
IOException,ServletException
{
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String realPath
=request.getSession().getServletContext().getRealPath("/");
String fileName = realPath + "WEB-INF\\urlrewrite.xml";
String uri = request.getServletPath();
String rewriteUrl = getRewriteUrl(uri, fileName);
if (null != rewriteUrl) {
request.getRequestDispatcher(rewriteUrl).forward(request, response);
return;
}
filterChain.doFilter(servletRequest, servletResponse);
}
private String getRewriteUrl(String url, String fileName) {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = f.newDocumentBuilder();
Document document = builder.parse(fileName);
NodeList list = document.getElementsByTagName_r("rule");
for (int i = 0; i < list.getLength(); i++) {
Element elemnt = (Element) list.item(i);
NodeList list2 = elemnt.getElementsByTagName_r("from");
Element element = (Element) list2.item(0);
String formValue = element.getFirstChild().getNodeValue();
NodeList list3 = elemnt.getElementsByTagName_r("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)) {
return rewriteUrl;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
二、 Tomcat的过滤器组件
1:下载urlrewrite-2.6.0.现在最新版本是3.1 的。
http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip
http://urlrewritefilter.googlecode.com/files/urlrewritefilter-3.1.0.zip
把 urlrewrite-2.6.0.jar拷到classpath下。
2:在WEB-INF目录下建一个urlrewrite.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<urlrewrite>
<rule>
<from>^/(\w+)_(\w+)_(\d+)\.shtml$</from>
<to type="forward">/$1.do?$2=$3&pageSize=15</to>
</rule>
</urlrewrite>
解释:
<from></from>写上你自己定义的访问地址
<to type="forward></to>就是实际的访问地址。
比如我们实际的访问地址是:
/URLRewriterDemo/ uRLTestAction.do?pageNumber=123
而我们想把它重写:
/URLRewriterDemo/uRLTestAction_pageNumber_123.shtml
这样看起来比我们实际的要好看的多。我们就应该这样的写
<rule>
<from>^/(\w+)_(\w+)_(\d+)\.shtml$</from>
<to type="forward">/$1.do?$2=$3&pageSize=15</to>
</rule>
注意:常用的&要用 & 来表示。$1,$2代表与你配置正规表达式>/(\w+)/(\w+)/相对应的参数。<to type="forward">默认的是 type="forward".
3:在web.xml中初始化。加上下面的代码:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>*.shtml</url-pattern>
</filter-mapping>
三、使用系统的404错误进行
当请求时肯定是找不见相关页面了,那就跳到Error.jsp 进行相应的处理
1.加入错误跳转配置 web.xml
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
2.写错误页面 Error.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%
response.setStatus(HttpServletResponse.SC_OK);
String key = (String)
request.getAttribute("javax.servlet.forward.servlet_path");
String reg = "^/(\\w+)_(\\w+)_(\\d+).shtml$";
String rewriteUrl = key.replaceAll(reg,"/$1.do?$2=$3&pageSize=15");
if(key != null || !key.equals(rewriteUrl)){
request.getRequestDispatcher(rewriteUrl).forward(request,response);
}
else{
out.print("对不起,您请求的页面没有找到! ");
}
%>