当前位置:网站首页>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 (*^▽^*)!!!
边栏推荐
- Captcha killer verification code identification plug-in
- Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
- Chain team implementation (C language)
- ES全文索引
- Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
- New version of postman flows [introductory teaching chapter 01 send request]
- Windows platform mongodb database installation
- 链队实现(C语言)
- Always of SystemVerilog usage_ comb 、always_ iff
- 函数:用牛顿迭代法求方程的根
猜你喜欢
图书管理系统
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
JVM memory model concept
数字电路基础(五)算术运算电路
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
Network technology related topics
Quaternion -- basic concepts (Reprint)
Get started with Matplotlib drawing
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
5分钟掌握机器学习鸢尾花逻辑回归分类
随机推荐
数字电路基础(一)数制与码制
【指针】统计一字符串在另一个字符串中出现的次数
Feature extraction and detection 14 plane object recognition
图书管理系统
函数:计算字符串中大写字母的个数
Mathematical modeling idea of 2022 central China Cup
[pointer] octal to decimal
[pointer] the array is stored in reverse order and output
【指针】求解最后留下的人
线程的实现方式总结
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
《统计学》第八版贾俊平第三章课后习题及答案总结
[pointer] find the largest string
我的第一篇博客
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
Detailed explanation of network foundation
《统计学》第八版贾俊平第五章概率与概率分布
关于交换a和b的值的四种方法
Binary search tree concept
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class