当前位置:网站首页>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 (*^▽^*)!!!
边栏推荐
- [pointer] use the insertion sorting method to arrange n numbers from small to large
- 【指针】使用插入排序法将n个数从小到大进行排列
- Record an API interface SQL injection practice
- 链队实现(C语言)
- MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
- Intranet information collection of Intranet penetration (4)
- Function: calculates the number of uppercase letters in a string
- Keil5-MDK的格式化代码工具及添加快捷方式
- [pointer] delete all spaces in the string s
- Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
猜你喜欢
“人生若只如初见”——RISC-V
Based on authorized access, cross host, and permission allocation under sqlserver
JDBC transactions, batch processing, and connection pooling (super detailed)
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
Attack and defense world misc practice area (GIF lift table ext3)
Intranet information collection of Intranet penetration (2)
数字电路基础(二)逻辑代数
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
Captcha killer verification code identification plug-in
随机推荐
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
Proceedingjoinpoint API use
Numpy快速上手指南
【指针】使用插入排序法将n个数从小到大进行排列
【指针】数组逆序重新存放后并输出
数字电路基础(一)数制与码制
函数:字符串反序存放
刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Record an API interface SQL injection practice
指針:最大值、最小值和平均值
指针:最大值、最小值和平均值
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
图书管理系统
关于交换a和b的值的四种方法
《统计学》第八版贾俊平第四章总结及课后习题答案
Hcip -- MPLS experiment
[pointer] the array is stored in reverse order and output
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)