当前位置:网站首页>The common methods of servlet context, session and request objects and the scope of storing data in servlet.
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
2022-07-06 14:38:00 【Floating~】
Tips :context,session,request, As Servlet Important means of value transmission between , The distinction between their scopes , The key and difficult points of our study .
Catalog
One 、Servlet Three data storage objects
ServletContext:( From server startup to server shutdown .)
HttpServletRequest: One request .
Preface : In this article, we will start with the basic concepts of the three , Start with the advantages and disadvantages of the three , Then analyze the similarities and differences of their scopes .
One 、Servlet Three data storage objects
①ServletContext
WEB Container at startup , It will WEB The application creates a corresponding ServletContext object , It represents the current web application .ServletConfig Maintained in object ServletContext References to objects , Developers are writing servlet when , Can pass config.getServletContext() Methods to get ServletContext object .
②HttpSession
Created on the server side , Save on server , Maintain on the server side , Every time you create a new Session, The server will assign a unique ID, And put this ID Save to client Cookie in , The form of preservation is JSESSIONID To save the .
③HttpServletRequest
HttpServletRequest Object represents the client's request , When the client passes HTTP When the protocol accesses the server ,HTTP All the information in the request header is encapsulated in this object , Through the methods provided by this object , You can get all the information requested by the client .
Three public methods (API)
Store the data :setAttribute(name,value);
get data :getAttribute(name);
Delete data : removeAttribute(name);
Two 、 Scope
ServletContext:( From server startup to server shutdown .)
Test01
package com.ape.view;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
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 org.thymeleaf.context.WebContext;
/**
* Servlet implementation class Test01
*/
@WebServlet("/Test01")
public class Test01 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. Deal with the mess
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
// Access to resources
PrintWriter out = response.getWriter();
ServletContext context = getServletContext();
//request Domain
String name = " Pig eight quit ";
context.setAttribute("name", name);
// Print to page
out.print(" page 1"+context.getAttribute("name"));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Test02
package com.ape.view;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Test02
*/
@WebServlet("/Test02")
public class Test02 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. Deal with the mess
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
// Access to resources
PrintWriter out = response.getWriter();
ServletContext context = getServletContext();
//request Domain
// Print to page
out.print(" page 2"+context.getAttribute("name"));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Running results :
At present edge Browsers can request


Another Firefox browser can also request

HttpSession: One session , When session End on Destruction ( The default is short session , To persist a session, you need to set the maximum lifetime session.setMaxInactiveInterval( Number of seconds );).
Test01
package com.ape.view;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;
import org.thymeleaf.context.WebContext;
/**
* Servlet implementation class Test01
*/
@WebServlet("/Test01")
public class Test01 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. Deal with the mess
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
// Access to resources
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
//request Domain
String name = " The sand monk ";
session.setAttribute("name", name);
// Print to page
out.print(" page 1"+session.getAttribute("name"));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Test02
package com.ape.view;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;
/**
* Servlet implementation class Test02
*/
@WebServlet("/Test02")
public class Test02 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. Deal with the mess
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
// Access to resources
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
//request Domain
// Print to page
out.print(" page 2"+session.getAttribute("name"));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Running results :
At present edge Browsers can request

Another Firefox browser cannot be requested

HttpServletRequest: One request .
Test01
package com.ape.view;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Test01
*/
@WebServlet("/Test01")
public class Test01 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. Deal with the mess
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
// Access to resources
PrintWriter out = response.getWriter();
//request Domain
String name = " The Monkey King ";
request.setAttribute("name", name);
// Print to page
out.print(" page 1"+request.getAttribute("name"));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Test02
package com.ape.view;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Test02
*/
@WebServlet("/Test02")
public class Test02 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. Deal with the mess
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
// Access to resources
PrintWriter out = response.getWriter();
//request Domain
// Print to page
out.print(" page 2"+request.getAttribute("name"));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Running results :

After forwarding : ( The address bar doesn't change , but request The request is complete )
3、 ... and 、 Use scenarios
Request Domain : Related to the current operation , You can forward the value to other pages .
Session Domain : It's a reply , It can only be used in the current browser , It is applicable to those related to user information , For example, save login information .
application Domain : Public , Can be called during the entire project run , Store global information , Some important information is not recommended .
Finally, everyone :
The new week is full of vitality (*^▽^*)!!!
边栏推荐
- 移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
- Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
- Overview of LNMP architecture and construction of related services
- C language learning summary (I) (under update)
- Interview Essentials: what is the mysterious framework asking?
- [pointer] find the length of the string
- How does SQLite count the data that meets another condition under the data that has been classified once
- Intranet information collection of Intranet penetration (I)
- {1,2,3,2,5}查重问题
- Intranet information collection of Intranet penetration (5)
猜你喜欢

Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?

ES全文索引

《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案

Solutions to common problems in database development such as MySQL

captcha-killer验证码识别插件

Intranet information collection of Intranet penetration (3)

High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters

数据库多表链接的查询方式

Web vulnerability - File Inclusion Vulnerability of file operation

Matplotlib绘图快速入门
随机推荐
Always of SystemVerilog usage_ comb 、always_ iff
5 minutes to master machine learning iris logical regression classification
Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
【指针】删除字符串s中的所有空格
Intranet information collection of Intranet penetration (3)
1.支付系统
JVM memory model concept
Function: calculates the number of uppercase letters in a string
Based on authorized access, cross host, and permission allocation under sqlserver
数字电路基础(四) 数据分配器、数据选择器和数值比较器
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
Pointers: maximum, minimum, and average
flask实现强制登陆
关于超星脚本出现乱码问题
Constants, variables, and operators of SystemVerilog usage
Overview of LNMP architecture and construction of related services
Pointer -- eliminate all numbers in the string
[pointer] solve the last person left