当前位置:网站首页>Analysis of the common methods and scopes of the three servlet containers
Analysis of the common methods and scopes of the three servlet containers
2022-08-09 16:17:00 【Ran959】
在JavaWeb中,ServletThe three scope objects arerequest,session,application,Its main purpose is to share data.
三大作用域的使用,其本质是根据作用域的范围,生命周期决定其使用的方式.:
| 对象名称 | 对象的类型 |
| request | HttpServletRequest |
| session | HttpSession |
| application | ServletContext |
How scoped objects share data:
1.设置作用域中的共享数据.
作用域对象名.setAttribute(String name,Object value);
2.获取作用域中的共享数据.
Object value=作用域对象名.getAttribute(String name);
3.获取作用域中的共享数据.
作用域对象.removeAttribute(String name);
注意:In which scope to set shared data,就只能从该作用域中取出数据.
三大作用域:
request:每一次请求都是一个新的request对象,如果在Web组件之间需要共享同一个请求中的数据,只能使用请求转发.
session:每一次会话都是一个新的session对象,如果需要在一次会话中的多个请求之间需要共享数据,只能使用session.
application:应用对象,Tomcat 启动到关闭,表示一个应用,在一个应用中有且只有一个applic-ation对象,作用于整个Web应用,可以实现多次会话之间的数据共享.
代码演示:
我们新建两个类:ScopeServlet ResultServlet
package com.ape.view;
import java.io.IOException;
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 ScopeServlet
*/
@WebServlet("/scope.do")
public class ScopeServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.request
Integer numInRequest=(Integer)req.getAttribute("NUM_IN_REQUEST") ;
if (numInRequest == null) {
req.setAttribute("NUM_IN_REQUEST",1);
}
else {
req.setAttribute("NUM_IN_REQUEST",numInRequest+1);
}
//2.Session
Integer numInSession=(Integer)req.getSession().getAttribute("NUM_IN_SESSION") ;
if (numInSession == null) {
req.getSession().setAttribute("NUM_IN_SESSION",1);
}
else {
req.getSession().setAttribute("NUM_IN_SESSION",numInSession+1);
}
//3.application
Integer numInApplication=(Integer) req.getServletContext().getAttribute("NUM_IN_APPLICATION");
if (numInApplication == null) {
req.getServletContext().setAttribute("NUM_IN_APPLICATION",1);
}
else {
req.getServletContext().setAttribute("NUM_IN_APPLICATION",numInApplication+1);
}
req.getRequestDispatcher("/result").forward(req,resp);
}
}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 ResultServlet
*/
@WebServlet("/result.do")
public class ResultServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
Integer numInRequest=(Integer) req.getAttribute("NUM_IN_REQUEST");
Integer numInSession=(Integer) req.getSession().getAttribute("NUM_IN_SESSION");
Integer numInApplication=(Integer) req.getServletContext().getAttribute("NUM_IN_APPLICATION");
out.println("Request="+numInRequest);
out.println("Session="+numInSession);
out.println("Application="+numInApplication);
}
}然后我们运行一下
第一次访问 The three scopes are1

刷新了5次 会看到 request一次 session与Application都是6次

Next we change the browser 会发现 session 是1了 而 Appplicationadded again

refreshed several times,发现session从0开始重新计数

Back to our firstGoogle浏览器 Google里面的sessionAnd add1了

因此 我们会发现
Request是一次请求
Session是一个会话
Applicationis multiple sessions(Tomcat开启到关闭)
常用方法:
1. Request:
request.setAttribute(); //向requestSetting data in the field
request.getAttribute(); //向requestExtract data from the field
request.removeAttribute(); //向requestremove data from domain
request.getParameter(). //从requestGet the passed data in the field
2.Session:
session.setAttribute(); //向sessionIn the domain store Shared data
session.getAttribute(); //从sessionExtract shared data from the domain
session.removeAttribute();//从sessionRemove Shared data in the field
3. Application(ServletContext):
1. 设置作用域中的共享数据
作用域对象.setAttribute(String name,Object value);
2. 获取作用域中的共享数据
Object value = 作用域对象.getAttribute(String name);
3. removes the specified shared data from the scope
作用域对象.removeAttribute(String name);
完事.
看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]
加油!
共同努力!
边栏推荐
猜你喜欢
随机推荐
运算符学习
正则化原理的简单分析(L1/L2正则化)
二维数组实现八皇后问题
多线程学习
Regular Expressions for Shell Programming
Seize the opportunity of quantitative trading fund products, and quantitative investment has room for development?
[Mysql]--Transaction, transaction isolation level, dirty read, non-repeatable read, phantom read analysis
FilenameFilter filters filenames
原子的核型结构及氢原子的波尔理论
英语议论文读写02 Engineering
常见的四种电阻之间有什么不同?
如何保证电脑硬盘格式化后数据不能被恢复?
MongoDB adds permission management
名词概念总结(不定期更新~~)
爱因斯坦的光子理论
思维导图FreeMind安装问题及简单使用
Suddenly want to analyze the mortgage interest rate and interest calculation
注释,标识符,数据类型
贝塞尔函数
FilenameFilter过滤文件名









