这期主要分享Java Servlet技术的相关知识
Servlet是Java类编写的服务端应用程序,负责接收和响应客户端发送的请求
Servlet的生命周期有三个阶段:初始化(包括装载和初始化)、运行和销毁。
首先编写HelloServlet类继承HttpServlet
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("调用了doGet(HttpServletRequest req, HttpServletResponse resp)方法");
// 不应该调用父类的super.doGet(req, resp)方法,否则会出现HTTP 405错误
// super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost(HttpServletRequest req, HttpServletResponse resp)方法");
// 不应该调用父类的super.doPost(req, resp)方法,否则会出现HTTP 405错误
// super.doPost(req, resp);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("调用了protected void service(HttpServletRequest req, HttpServletResponse resp) 方法");
}
@Override
public void destroy() {
System.out.println("调用了destroy方法");
super.destroy();
}
@Override
public void init() throws ServletException {
System.out.println("调用了init方法");
super.init();
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("调用了 public void service 方法");
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.println("收到了service(ServletRequest req, ServletResponse res)请求");
super.service(req, res);
}
}
然后在web.xml中进行配置
<web-app version="2.5"
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_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>
jspconfigdemo
servlet-name>
<jsp-file>/config.jspjsp-file>
<init-param>
<param-name>urlparam-name>
<param-value>http://www.baidu.comparam-value>
init-param>
servlet>
<servlet>
<servlet-name>helloServletservlet-name>
<servlet-class>com.wen.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>
jspconfigdemo
servlet-name>
<url-pattern>/config.jspurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>helloServletservlet-name>
<url-pattern>/servlet1url-pattern>
servlet-mapping>
web-app>
重启Tomcat容器
在浏览器输入自定义Servlet类匹配的url
运行结果为:
idea控制台的打印结果为:
从运行的结果可知:当重写了service()方法后,doGet()方法和doPost()方法不会被执行,由service()方法管理转向对应的方法。
验证了Servlet的生命周期如下图所示:
在Servlet3.0之后可以使用注解的方式代替web.xml方式来配置Servlet
@WebServlet(
urlPatterns = {
"/servlet1"},
name = "helloServlet"
)
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("调用了doGet(HttpServletRequest req, HttpServletResponse resp)方法");
// 不应该调用父类的super.doGet(req, resp)方法,否则会出现HTTP 405错误
// super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost(HttpServletRequest req, HttpServletResponse resp)方法");
// 不应该调用父类的super.doPost(req, resp)方法,否则会出现HTTP 405错误
// super.doPost(req, resp);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("调用了protected void service(HttpServletRequest req, HttpServletResponse resp) 方法");
}
@Override
public void destroy() {
System.out.println("调用了destroy方法");
super.destroy();
}
@Override
public void init() throws ServletException {
System.out.println("调用了init方法");
super.init();
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("调用了 public void service 方法");
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.println("收到了service(ServletRequest req, ServletResponse res)请求");
super.service(req, res);
}
}
运行的结果是一样的。
@WebServlet注解主要属性说明
注意:在web.xml中的配置方式优先级要高于使用注解方式
在实际的开发中一般只会重写init()、doGet()、doPost()、destroy()这4个方法
package com.wen.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
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;
/**
* @author hw
* @version 1.0
* @date 2021/1/8 15:27
*/
@WebServlet(
urlPatterns = {
"/servlet1"},
name = "helloServlet"
)
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("调用了doGet(HttpServletRequest req, HttpServletResponse resp)方法");
String servletPath = req.getServletPath();
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("收到了"+servletPath+"请求");
// 不应该调用父类的super.doGet(req, resp)方法,否则会出现HTTP 405错误
// super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost(HttpServletRequest req, HttpServletResponse resp)方法");
doGet(req,resp);
// 不应该调用父类的super.doPost(req, resp)方法,否则会出现HTTP 405错误
// super.doPost(req, resp);
}
// @Override
// protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// System.out.println("调用了protected void service(HttpServletRequest req, HttpServletResponse resp) 方法");
// }
@Override
public void destroy() {
System.out.println("调用了destroy方法");
super.destroy();
}
@Override
public void init() throws ServletException {
System.out.println("调用了init方法");
super.init();
}
// @Override
// public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
// System.out.println("调用了 public void service 方法");
// res.setContentType("text/html;charset=utf-8");
// PrintWriter out = res.getWriter();
// out.println("收到了service(ServletRequest req, ServletResponse res)请求");
// super.service(req, res);
// }
}
运行的结果:
控制台的打印结果:
当关闭Tomcat web容器时:
Servlet容器会调用destroy()方法
主要区别:Servlet是java代码,jsp是页面代码
jsp主要以HTML为主,主要负责将动态数据的展示给客户
Servlet主要负责对客户端的请求进行处理和调用java bean,java bean将数据库中的数据进行映射,操作java bean就相当于操作数据库中数据,将处理好的结果返回给jsp,由jsp进行数据的动态展示。简易的MVC模式。