The four major scopes of JSP (pageContext, request, session, application)

The storage and data acquisition methods of the four major scopes of JSP are the same, the difference lies in the range of values.

The scope of action of the four domains is as follows:

pageContext: the current JSP page is valid

request: The request is valid

session: The session is valid (it will be invalid when the browser is closed)

application: The entire web application is valid (if the server is shut down or restarted, it will be invalid)

1. pageContext scope
(pageContextScope: key value, set value: Li Si)

Since a in the output script is a character type, it is necessary to cast String a = (String) to pageContext.getAttribute to solve the error of the output script a

jsp code: Create a jsp page and write the following code in the body tag

<%
	pageContext.setAttribute("pageContextScope", "李四");
	String a = (String) pageContext.getAttribute("pageContextScope");
%>

The complete code of the servlet:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pageConext作用域</title>
</head>
<body>
	<%
	pageContext.setAttribute("pageContextScope", "李四");
	String a = (String) pageContext.getAttribute("pageContextScope");
	%>

	<h2>
		pageContextScope:<%=a%>
	</h2>
</body>
</html>

Output result:

 2. Request scope

Create a servlet (ScopeServlet.java) and store the value in the servlet

Forwarding operation (values ​​can be obtained)

getRequestDispatcher: forward to the page;

forward: pass reqest, response

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("requestScope","李二");
		req.getRequestDispatcher("/jsp/object/scope.jsp").forward(req, resp);
		
	}

req.getRequestDispatcher("forwarding path"), my forwarding jsp page is located at: scope.jsp under the jsp/object folder under webapp

jsp page code:

<h4>request域</h4>
<%
    String b = (String) request.getAttribute("requestScope");
%>
requestScope:<%=b%>

Note: Due to changes in the Servlet, the project needs to be redeployed;

Since the request is stored on the servlet, it needs to be started on the ScopeServlet;

Output result:

 Redirect operation (cannot get value)

 Servlet code:

req.setAttribute("requestScope","李二");
resp.sendRedirect(req.getContextPath()+"/jsp/object/scope.jsp");

jsp code:

<h4>request域</h4>
<%
	String b = (String) request.getAttribute("requestScope");
%>
requestScope:<%=b%>

Output result:

Note: Observe the difference between the two

Forward action: No change in the address bar

Redirect action: address bar changes

3. Session scope

servlet code:

req.getSession().setAttribute("sessionScope", "李三");

jsp code:

<h4>session域</h4>
	<%
	String c = (String)session.getAttribute("sessionScope");
	%>
	session:<%=c %>

Output result:

 4. Application scope

servlet code:

Note: The scope of the servlet here is not application but servletContextScope, because the servlet type corresponding to application is javax.servlet.servletContext

req.getServletContext().setAttribute("servletContextScope", "李四");

jsp code:

Note:

<h4>application域</h4>
	<%
	String d = (String) application.getAttribute("servletContextScope");
	%>
	application:<%=d %>

Output result:

 Full code:

servlet:

package com.servlet;

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;

@WebServlet(name="ScopeServlet",value="/scopeServlet")
public class ScopeServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("requestScope","李二");
		//req.getRequestDispatcher("/jsp/object/scope.jsp").forward(req, resp);
        
        //转发
		//req.getRequestDispatcher("/jsp/object/scope.jsp").include(req, resp);
		
		//重定向
		resp.sendRedirect(req.getContextPath()+"/jsp/object/scope.jsp");
		 
		req.getSession().setAttribute("sessionScope", "李三");
		
		req.getServletContext().setAttribute("servletContextScope", "李四");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doPost(req, resp);
	}
	
}

jsp code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pageConext\request\session\application四大作用域对象</title>
</head>
<body>
	<h4>pageContext域</h4>
	<%
	pageContext.setAttribute("pageContextScope", "李一");
	String a = (String) pageContext.getAttribute("pageContextScope");
	%> 
	pageContextScope:<%=a%>

	<h4>request域</h4>
	<%
	String b = (String) request.getAttribute("requestScope");
	%>
	requestScope:<%=b%>

	<h4>session域</h4>
	<%
	String c = (String) session.getAttribute("sessionScope");
	%>
	session:<%=c%>
	<h4>application域</h4>
	<%
	String d = (String) application.getAttribute("servletContextScope");
	%>
	application:<%=d %>
</body>
</html>

Copy the address in the address bar, close all open browser pages, reopen the browser, paste the address and press Enter, you will find that the session field cannot get the value

For reference only, please forgive me if there are any deficiencies.

Guess you like

Origin blog.csdn.net/qq_53376718/article/details/127472942