工程结构
1、创建一个maven 项目 添加web框架(将web.xml 换成最新的4.0 后面有完整配置文件) 配置tomcat
2、导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
3、编写一个Servlet程序 编写一个普通类实现HttpServlet接口
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author crush
* idea 提示快捷键 alt + enter
* 重写快捷键 是 ctrl + o
*/
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.print("hello Servlet");
System.out.println("");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4、编写Servlet 的映射
为什么需要映射?我们是不是需要通过浏览器去访问,而浏览器需要连接web服务器,需要在web 服务器注册我们写的servlet,还需要给它一个浏览器能够访问的路径。
<?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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.crush.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
Servlet程序是由Web服务器调用的,Web服务器收到客户端的Servlet访问请求后:
动图来源于:https://www.cnblogs.com/yunlongn/p/10756573.html
1、一个Servlet 可以指定一个映射路径
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.crush.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
2、一个Servlet 可以指定多个映射路径
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.crush.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
3、一个Servlet 可以指定通用映射路径
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.crush.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello.*</url-pattern>
</servlet-mapping>
4、默认请求路径
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.crush.servlet.HelloServletDays</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
5、可以指定一些前缀或者后缀
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.crush.servlet.HelloServletDays</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>*.crush</url-pattern>
</servlet-mapping>
6、优先级问题 指定固定的映射路径优先级最高,如果找不到就会走默认的处理请求。
模拟指定error 页面
1、写一个ErrorServlet 类
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @Author: crush
* @Date: 2021-05-07 23:03
* version 1.0
*/
public class ErrorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.print("404");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2、配置映射路径
<servlet>
<servlet-name>error</servlet-name>
<servlet-class>com.crush.servlet.ErrorServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>error</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
测试:访问不存在的路径 会直接到访问我们自己写的ErrorServlet 访问其他的具体的路径仍然不变
把自己掌握的知识,传授给另外一个人,这个度真的很难把握,太简单了,没有意义,太难了,听着乏味。 🚹 我🤦♂️