zoukankan      html  css  js  c++  java
  • javaweb之jsp的属性范围

    1.什么是jsp的属性范围?

    所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以保存并继续使用。jsp提供了四种属性范围,如下:

    • 当前页,对应的jsp对象为pageContext,属性的作用范围仅限于当前JSP页面,跳转到其他页面无法取得。
    • 一次服务请求,对应的jsp对象为request,属性的作用范围仅限于同一个请求。
    • 一次会话(浏览器打开到关闭为一次会话),对应的jsp对象为session,一个用户设置的内容,只要是与此用户相关的页面都可以访问。
    • 上下文,对应的jsp对象为application,属性的作用范围限于当前web应用,是范围最大的属性作用范围,只要在一处设置属性,在其他各处的JSP或Servlet中都可以获取到。

    2.属性的操作方法

     public void setAttribute(String name,Object value)    设置属性

     public object getAttribute(String name)    取得属性

     public void removeAttribute(String name)    删除属性

    示例代码如下:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <title>My JSP 'attrTest1.jsp' starting page</title>
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
      </head>
      <body>
        <%
    	pageContext.setAttribute("pageContextAttr", "pageContextValue");
        request.setAttribute("requestAttr", "requestValue");
        session.setAttribute("sessionAttr", "sessionValue");
        application.setAttribute("applicationAttr", "applicationValue");
        %>
        <br><br>
        pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>
        <br><br>
        request: <%=request.getAttribute("requestAttr") %>
        <br><br>
        session: <%=session.getAttribute("sessionAttr") %>
        <br><br>
        application: <%=application.getAttribute("applicationAttr") %>
      </body>
    </html>
    

     运行后输出结果如下:

    3.四种属性范围的具体介绍

    示例代码如下:

    attrTest1.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <title>My JSP 'attrTest1.jsp' starting page</title>
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
      </head>
      <body>
        <%
    	pageContext.setAttribute("pageContextAttr", "pageContextValue");
        request.setAttribute("requestAttr", "requestValue");
        session.setAttribute("sessionAttr", "sessionValue");
        application.setAttribute("applicationAttr", "applicationValue");
        %>
        <h2>attrTest1 page</h2>
        <br>
        pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>
        <br><br>
        request: <%=request.getAttribute("requestAttr") %>
        <br><br>
        session: <%=session.getAttribute("sessionAttr") %>
        <br><br>
        application: <%=application.getAttribute("applicationAttr") %>
        <br><br>
        <a href="jspTest/attrTest2.jsp">To attrTest2 Page</a>
      </body>
    </html>
    

    attrTest2.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <title>My JSP 'attrTest2.jsp' starting page</title>
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
      </head>
      <body>
      	<h2>attrTest2 page</h2>
        <br>
        pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>
        <br><br>
        request: <%=request.getAttribute("requestAttr") %>
        <br><br>
        session: <%=session.getAttribute("sessionAttr") %>
        <br><br>
        application: <%=application.getAttribute("applicationAttr") %>
        <br><br>
      </body>
    </html>
    

    运行后跳转页面显示结果如下:

    从上面的运行结果来看,pageContext属性的作用范围仅限于当前JSP页面,从attrTest1页面跳转到attrTest2页面之后,pageContext对象获得的属性值为null。

    修改上面的代码:

    attrTest1.jsp中,做如下修改:

    <h2>attrTest1 page: <%=new Date() %></h2>
    

    attrTest2.jsp中,做如下修改:

    <h2>attrTest2 page:<%=new Date() %></h2>
    

    运行attrTest1.jsp后:

    跳转到attrTest2页面:

    从上面的运行结果可以看出,request的属性作用范围仅限于同一个请求,对于不同页面的不同请求的Date函数的调用,输出的时间是不同的。

    运行attrTest1.jsp:

     

    跳转到attrTest2 页面时:

    关闭浏览器,直接运行attrTest2.jsp,看到session的属性值为null,运行结果显示如下:

    从上面的运行结果可以看出,在一次会话(session)过程中,在第一个页面上设置的属性,跳转(服务器跳转/客户端跳转)到其他页面之后,其他的页面依然可以取得第一个页面上设置的属性。会话结束后,属性值丢失(体现为session值为null)。

  • 相关阅读:
    Linux 多线程编程 实例 1
    面试题-链表反转c实现
    information_schema.TABLES
    mongodb遇到的错误
    MySQL优化的奇技淫巧之STRAIGHT_JOIN
    mongodb安装
    XtraBackup安装
    提高mysql千万级大数据SQL查询优化30条经验(Mysql索引优化注意)
    我用 TypeScript 语言的七个月
    Grunt之添加文件监视:Grunt-watch (已备份)
  • 原文地址:https://www.cnblogs.com/naihuangbao/p/9794792.html
Copyright © 2011-2022 走看看