servlet
1.Servlet技术
a)什么是Servlet
| 1、Servlet是JavaEE规范之一。规范就是接口 |
| 2、Servlet就JavaWeb三大组件之一。三大组件分别是:Servlet程序、Filter过滤器、Listener监听器。 |
| 3、Servlet是运行在服务器上的一个小型java程序,它可以接收客户端发送过来的请求,并响应数据给客户端。 |
| |
b)手动实现Servlet程序
| 1、编写一个类去实现Servlet接口 |
| 2、实现service方法,处理请求,并响应数据 |
| 3、到web.xml中去配置servlet程序的访问地址 |
Servlet程序的示例代码:
| |
| public class HelloServlet implements Servlet{ |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Override |
| public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { |
| System.out.println("Hello Servlet 被执行了"); |
| } |
| } |
web.xml中的配置:
| <?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_3_1.xsd" |
| version="3.1"> |
| |
| |
| <servlet> |
| |
| <servlet-name>HelloServlet</servlet-name> |
| |
| |
| <servlet-class>com.stt.servlet.HelloServlet</servlet-class> |
| </servlet> |
| |
| |
| <servlet-mapping> |
| |
| <servlet-name>HelloServlet</servlet-name> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <url-pattern>/hello</url-pattern> |
| </servlet-mapping> |
| |
| </web-app> |
常见的错误1:url-pattern中配置的路径没有以斜杠打头。

常见错误2:servlet-name配置的值不存在:

常见错误3:servlet-class标签的全类名配置错误:

c)url地址到Servlet程序的访问

d)Servlet的生命周期
| 1、执行Servlet构造器方法 |
| 2、执行init初始化方法 |
| 第一、二步,是在第一次访问的时候创建Servlet程序会调用。 |
| |
| 3、执行service方法 |
| 第三步,每次访问都会调用。 |
| |
| 4、执行destroy销毁方法 |
| 第四步,在web工程停止的时候调用。 |
e)GET和POST请求的分发处理
| @Override |
| public void service(ServletRequest servletRequest,ServletResponse servletResponse)throws |
| ServletException,IOException{ |
| System.out.println("3service===HelloServlet被访问了"); |
| |
| |
| |
| HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest; |
| |
| String method = httpServletRequest.getMethod(); |
| if("GET".equals(method)){ |
| doGet(); |
| }elseif("POST".equals(method)){ |
| doPost(); |
| } |
| } |
| |
| |
| |
| public void doGet(){ |
| System.out.println("get请求"); |
| } |
| |
| |
| |
| public void doPost(){ |
| System.out.println("post请求"); |
| } |
f)通过继承HttpServlet实现Servlet程序
| 一般在实际项目开发中,都是使用继承HttpServlet类的方式去实现Servlet程序。 |
| 1、编写一个类去继承HttpServlet 类 |
| 2、根据业务需要重写doGet或doPost方法 |
| 3、到web.xml中的配置Servlet程序的访问地址 |
Servlet类的代码:
| public class HelloServlet2 extends HttpServlet { |
| |
| |
| |
| |
| |
| |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| System.out.println("HelloServlet2的doGet方法"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| System.out.println("HelloServlet2的doPost方法"); |
| } |
| } |
web.xml中的配置:
| <servlet> |
| <servlet-name>HelloServlet2</servlet-name> |
| <servlet-class>com.atguigu.servlet.HelloServlet2</servlet-class> |
| </servlet> |
| <servlet-mapping> |
| <servlet-name>HelloServlet2</servlet-name> |
| <url-pattern>/hello2</url-pattern> |
| </servlet-mapping> |
hello.html
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>Hello</title> |
| </head> |
| <body> |
| <form action="http://localhost:8080/hello2" method="get"> |
| <input type="submit"> |
| </form> |
| </body> |
| </html> |

| 当客户端点击hello.html中的提交按钮,根据不同的请求方式,调用HelloServlet2类中的不同的方法 |

g)使用IDEA创建Servlet程序
菜单:new->Servlet程序

配置Servlet的信息:

h)Servlet类的继承体系

2.ServletConfig类
| 1.ServletConfig类从类名上来看,就知道是Servlet程序的配置信息类。 |
| |
| 2.Servlet程序和ServletConfig对象都是由Tomcat负责创建,我们负责使用。 |
| |
| 3.Servlet程序默认是第一次访问的时候创建,ServletConfig是每个Servlet程序创建时,就创建一个对应的ServletConfig对象。 |
a)ServletConfig类的三大作用
| 1、可以获取Servlet程序的别名servlet-name的值 |
| 2、获取初始化参数init-param |
| 3、获取ServletContext对象 |
web.xml中的配置:
| <servlet> |
| <servlet-name>HelloServlet</servlet-name> |
| <servlet-class>com.stt.servlet.HelloServlet</servlet-class> |
| |
| |
| <init-param> |
| |
| <param-name>username</param-name> |
| |
| <param-value>root</param-value> |
| </init-param> |
| |
| |
| <init-param> |
| |
| <param-name>password</param-name> |
| |
| <param-value>root</param-value> |
| </init-param> |
| </servlet> |
| |
| <servlet-mapping> |
| <servlet-name>HelloServlet</servlet-name> |
| <url-pattern>/hello</url-pattern> |
| </servlet-mapping> |
Servlet中的代码:
| public class HelloServlet implements Servlet { |
| @Override |
| public void init(ServletConfig servletConfig) throws ServletException { |
| |
| System.out.println("HelloServlet程序的别名是:" + |
| servletConfig.getServletName()); |
| |
| |
| System.out.println("初始化参数username的值是:" + |
| servletConfig.getInitParameter("username")); |
| System.out.println("初始化参数password的值是:" + |
| servletConfig.getInitParameter("password")); |
| |
| |
| System.out.println("ServletContext对象" + |
| servletConfig.getServletContext()); |
| } |
| } |

| 每一个servlet程序的init方法内通过servletConfig对象只能获取自己servlet程序内的值,比如:HelloSerlvet2类的init方法内不能获取HelloSerlvet的init的参数,若定义HelloSerlvet2的init参数则可以获取。 |
注意点:
自定义类继承HttpServlet类重写 init() 方法后,方法内要调用父类的 init()方法。因为ServletConfig是在父类中定义声明,父类会对ServletConfig对象进行保存操作,重写后子类中若没有,则父类中保存config对象会丢失,所以会报错。
HttpServlet的父类GenericServlet:
| public abstract class GenericServlet implements Servlet, ServletConfig, Serializable { |
| private transient ServletConfig config; |
| |
| public void init(ServletConfig config) throws ServletException { |
| this.config = config; |
| this.init(); |
| } |
| } |

3.ServletContext类
a)什么是ServletContext?
| 1、ServletContext是一个接口,它表示Servlet上下文对象 |
| 2、一个web工程,只有一个ServletContext对象实例。 |
| 3、ServletContext对象是一个域对象。 |
| 4、ServletContext是在web工程部署启动的时候创建。在web工程停止的时候销毁。 |
| |
| 什么是域对象? |
| 域对象,是可以像Map一样存取数据的对象,叫域对象。 |
| 这里的域指的是存取数据的操作范围,整个web工程。 |
|
存数据 |
取数据 |
删除数据 |
Map |
put() |
get() |
remove() |
域对象 |
setAttribute() |
getAttribute() |
removeAttribute() |
b)ServletContext类的四个作用
| 1、获取web.xml中配置的上下文参数context-param |
| 2、获取当前的工程路径,格式:/工程路径 |
| 3、获取工程部署后在服务器硬盘上的绝对路径 |
| 4、像Map一样存取数据 |
ServletContext演示代码:
| @Override |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| |
| javax.servlet.ServletContext servletContext = getServletConfig().getServletContext(); |
| String username = servletContext.getInitParameter("username"); |
| System.out.println("context-param参数username的值是:" + username); |
| System.out.println("context-param参数password的值是:" + |
| servletContext.getInitParameter("password")); |
| |
| |
| String contextPath = servletContext.getContextPath(); |
| System.out.println("当前工程路径:" + contextPath); |
| |
| |
| |
| |
| |
| System.out.println("工程部署的路径:" + servletContext.getRealPath("/")); |
| } |
web.xml中的配置:
| |
| <context-param> |
| <param-name>username</param-name> |
| <param-value>root</param-value> |
| </context-param> |
| <context-param> |
| <param-name>password</param-name> |
| <param-value>123456</param-value> |
| </context-param> |
ServletContext像Map一样存取数据:
ContextServlet1代码:
| @Override |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| |
| ServletContext context = getServletContext(); |
| System.out.println(context); |
| |
| |
| System.out.println("保存之前:Context1获取key1的值是:" + |
| context.getAttribute("username")); |
| |
| |
| context.setAttribute("username", "zhangsan"); |
| |
| |
| System.out.println("Context1中获取域数据key1的值是:" + |
| context.getAttribute("username")); |
| } |
ContextServlet2代码:
| @Override |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| ServletContext context = getServletContext(); |
| |
| System.out.println("Context2中获取域数据key1的值是:" + |
| context.getAttribute("username")); |
| } |

4.HTTP协议
a)什么是HTTP协议
| 什么是协议? |
| 协议是指双方,或多方,相互约定好,大家都需要遵守的规则,叫协议。 |
| |
| 所谓HTTP协议,就是指,客户端和服务器之间通信时,发送的数据,需要遵守的规则,叫HTTP协议。 |
| HTTP协议中的数据又叫报文。 |
b)请求的HTTP协议格式
| 客户端给服务器发送数据叫请求。 |
| 服务器给客户端回传数据叫响应。 |
| |
| 请求又分为GET请求,和POST请求两种 |
GET请求
| 1、请求行 |
| (1) 请求的方式 GET |
| (2) 请求的资源路径 [+ ? +请求参数](中括号内可选的,加? 加请求参数) |
| (3) 请求的协议的版本号 HTTP/1.1 |
| |
| 2、请求头 |
| key:value 组成 不同的键值对,表示不同的含义。 |

POST请求
| 1、请求行 |
| (1) 请求的方式 POST |
| (2) 请求的资源路径 [+ ? +请求参数] (中括号内可选的,加? 加请求参数) |
| (3) 请求的协议的版本号 HTTP/1.1 |
| |
| 2、请求头 |
| 1) key:value 不同的请求头,有不同的含义 |
| |
| 空行 (请求头与请求体之间有空行隔开) |
| |
| 3、请求体===>>> 就是发送给服务器的数据 |

常用请求头的说明
| Accept: 表示客户端可以接收的数据类型 |
| Accpet-Languege: 表示客户端可以接收的语言类型 |
| User-Agent: 表示客户端浏览器的信息 |
| Host:表示请求时的服务器ip和端口号 |
哪些是GET请求,哪些是POST请求
| GET请求有哪些: |
| 1、form标签method=get |
| 2、a标签 |
| 3、link标签引入css |
| 4、Script标签引入js文件 |
| 5、img标签引入图片 |
| 6、iframe引入html页面 |
| 7、在浏览器地址栏中输入地址后敲回车 |
| |
| POST请求有哪些: |
| 8、form标签method=post |
c)响应的HTTP协议格式
| 1、响应行 |
| (1) 响应的协议和版本号 |
| (2) 响应状态码 |
| (3) 响应状态描述符 |
| |
| 2、响应头 |
| (1) key:value 不同的响应头,有其不同含义 |
| |
| 空行 (请求头与请求体之间有空行隔开) |
| |
| 3、响应体 ---->>> 就是回传给客户端的数据 |

d)常用的响应码说明
| 200 表示请求成功 |
| 302 表示请求重定向 |
| 404 表示请求服务器已经收到了,但是你要的数据不存在(请求地址错误) |
| 500 表示服务器已经收到请求,但是服务器内部错误(代码错误) |
e)MIME类型说明
| MIME是HTTP协议中数据类型。 |
| MIME的英文全称是"Multipurpose Internet Mail Extensions" 多功能Internet 邮件扩充服务。 |
| MIME类型的格式是“大类型/小类型”,并与某一种文件的扩展名相对应。 |
常见的MIME类型:
文件 |
原始类型 |
MIME类型 |
超文本标记语言文本 |
.html,.htm |
text/html |
普通文本 |
.txt |
text/plain |
RTF文本 |
.rtf |
application/rtf |
GIF图形 |
.gif |
image/gif |
JPEG图形 |
.jpeg,.jpg |
image/jpeg |
au声音文件 |
.au |
audio/basic |
MIDI音乐文件 |
mid,.midi |
audio/midi,audio/x-midi |
RealAudio音乐文件 |
.ra,.ram |
audio/x-pn-realaudio |
MPEG文件 |
.mpg,.mpeg |
video/mpeg |
AVI文件 |
.avi |
video/x-msvideo |
GZIP文件 |
.gz |
application/x-gzip |
TAR文件 |
.tar |
application/x-tar |
谷歌浏览器如何查看HTTP协议:

火狐浏览器如何查看HTTP协议:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步