9.15JavaWeb之Servlet虚拟路径映射
什么是虚拟路径映射
概念:
客户端通过 URL 地址来访问 Web 服务器中的资源,Servlet 程序若想被外界访问,就必须被映射到一个 URL 地址上。
特点:
-
该 URL 地址和 Servlet 程序的物理路径(在硬盘上的存储位置)不一致,被称为虚拟路径。
-
Servlet 与虚拟路径的对应关系叫做 Servlet 虚拟路径映射。
Servlet虚拟路径映射分类
-
单一映射
-
多重映射
单一映射
使用web.xml实现单一映射
使用@WebServlet实现单一映射
使用urlPatterns实现单一映射
web.xml实现单一映射
使用方法:
使用 <servlet>
和 <servlet-mapping>
元素实现 Servlet 单一映射
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<servlet>
<servlet-name>MyServletDemoNo3</servlet-name>
<servlet-class>net.biancheng.www.MyServletDemoNo3</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServletDemoNo3</servlet-name>
<url-pattern>/MyServletDemoNo3</url-pattern>
</servlet-mapping>
</web-app>
@WebServlet实现单一映射
使用方法:
直接在@WebServlet注解后+路径:
package ServletPractice;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 编写一个Servlet容器,不使用注释的方法
* @since JDK 1.8
* @date 2021/09/15
* @author Lucifer
*/
@WebServlet("/MyServletDemoNo6")
public class MyServletDemoNo6 extends HttpServlet {
//Get方法
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向控制台打印内容
PrintWriter writer = response.getWriter();
writer.write("HelloMyServletDemoNo6");
writer.close();
}
//销毁对象
@Override
public void destroy(){
System.out.println(this.getServletName() + ":销毁");
}
//初始化Servlet容器
@Override
public void init() throws ServletException {
System.out.println(this.getServletName() + ":初始化完成");
}
}
urlPatterns实现单一映射
在@WebServlet内使用urlPatterns属性:
package ServletPractice;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 编写一个Servlet容器,不使用注释的方法
* @since JDK 1.8
* @date 2021/09/15
* @author Lucifer
*/
@WebServlet(urlPatterns = "/MyServletDemoNo6")
public class MyServletDemoNo6 extends HttpServlet {
//Get方法
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向控制台打印内容
PrintWriter writer = response.getWriter();
writer.write("HelloMyServletDemoNo6");
writer.close();
}
//销毁对象
@Override
public void destroy(){
System.out.println(this.getServletName() + ":销毁");
}
//初始化Servlet容器
@Override
public void init() throws ServletException {
System.out.println(this.getServletName() + ":初始化完成");
}
}
Servlet多重映射
配置多个
<servlet-mapping>
元素配置多个
<url-pattern>
子元素@WebServlet+
urlPatterns
实现
配置多个<servlet-mapping>
元素
配置多个元素,使多个元素指向一个<servlet-name>
元素:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" metadata-complete="false" version="4.0">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>net.biancheng.www.MyServletDemoNo3</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myServlet2</url-pattern>
</servlet-mapping>
</web-app>
这样配置以后在网页可以访问到该Servlet类的URL就有两个:
-
http://localhost:8080/servletPractice/myServlet
-
http://localhost:8080/servletPractice/myServlet2
还可以在一个servlet-mapping
下配置多个<url-pattern>
子节点:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" metadata-complete="false" version="4.0">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>net.biancheng.www.MyServletDemoNo3</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
<url-pattern>/myServlet3</url-pattern>
</servlet-mapping>
</web-app>
@WebServlet实现多重映射
实现方法:
使用@WebServlet注释+urlPattern实现:
package ServletPractice;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 使用@WebServlet注释+urlPatter实现多重映射一个Servlet资源
* @since JDK 1.8
* @date 2021/09/15
* @author Lucifer
*/
@WebServlet(urlPatterns = {
"/MyServletNo1",
"MyServletNo2"
})
public class MyServletDemoNo7 extends HttpServlet {
//定义一些属性
private static final long serialVersionUID = 1L;
private int initCount = 0;
private int httpCount = 0;
private int destoryCount = 0;
//Get方法
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向控制台打印结果
httpCount++;
// 控制台输出doGet方法次数
System.out.println("doGet方法:" + httpCount);
// 设置返回页面格式与字符集
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
// 向页面输出
writer.write("初始化次数:" + initCount + "<br/>" + "处理请求次数:" + httpCount + "<br/>" + "销毁次数:" + destoryCount);
writer.close();
}
//Post方法
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
doGet(request, response);
}
//初始化Servlet容器--->getServletName方法比较关键
@Override
public void init(){
System.out.println(this.getServletName() + ":初始化完成");
}
//销毁Servlet容器
@Override
public void destroy(){
System.out.println(this.getServletName() + "销毁完成");
}
}
这种方式也可以实现访问不同的Url获取到相同的Servlet资源