Loading...

java .net

用Url Rewrite Filter重写url

今天发现一个好东西

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语言: 临时自用代码@代码发芽网
<?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

Java语言: 临时自用代码@代码发芽网
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");
}
}
}






 

posted on 2008-09-24 18:04 阅读(4732) 评论(4)  编辑  收藏 所属分类: 学习java

评论

# re: 用Url Rewrite Filter重写url 2008-09-25 08:37 淘声依旧

恩  回复  更多评论   

# re: 用Url Rewrite Filter重写url 2008-09-25 09:18 rubyol

这几天也看了看UrlRewrite,很不错的。  回复  更多评论   

# 为什么我的用不成啊 2008-10-06 21:38 zhjdenis

<rule>
<from>^/([a-z0-9A-Z_]+)/([a-z0-9A-Z_]+)/?$</from>
<to type="forward">>/$2.jsp?go=$1</to>
</rule>

index.jsp页面放了一个链接
<a href="result.jsp?go=3">turn to </a><br>

但是链接过去还是失败了,没有重写啊。

我部署后的log是
信息: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: destroy called
2008-10-6 21:37:10 org.apache.catalina.core.ApplicationContext log
信息: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter INFO: loaded (conf ok)

应该是部署成功了啊  回复  更多评论   

# re: 用Url Rewrite Filter重写url 2008-10-07 09:11

看看是不是写反了?

<a href="result.jsp?go=3">turn to </a><br>
改成
<a href="/3/result">turn to </a><br>   回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

公告

希望有一天

我能用鼠标双击我的钱包

然后选中一张100元

按住“ctrl+c”

接着不停的“ctrl+v”

嘻嘻~~~笑醒~~~



导航

<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(6)

随笔分类(102)

随笔档案(398)

文章分类

文章档案(10)

有趣网络

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜