Url Rewrite Filter 是一个基于java的 URL rewrite 一个包。 使用它后就可以使用一些友好的URL来代替 ?&组成的URL了。 例如可以把 http://www.cngump.com/world/china/guangzhou 转换为 http://www.cngump.com/world.jsp?country=china&city=guangzhou
步骤如下:
1. 下载和安装 Tomcat
2. 部署一个空白的 Java Web Application. urlrewrite.war
3. 下载 Url Rewrite Filter http://tuckey.org/urlrewrite/
4. 解压 下载后的 urlrewritefilter-2.6.zip 到 TOMCAT_HOME\webapps\urlrewrite\ 下
5. 修改 web.xml 如下
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app id=”WebApp_9″ version=”2.4″ xmlns=”http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd“>
<display-name>Url Rewrite</display-name>
<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>/*</url-pattern>
</filter-mapping>
</web-app>
6. 在更目录新建一个 world.jsp 文件。代码如下:
<html>
<header>
<title>world</title>
</header>
<body>
<%
String country = request.getParameter(”country”);
String city = request.getParameter(”city”);
out.write(”Country=” + country);
out.write(”<br>city=” + city);
%>
</body>
</html>
7. 修改 WEB-INF 下的 urlrewrite.xml: (使用正则式进行解释)
<urlrewrite>
<rule>
<from>/world/([0-9]+)/([0-9]+)</from>
<to>/world.jsp?country=$1&city=$2</to>
</rule>
</urlrewrite>
8. 重新加载 urlrewrite 后测试:
http://localhost:8080/urlrewrite/world/china/guangzhou
和http://localhost:8080/urlrewrite/world.jsp?country=china&city=guangzhou效果一样
|
相关推荐
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>...
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <param-name>logLevel <param-value>WARN <param-name>statusEnabled <param-value>true </filter> <filter-...
集成`URLrewrite`到Java Web应用中,通常需要在web.xml中配置Filter,指定`urlrewrite.xml`的位置,然后在Servlet容器启动时加载配置: ```xml <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-...
2. 配置web.xml:在`web.xml`中添加`Filter`和`Filter-Mapping`,以启动`urlrewrite`过滤器。 3. 编写urlrewrite.xml:这是URL重写的核心配置文件,定义了各种URL重写规则。 4. 测试与调试:运行项目,通过浏览器...
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping...
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-...
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>...
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <param-name>logLevel <param-value>info </filter> <filter-mapping> <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>/*</url-pattern> <dispatcher>...
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <!-- 设置自动更新urlrewrite.xml信息 --> <param-name>confReloadCheckInterval <param-value>60 </filter> <filter-...
例如,项目可能包含了一个简单的Java控制器,展示了如何配合过滤器实现URL重定向,以及一个`urlrewrite.xml`文件,展示了多种URL重写和重定向的规则实例。 通过深入学习和实践`urlrewrite`,开发者不仅可以优化应用...
DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN" "http://tuckey.org/res/dtds/urlrewrite2.6.dtd"> <urlrewrite> ^/([0-9]+).html$ <to type="forward">/index.jsp?id=$1 </urlrewrite> ...
- 在Web应用服务器中配置URLRewrite Filter。 - 测试并调整规则,确保URL重写效果符合预期。 5. 示例代码: 以下是一个简单的URL重写规则示例,将所有以“/product?id=”开头的请求转换为“/product/”格式: `...
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <param-name>logLevel <param-value>info <param-name>statusPath <param-value>/urlrewrite-status </filter> ...
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <param-name>logLevel <param-value>WARN <param-name>statusEnabled <param-value>true </filter> <filter-...
以下是一个简单的`urlrewrite.xml`配置示例: ```xml <urlrewrite> ^/old-page.html$ <to>/new-page ^/secure/(.*) <to type="redirect">https://example.com/$1 </urlrewrite> ``` 在这个例子中,第...
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <param-name>logLevel <param-value>WARN </filter> <filter-mapping> <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>/*</url-pattern> <dispatcher>...
配置文件`urlrewrite.xml`中的参数可以通过在`web.xml`中添加`<init-param>`标签来设置,或者在`urlrewrite.xml`中直接定义。下面详细介绍这些配置参数: 1. **confReloadCheckInterval**:配置文件重加载间隔。...