一、请求(request scope)作用域
定义:表示数据再一次http请求和响应期间有效,从客户端发送请求到服务器响应请求的整个过程(1次请求中数据是有效的
)
生命周期:当一次请求处理完成,即浏览器得到响应了,请求作用域中的数据就会被销毁
适用场景:请求转发,表单提交—》
在request中存放数据(request.setAttribute(“key”,value)
JSP取数据:EL表达式(${requestScope.key})
二、会话(session scope)作用域
定义:表示用户在会话开始的时候创建,在用户关闭浏览器或者会话过期后销毁,一次请求结束之后,跳转到其他界面,只要对应的浏览器没有关闭,session域就不会关闭,就还是可以从中取数据
生命周期:在用户的会话期间,多个请求可以共享会话作用域中的数据
适用场景:用户登录状态,购物车信息等
获取session:request.getSession();
往session中存数据:session.setAttribute(“属性名”,“属性值”)
JSP取出数据:${sessionScope.属性名}
用于读取判断登录校验:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Titletitle>
head>
<body>
<div>
<h1 style="color: red;">${login_error}h1>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="username" placeholder="请输入用户名">
<br>
密码:<input type="password" name="password" placeholder="请输入密码">
<br>
验证码:<input type="text" name="captcha" placeholder="请输入验证码">
<%--TODO:这个地方使用'?time='+new Date().getTime()是为了每一次请求都是不一样的--%>
<img src="${pageContext.request.contextPath}/captchacode" alt="图片验证码" width="40" height="30" onclick="this.src='${pageContext.request.contextPath}/captchacode?time='+new Date().getTime()">
input>
<br>
<input type="submit" value="登录">
form>
div>
body>
html>
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取输入框中的验证码
String captcha_code = request.getParameter("captcha");
//获取session中存储的验证码
HttpSession session = request.getSession();
String session_code = (String) session.getAttribute("captcha_code");
if (session_code.equalsIgnoreCase(captcha_code)){
//验证码正确
//获取用户名,密码
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("admin") && password.equals("123456")){
//登录成功
//重定向跳转到登录成功页面
response.sendRedirect(request.getContextPath()+"/index.jsp");
}else {
//登录失败
//重定向跳转到登录页面
session.setAttribute("login_error","用户名或密码错误");
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}else {
//验证码错误
//重定向跳转到登录页面
session.setAttribute("login_error","验证码错误");
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
//TODO:生成图片验证码并响应给前端
@WebServlet("/captchacode")
public class CaptchaServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO:获取图片验证码的对象
LineCaptcha lineCaptcha=new LineCaptcha(200,100,4,6);
//获取图片验证码
String code = lineCaptcha.getCode();
//获取session对象
HttpSession session = request.getSession();
//将图片验证码存入session中
session.setAttribute("captcha_code",code);
//设置响应的类型
response.setContentType("image/png");
//将图片验证码输出到客户端
lineCaptcha.write(response.getOutputStream());
response.getOutputStream().close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
三、应用程序(application)作用域
定义:应用程序作用域是最大范围的作用域,表示数据在整个Web应用程序中有效,因此只要服务器不关闭,服务器上的工程不移除,不重新其中,application对象就一直存在(即Tomcat服务器不关闭的话,数据就是一直都有效的)
生命周期:数据存储在ServletContext对象中,在Web应用程序启动的时候创建,在应用程序关闭的时候销毁
适用场景:用于在整个Web应用程序中共享全局数据,比如配置信息,数据库连接等
通过application对象获取页面访问量(PV)——不论从哪里访问的都可以记录其浏览量
通过ApplicationContext
@WebServlet("/pageVisit")
public class PVCountServlet extends HttpServlet {
private final static String PV_COUNT = "pv";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取application
ServletContext servletContext = request.getServletContext();
//获取pv--页面访问量
Object count = servletContext.getAttribute(PV_COUNT);
if (count == null){
//第一次访问
servletContext.setAttribute(PV_COUNT,1);
}else {
//不是第一次访问
int count1 = (int) count;
servletContext.setAttribute(PV_COUNT,count1+1);
}
System.out.println("当前的页面访问量为:"+ servletContext.getAttribute(PV_COUNT));
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
四、页面(page scope)作用域
这个表示的是只在一个JSP页面中有效
pageContext:当前页面
request:一次请求
session:一次会话
application:当前应用
作用域从小到大为:
pageContext < request < session < application
优先级
pageContext > request > session > application
作用域生效比较
@WebServlet("/servlet1")
public class MyServlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//在各作用域中各自放一个同名属性
//request作用域
request.setAttribute("name","request");
//session作用域
HttpSession session = request.getSession();
session.setAttribute("name","session");
//application作用域
ServletContext application = request.getServletContext();
application.setAttribute("name","application");
//请求转发-----》到test1.jsp页面
//request.getRequestDispatcher("/test1.jsp").forward(request,response);
//重定向的形式(两次请求--》第一次的request销毁了)
response.sendRedirect(request.getContextPath()+"/test1.jsp");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
test1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Titletitle>
head>
<body>
<h1>${name}h1>
body>
html>