时间:2022-10-10 12:52:12 | 栏目:JAVA代码 | 点击:次
1、写一个普通类实现servlet 这个接口
2、把写好的servlet 放进服务器。
sun公司给出了两个默认的实现类 HttpServlet 和 GenericServlet
工程结构
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>
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); } }
为什么需要映射?我们是不是需要通过浏览器去访问,而浏览器需要连接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的运行过程
Servlet程序是由Web服务器调用的,Web服务器收到客户端的Servlet访问请求后:
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>
指定固定的映射路径优先级最高,如果找不到就会走默认的处理请求。
模拟指定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 访问其他的具体的路径仍然不变