文章目录
- 1.Servlet整体框架
- 2.Servlet快速入门
- 1.创建项目配置基本环境
- 2.添加jar包
- 1.在WEB-INF下创建目录lib,添加文件
- 2.添加到项目中
- 3.配置代码提示
- 3.src下创建文件
- 4.实现Servlet接口
- 5.在web.xml配置HelloServlet
- 6.通过浏览器访问HelloServlet
- 3.浏览器访问Servlet的流程分析
- 一共有三个容器
- url-name
- name-全类名
- name-实例
- Servlet的生命周期
- 初始化阶段
- 处理浏览器请求阶段
- 终止阶段
- 4.GET和POST的分发处理
- 代码示例
- 5.继承HttpServlet来开发Servlet
- 1.创建文件
- 2.配置web.xml
- 3.实现doGet和doPost方法并编写
- 4.浏览器访问
- 5.过程解析
- 1.http://localhost:8080/servlet/hiServlet发送请求到Tomcat服务
- 2.Tomcat服务根据资源路径/hiServlet,找到HelloServlet.HiServlet
- 3.创建HiServlet的实例,并调用它的init()方法和service()方法
- 4.init()方法就是个空的,重点看service()方法,是它的父类HttpServlet的
- 6.debug方法
- 1.打断点
- 2.debug
- 3.输入网址
- 4.此时由于是get方式,所以程序会停在断点处
- 5.查看运行类型的确是HiServlet
- 6.Servlet注意事项
- 最简化的浏览器访问Servlet过程
- 7.Servlet注解方式
- 1.快速入门
- 1.代码实例
- 2.结果
- 2.Servlet注解方式解密
- 1.与xml方式的区别
- 简要介绍
- 2.Servlet注解的参数说明
- 3.注解URL的4种匹配方式
- 1.精确匹配
- 2.目录匹配
- 3.扩展名匹配
- 4.任意匹配
- 4.URL配置注意事项
- 8.课后练习
- 1.练习一
- login.html
- CatServlet.java
- web.xml
- 结果
- 2.练习二
- login.html
- DogServlet.java
- web.xml
- 结果
- 3.练习三
- PigServlet.java
- 结果
1.Servlet整体框架
2.Servlet快速入门
1.创建项目配置基本环境
上个文件有详细介绍
2.添加jar包
1.在WEB-INF下创建目录lib,添加文件
2.添加到项目中
3.配置代码提示
3.src下创建文件
4.实现Servlet接口
package HelloServlet;import javax.servlet.*;
import java.io.IOException;/*** @author 孙显圣* @version 1.0*/
public class HelloServlet implements Servlet {/*** 当创建HelloServlet会调用,且只会调用一次* @param servletConfig* @throws ServletException*/@Overridepublic void init(ServletConfig servletConfig) throws ServletException {System.out.println("init()");}/*** 返回ServletConfig,也就是servlet的配置* @return*/@Overridepublic ServletConfig getServletConfig() {return null;}/*** 1.该方法处理浏览器的请求* 2.当浏览器每次请求一次servlet时,就会调用一次* 3.当tomcat调用该方法时,会把http请求的数据封装成实现ServletRequest接口的request对象* 4.通过servletRequest对象,可以得到用户提交的数据* 5.ServletResponsed对象可以用于返回数据给tomcat服务器* @param servletRequest* @param servletResponse* @throws ServletException* @throws IOException*/@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {System.out.println("hi, helloServlet");}/*** 返回servlet的信息,使用较少* @return*/@Overridepublic String getServletInfo() {return null;}/*** 1.该方法是servlet销毁时调用的* 2.只会调用一次*/@Overridepublic void destroy() {}
}
5.在web.xml配置HelloServlet
6.通过浏览器访问HelloServlet
3.浏览器访问Servlet的流程分析
一共有三个容器
url-name
name-全类名
name-实例
Servlet的生命周期
初始化阶段
处理浏览器请求阶段
终止阶段
4.GET和POST的分发处理
代码示例
@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {//GET和POST的分发处理//1.首先需要知道,ServletRequest的运行类型其实是它的子类HttpServletRequest的实现类,而getMethod方法是这个子接口的,所以需要将这个父接口转换为子接口的类型HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; //现在就是他的子接口,指向它的实现类,封装了请求的方法String method = httpServletRequest.getMethod();if (method.equals("GET")) {doGet();}else if (method.equals("POST")) {doPost();}}//编写doGet和doPost方法,用于分发处理public void doGet() {System.out.println("doGet被调用");}public void doPost() {System.out.println("doPost被调用");}
<%--Created by IntelliJ IDEA.User: 8615941515990Date: 2024/1/24Time: 16:38To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><head><title>$Title$</title></head><body><form action="http://localhost:8080/servlet/helloServlet" method="post">username: <input type="text" name="username"></form></body>
</html>
5.继承HttpServlet来开发Servlet
1.创建文件
2.配置web.xml
3.实现doGet和doPost方法并编写
package HelloServlet;import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;/*** @author 孙显圣* @version 1.0*/
public class HiServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("hi HiServlet");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);}
}
4.浏览器访问
5.过程解析
1.http://localhost:8080/servlet/hiServlet发送请求到Tomcat服务
2.Tomcat服务根据资源路径/hiServlet,找到HelloServlet.HiServlet
3.创建HiServlet的实例,并调用它的init()方法和service()方法
4.init()方法就是个空的,重点看service()方法,是它的父类HttpServlet的
6.debug方法
1.打断点
2.debug
3.输入网址
4.此时由于是get方式,所以程序会停在断点处
5.查看运行类型的确是HiServlet
6.Servlet注意事项
最简化的浏览器访问Servlet过程
- 浏览器向Tomcat服务发送请求
- Tomcat服务根据浏览器地址最后的路由/xxx去web.xml中找到对应的类并创建一个对象
- 调用init()方法和service()方法
- 第二次则直接调用service()方法
7.Servlet注解方式
1.快速入门
1.代码实例
package annotation;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;/*** @author 孙显圣* @version 1.0*/
//使用注解配置路由,一个servlet可以有多个路由,访问网址为http://localhost:8080/servlet/ok1 或者http://localhost:8080/servlet/ok2
@WebServlet(urlPatterns = {"/ok1", "/ok2"})
public class OkServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("注解的doGet");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("注解的doPost");}
}
2.结果
2.Servlet注解方式解密
1.与xml方式的区别
简要介绍
- 浏览器向Tomcat服务发送请求
- 服务扫描所有类的全路径并且通过反射获取注解对象,从而得到urlPatterns(路由)
- 将其与浏览器地址最后的/xxx比对,如果比对成功则调用init()方法和service()方法
- 第二次就直接调用service()方法
2.Servlet注解的参数说明
3.注解URL的4种匹配方式
1.精确匹配
2.目录匹配
3.扩展名匹配
4.任意匹配
4.URL配置注意事项
8.课后练习
1.练习一
login.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<form action="http://localhost:8080/servlet/cat" method="post">username:<input name="username" type="text">
</form>
</body>
</html>
CatServlet.java
package HomeWork;import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;/*** @author 孙显圣* @version 1.0*/
public class CatServlet implements Servlet {private int num = 0; //统计次数@Overridepublic void init(ServletConfig servletConfig) throws ServletException {}@Overridepublic ServletConfig getServletConfig() {return null;}@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {System.out.println("访问次数:" + (++num));//向下转型获取请求方式HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;String method = httpServletRequest.getMethod();if (method.equals("GET")) {System.out.println("GET");}else if (method.equals("POST")) {System.out.println("POST");}}@Overridepublic String getServletInfo() {return null;}@Overridepublic void destroy() {}
}
web.xml
结果
2.练习二
login.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<form action="http://localhost:8080/servlet/dog" method="post">username:<input name="username" type="text">
</form>
</body>
</html>
DogServlet.java
package HomeWork;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;/*** @author 孙显圣* @version 1.0*/
public class DogServlet extends HttpServlet {//统计次数private int get = 0;private int post = 0;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("GET:" + (++get));}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("POST:" + (++post));}
}
web.xml
结果
3.练习三
PigServlet.java
package HomeWork;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.net.InetAddress;/*** @author 孙显圣* @version 1.0*/
@WebServlet(urlPatterns = {"/pig1", "/pig2"})
public class PigServlet extends HttpServlet {//统计次数private int get = 0;private int post = 0;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("发出请求的浏览器的ip:" + req.getRemoteAddr());System.out.println("GET:" + (++get));}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("发出请求的浏览器的ip:" + req.getRemoteAddr());System.out.println("POST:" + (++post));}
}