您的位置:首页 > 财经 > 产业 > 三亚网站定制_销售软件哪个好用免费的_浏览器直接进入网站的注意事项_阿里云空间+1对1私人专属设计师

三亚网站定制_销售软件哪个好用免费的_浏览器直接进入网站的注意事项_阿里云空间+1对1私人专属设计师

2025/1/8 4:50:47 来源:https://blog.csdn.net/JCM_ZZ/article/details/143368654  浏览:    关键词:三亚网站定制_销售软件哪个好用免费的_浏览器直接进入网站的注意事项_阿里云空间+1对1私人专属设计师
三亚网站定制_销售软件哪个好用免费的_浏览器直接进入网站的注意事项_阿里云空间+1对1私人专属设计师

Tomcat UrlRewriteFilter 部署项目虚拟路径配置,路由重写,可参考配置部署前端控制路由项目(Vue,React 等)

环境:Windows 11apache-tomcat-8.5.78

1. 例:有以下 myapp 项目包:

/tomcat/└── webapps/└── myapp/├── WEB-INF/│   └── web.xml├── index.html└── my.html
<!-- 以下为 index.html 内容 -->
<!-- my.html 与 index.html 内容基本相同,仅为演示非index.html命名的文件也能被正常配置访问 -->
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Index</title><style>html,body,h1 {margin: 0;}html,body {display: flex;flex-direction: column;justify-content: center;align-items: center;width: 100%;height: 100%;background: url(/static/img/bg.png) center no-repeat;background-size: 100% 100%;}.h1 {color: gold;width: 80vw;text-align: center;margin: 0 auto;}.h1.white {color: white;margin-bottom: 30px;}</style>
</head><body><script>document.write(`<h1 class="h1 white">index.html</h1><h1 class="h1">访问路径:${window.location.pathname}</h1>`)</script>
</body></html>
<!-- 以下为 web.xml 内容 -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><!-- SERVLET 启动初始化 --><servlet><servlet-name>default</servlet-name><servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class><init-param><param-name>listings</param-name><param-value>false</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.js</url-pattern>  </servlet-mapping> <servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.css</url-pattern>  </servlet-mapping>  <servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.gif</url-pattern>  </servlet-mapping><servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.jpg</url-pattern>  </servlet-mapping> <servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.png</url-pattern>  </servlet-mapping> <servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.html</url-pattern></servlet-mapping> </web-app>

2. 启动Tomcat进行访问

  • 访问:http://localhost/myapp/
  • 响应:index.html 对应资源
  • 访问:http://localhost/myapp/index.html
  • 响应:index.html 对应资源
  • 访问:http://localhost/myapp/my.html
  • 响应:my.html 对应资源
  • 访问:http://localhost/myapp/index
  • 响应:404

3. 通过配置虚拟路径,移除项目路由前缀 /myapp 和 新增路由前缀 /myapp-custom

  • 配置 tomcat/conf/server.xml
<!-- 找到server.xml中Host标签,并插入Context标签配置,如下 -->
<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true"><!-- other code ... --><!-- 配置 / 路由 要映射到的项目目录 --><Context path="/" docBase="C:\tomcat\webapps\myapp" reloadable="false" ></Context ><!-- 配置  /myapp-custom 路径 要映射到的项目目录,此处与 / 路由资源映射相同 --><Context path="/myapp-custom"  docBase="C:\tomcat\webapps\myapp" reloadable="false" ></Context ><!-- other code ... --></Host>

4. 重启 Tomcat 访问测试

  • 访问:http://localhost/
  • 响应:index.html 对应资源
  • 访问:http://localhost/myapp/
  • 响应:index.html 对应资源
  • 访问:http://localhost/my.html
  • 响应:my.html 对应资源
  • 访问:http://localhost/myapp-custom/
  • 响应:index.html 对应资源
  • 访问:http://localhost/myapp-custom/index.html
  • 响应:index.html 对应资源
  • 访问:http://localhost/myapp-custom/my.html
  • 响应:my.html 对应资源

5. 通过配置 Servlet + UrlRewriteFilter 自定义路由访问指定资源

  • 配置 tomcat/webapps/myapp/WEB-INF/web.xml
    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><!-- SERVLET 启动初始化 --><servlet><servlet-name>default</servlet-name><servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class><init-param><param-name>listings</param-name><param-value>false</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.js</url-pattern>  </servlet-mapping> <servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.css</url-pattern>  </servlet-mapping>  <servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.gif</url-pattern>  </servlet-mapping><servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.jpg</url-pattern>  </servlet-mapping> <servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.png</url-pattern>  </servlet-mapping> <servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>*.html</url-pattern></servlet-mapping> <!-- 路由映射 --><servlet-mapping><servlet-name>default</servlet-name><url-pattern>/myapp-web/*</url-pattern></servlet-mapping><!-- 应用 UrlRewriteFilter --><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>WARN</param-value></init-param></filter><filter-mapping><filter-name>UrlRewriteFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
    
  • 配置 UrlRewriteFilter,在 tomcat/webapps/myapp/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"><!--Configuration file for UrlRewriteFilterhttp://tuckey.org/urlrewrite/--><urlrewrite><rule><note>The rule means that requests to /test/status/ will be redirected to /rewrite-statusthe url will be rewritten.</note><from>/test/status/</from><to type="redirect">%{context-path}/rewrite-status</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 theurl /rewrite-status only /test/status/ both in thier location bar and in hyperlinksin your pages.</note><from>/rewrite-status</from><to>/test/status/</to></outbound-rule><!-- 配置将 /myapp-web 映射到资源 my.html --><rule><from>^/myapp-web$</from><to>/my.html</to></rule></urlrewrite>
  • 下载 UrlRewriteFilter, 官方下载
  • 将下载的 urlrewrite-x.x.x.jar 文件复制到 tomcat/lib/ 目录下
  • 最终目录结构
    /tomcat/
    ├── lib/
    │    ├── ... other jar
    │    ├── urlrewrite-x.x.x.jar
    │    └── ... other jar
    └── webapps/└── myapp/├── WEB-INF/│   ├── urlrewrite.xml│   └── web.xml├── index.html└── my.html
  • UrlRewriteFilter 官网

6. 重启 Tomcat 访问测试

  • 访问:http://localhost/
  • 响应:index.html 对应资源
  • 访问:http://localhost/myapp/
  • 响应:index.html 对应资源
  • 访问:http://localhost/my.html
  • 响应:my.html 对应资源
  • 访问:http://localhost/myapp-custom/
  • 响应:index.html 对应资源
  • 访问:http://localhost/myapp-custom/index.html
  • 响应:index.html 对应资源
  • 访问:http://localhost/myapp-custom/my.html
  • 响应:my.html 对应资源
  • 访问:http://localhost:8888/myapp-web
  • 响应:my.html 对应资源
  • 访问:http://localhost/myapp-custom/myapp-web
  • 响应:my.html 对应资源
  • 访问:http://localhost/myapp-custom/myapp-web/my.html
  • 响应:404

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com