URL Rewrite achieve pseudo-static website

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/chengqiuming/article/details/100585863

A package ready Jar

The urlrewritefilter-4.0.3.jar copied to D: \ under Program \ Tomcat8 \ webapps \ urlrewrite \ WEB-INF \ lib.

Two filters prepared in web.xml

<?xml version="1.0" encoding="GBK"?>
<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">

    <!-- 配置Url Rewrite的Filter -->
    <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    </filter>
    <!-- 配置Url Rewrite的Filter拦截所有请求 -->
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

Three-defined pseudo-static mapping rules

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
    "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite>
    <rule>
        <!-- 所有配置如下正则表达式的请求 -->
        <from>/userinf-(\w*).html</from>
        <!-- 将被forward到如下JSP页面,其中$1代表
            上面第一个正则表达式所匹配的字符串 -->
        <to type="forward">/userinf.jsp?username=$1</to>
    </rule>
</urlrewrite>

Provided in the fourth corresponding JSP file

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%
// 获取请求参数
String user = request.getParameter("username");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> <%=user%>的个人信息 </title>
    <meta name="website" content="http://www.crazyit.org" />
</head>
<body>
<%
// 此处应该通过数据库读取该用户对应的信息
// 此处只是模拟,因此简单输出:
out.println("现在时间是:" + new java.util.Date() + "<br/>");
out.println("用户名:" + user);
%>
</body>
</html>

Five test

Guess you like

Origin blog.csdn.net/chengqiuming/article/details/100585863