今天发现一个好东西
Url Rewrite Filter
它可以实现url重写,从而隐藏实际的url,同时使url看起来更美观,简单
最令人兴奋的是它一下解决了一值在寻找的blog用户访问自己的空间的问题
比如http://hi.baidu.com/liuspring 就显示我的空间
1、下载Url Rewrite Filter
2、在项目的web.xml配置过滤器
XML语言:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>debug</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、将urlrewrite-2.6.0.jar放入lib文件夹
4、新建urlrewrite.xml文件置于WEB-INF目录
5、配置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">
<!--
Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/
-->
<urlrewrite>
<rule>
<from>^/([a-z]+)/?$</from>
<to type= "forward" >/blogView.do?go=$1</to>
</rule>
<rule>
<note> 这是一个通用请求url rewrite</note>
<from>^/([a-z0-9A-Z_]+)/([a-z0-9A-Z_]+)/?$</from>
<to type= "forward" >/$2.do?go=$1</to>
</rule>
<outbound-rule>
<note>
The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
the url /rewrite-status will be rewritten to /test/status/.
The above rule and this outbound-rule means that end users should never see the
url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
in your pages.
</note>
<from>/rewrite-status</from>
<to>/test/status/</to>
</outbound-rule>
</urlrewrite>
url匹配使用正则表达式的规则,
实验中发现一个问题,就是必须把里面的正则表达式用小括号括起来,在正则表达式中叫分组
不然会报异常:
java.lang.IndexOutOfBoundsException: No group 2
哈哈,前几日还费劲的自己写Servlet重写url呢,原来这有现成的,更加觉得自己现在的水平遇到的问题网上的前辈们早都遇到过了,一定要站在巨人的肩膀上,少走弯路啊。
把我的servlet贴在这,呵呵,参考自blojsom
package com.capinfo.servlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.capinfo.util.PageConstraint;
import com.capinfo.util.PigBlogUtil;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
*
* @author Administrator
*
*/
public class PigBlogServlet extends HttpServlet {
protected Log _logger = LogFactory.getLog(PigBlogServlet.class);
/**
* Initialize
*
* @param servletConfig {@link ServletConfig}
* @throws ServletException If there is an error initializing
*/
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
}
/**
* Handle requests made to
*
* @param httpServletRequest {@link HttpServletRequest} request
* @param httpServletResponse {@link HttpServletResponse} response
* @throws ServletException If there is an error serving the request
* @throws IOException If there is an error serving the request
*/
protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
// Make sure that we have a request URI ending with a / otherwise we need to
// redirect so that the browser can handle relative link generation
// if (!httpServletRequest.getRequestURI().endsWith("/")) {
// StringBuffer redirectURL = new StringBuffer();
// redirectURL.append(httpServletRequest.getRequestURI());
// redirectURL.append("/");
// if (httpServletRequest.getParameterMap().size() > 0) {
// redirectURL.append("?");
// redirectURL.append(PigBlogUtil.convertRequestParams(httpServletRequest));
// }
//
// if (_logger.isDebugEnabled()) {
// _logger.debug("Redirecting the user to: " + redirectURL.toString());
// }
//
// httpServletResponse.sendRedirect(redirectURL.toString());
//
// return;
// }
// Check for an overriding id
String blogId = httpServletRequest.getParameter(PageConstraint.GO);
if (PigBlogUtil.checkNullOrBlank(blogId)) {
String blogIdFromPath = PigBlogUtil.getBlogFromPath(httpServletRequest.getPathInfo());
if (blogIdFromPath == null) {
blogId = PageConstraint.GO1;
} else {
blogId = blogIdFromPath;
}
}
if (PigBlogUtil.checkNullOrBlank(blogId)) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "Blog ID not specified");
return;
}
StringBuffer redirectURL = new StringBuffer();
//redirectURL.append(httpServletRequest.getContextPath());
System.out.println(httpServletRequest.getRequestURI());
if(httpServletRequest.getRequestURI().indexOf("/blog/") > -1 && httpServletRequest.getRequestURI().indexOf(".jsp") == -1 ){
if(!httpServletRequest.getRequestURI().endsWith("/") && httpServletRequest.getRequestURI().indexOf(".do") > -1){
redirectURL.append(httpServletRequest.getRequestURI().substring(httpServletRequest.getRequestURI().lastIndexOf("/"), httpServletRequest.getRequestURI().length()));
}else if(httpServletRequest.getRequestURI().indexOf("/blog/") == -1){
}else{
redirectURL.append("/blogView.do");
}
redirectURL.append("?go=");
redirectURL.append(blogId);
httpServletRequest.getRequestDispatcher(redirectURL.toString())
.forward((ServletRequest)httpServletRequest, (ServletResponse)httpServletResponse);
//httpServletResponse.sendRedirect(redirectURL.toString());
}else{
httpServletRequest.getRequestDispatcher(httpServletRequest.getRequestURI())
.forward((ServletRequest)httpServletRequest, (ServletResponse)httpServletResponse);
//httpServletResponse.sendRedirect(httpServletRequest.getRequestURI());
}
System.out.println(redirectURL.toString());
return;
}
/**
* Take out of service
*/
public void destroy() {
super.destroy();
if (_logger.isDebugEnabled()) {
_logger.debug(" destroyed");
}
}
}