利用tomecat的ROOT项目做一个url重定向
我们在某餐馆就餐,当你连上人家的wifi不管点击任何网站或是输入任何的网址都会跳到该商家的页面,这里我们要完成的目的和这个很像,当我们访问IP:port/.*(".*"代表任意字符,“/”除外)时直接跳入到我们指定的项目,同时如果服务器下有其他项目也不会妨碍访问。
下面我们来看下如何实现:
1.首先我们将tomecat中webapps目录下的ROOT项目删除。
2.在eclipse中创建一个名叫ROOT的web项目
3.创建一个index.jsp的页面
4.下载urlrewritefilter-4.0.3.jar放入WEB-INF>lib下
5.在WEB-INF下生成web.xml配置文件,添加过滤器:
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <!-- sets up amount conf file will be checked for reload can be a valid integer (0 denotes check every time, empty/not set denotes no reload check) --> <init-param> <param-name>confReloadCheckInterval</param-name> <param-value>30</param-value> </init-param> <!-- sets up log level (will be logged to context log) can be: TRACE, DEBUG, INFO, WARN, ERROR, FATAL --> <init-param> <param-name>logLevel</param-name> <param-value>STDOUT:DEBUG</param-value> </init-param> <!-- you can disable status page if desired can be: true, false (default true) --> <init-param> <param-name>statusEnabled</param-name> <param-value>true</param-value> </init-param> <!-- you can change status path so that it does not conflict with your installed apps (note, defaults to /rewrite-status) note, must start with / --> <init-param> <param-name>statusPath</param-name> <param-value>/status</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
6.在WEB-INF下生成urlrewrite.xml配置文件,并配置过滤器规则达到重定向的目的:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"> <!-- configuration file for urlRewriteFilter --> <urlrewrite> <rule> <name>Test Rule1</name> <note>A test rule to show a simple redirect.</note> <from>/(.*)</from> <to type="redirect">/test/test.jsp</to> </rule> </urlrewrite>
7.将ROOT项目打包成war包放入ROOT项目原目录
8.测试一下^-^127.0.0.1:8080/*(‘*’代表任意字符,‘/*’除外),url会定向到test/test.jsp页面