JavaWeb-Servlet方法/生命周期、HTTP/会话session
Servlet方法
package javax.servlet;
import java.io.IOException;
public interface Servlet {
//初始化方法
void init(ServletConfig var1) throws ServletException;
ServletConfig getServletConfig();
//服务方法
void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
String getServletInfo();
//销毁方法
void destroy();
}
服务方法
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的方式
String method = req.getMethod();
long lastModified;
//get请求
if (method.equals("GET")) {
lastModified = this.getLastModified(req);
if (lastModified == -1L) {
this.doGet(req, resp);
} else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader("If-Modified-Since");
} catch (IllegalArgumentException var9) {
ifModifiedSince = -1L;
}
if (ifModifiedSince < lastModified / 1000L * 1000L) {
this.maybeSetLastModified(resp, lastModified);
this.doGet(req, resp);
} else {
resp.setStatus(304);
}
}
} else if (method.equals("HEAD")) {
lastModified = this.getLastModified(req);
this.maybeSetLastModified(resp, lastModified);
this.doHead(req, resp);
} else if (method.equals("POST")) {
this.doPost(req, resp);
} else if (method.equals("PUT")) {
this.doPut(req, resp);
} else if (method.equals("DELETE")) {
this.doDelete(req, resp);
} else if (method.equals("OPTIONS")) {
this.doOptions(req, resp);
} else if (method.equals("TRACE")) {
this.doTrace(req, resp);
} else {
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[]{method};
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
doPost方法
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(405, msg);
} else {
resp.sendError(400, msg);
}
}
生命周期
init(),service(),destroy()
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Demo02Servlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println("loading...");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Service running...");
}
@Override
public void destroy() {
System.out.println("destroyed");
}
}
第一次请求
Servlet会进行实例化(调用构造方法)
初始化调用init
服务调用service
第二次请求
都是调用service
关闭Tomcat容器
调用destroy
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>Demo01Servlet</servlet-name>
<servlet-class>Demo01Servlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Demo02Servlet</servlet-name>
<servlet-class>Demo02Servlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Demo01Servlet</servlet-name>
<url-pattern>/demo01</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Demo02Servlet</servlet-name>
<url-pattern>/demo02</url-pattern>
</servlet-mapping>
</web-app>
<load-on-startup>0</load-on-startup>
数值越小启动越靠前,最小值0
HTTP
超文本传输协议,确定了请求和响应数据的格式。
请求报文的三个部分
请求头
请求消息头
名称 | 功能 |
---|---|
Host | 服务器的主机地址 |
Accept | 声明当前请求能够接受的『媒体类型』 |
Referer | 当前请求来源页面的地址 |
Content-Length | 请求体内容的长度 |
Content-Type | 请求体的内容类型,这一项的具体值是媒体类型中的某一种 |
Cookie | 浏览器访问服务器时携带的Cookie数据 |
请求体
get方式,没有请求体,有一个querystring
post方式,有请求体,form data
json格式,有请求体,request payload
响应报文的三个部分
响应行
Status Code: 200 OK
响应头
包含服务器发送给浏览器的信息
响应体
响应的实际内容
会话
会话跟踪技术
Http是无状态的
两次请求,服务器无法判断是否是同一个客户端发送的
为了解决此问题,需要使用会话跟踪技术
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class Demo03Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取session
HttpSession session = req.getSession();
System.out.println("Session ID:" + session.getId());
}
}
第一次向demo03发请求
第二次向demo03发请求
保存作用域
req.getSession();
//向当前session保存作用域保存一个数据“fancy”,对应的key为uname
session.setAttribute("uname","fancy");
同一会话,不同组件请求uname,都能得到fancy