javascript
JSP之四大作用域(pageContext,request,session,application)
JSP的四大作用域的存儲和獲取數據的方式一樣,差別在于取值的范圍不同。
四大域各自作用范圍為:
pageContext:當前JSP頁面有效
request:請求有效
session:會話有效(關閉瀏覽器則失效)
application:整個Web應用有效(服務器關閉或者重啟則失效)
1、pageContext作用域
(pageContextScope:鍵值,設值:李四)
由于輸出腳本中的a是字符類型,所以需要對pageContext.getAttribute進行強制類型轉換String a = (String)來解決輸出腳本a的報錯
jsp代碼:創建一個jsp頁面,在body標簽里面書寫以下代碼
<%pageContext.setAttribute("pageContextScope", "李四");String a = (String) pageContext.getAttribute("pageContextScope"); %>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>輸出結果:
?2、request作用域
創建一個servlet(ScopeServlet.java),在servlet里存值
轉發操作(可以獲取到值)
getRequestDispatcher:轉發到頁面;
forward:傳遞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("轉發路徑"),我的轉發jsp頁面位于:webapp下的jsp/object文件夾下的scope.jsp
jsp頁面代碼:
<h4>request域</h4> <%String b = (String) request.getAttribute("requestScope"); %> requestScope:<%=b%>注:由于Servlet發生變化,需要重新部署項目;
由于request是在servlet上存值,所以需要在ScopeServlet上啟動;
輸出結果:
?重定向操作(獲取不到值)
?Servlet代碼:
req.setAttribute("requestScope","李二"); resp.sendRedirect(req.getContextPath()+"/jsp/object/scope.jsp");jsp代碼:
<h4>request域</h4> <%String b = (String) request.getAttribute("requestScope"); %> requestScope:<%=b%>輸出結果:
注:觀察兩者的區別
轉發操作:地址欄沒有發生變化
重定向操作:地址欄發生變化
3、session作用域
servlet代碼:
req.getSession().setAttribute("sessionScope", "李三");jsp代碼:
<h4>session域</h4><%String c = (String)session.getAttribute("sessionScope");%>session:<%=c %>輸出結果:
?4、application作用域
servlet代碼:
注:servlet這里的作用域不是application而是servletContextScope,因為application對應的servlet類型是javax.servlet.servletContext
req.getServletContext().setAttribute("servletContextScope", "李四");jsp代碼:
注:
<h4>application域</h4><%String d = (String) application.getAttribute("servletContextScope");%>application:<%=d %>輸出結果:
?完整代碼:
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{@Overrideprotected 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", "李四");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);}}jsp代碼:
<%@ 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>復制地址欄的地址,關閉所有開啟的瀏覽器頁面,重新打開瀏覽器,粘貼地址回車,你會發現,session域也取不到值
僅供參考,如有不足之處,敬請見諒。
總結
以上是生活随笔為你收集整理的JSP之四大作用域(pageContext,request,session,application)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 狂神GUI贪吃蛇免费图片资源加代码(其中
- 下一篇: 腾讯位置服务JSAPI GL实现文本标记