• jsp>四种作用域 小强斋


    page 范围:

    在 JSP 中设置一个页的属性范围,必须通过pageContext 完成

    <%
    pageContext.setAttribute("name","MLDN") ;
    pageContext.setAttribute("password","LXH") ;
    %>
    <%
    String name = (String)pageContext.getAttribute("name") ;
    String password = (String)pageContext.getAttribute("password") ;
    %>
    
    
    <h1>name : <%=name%></h1>
    <h1>password : <%=password%></h1>
    

    request

    将属性保存在一次请求范围之内:
    服务器端跳转<jsp:forward/>可以保存属性。

    session

    只要设置上去,则不管是什么跳转,都可以取得属性,与 session 有关的任何打开的页面都可以取得session,在重新打开的浏览器页面内失效。
    session 的主要功能:用于验证用户是否登陆

    application 属性范围值

    只要设置一次,则所有的网页窗口都可以取得数据,如果要释放 application 资源,只能重新启动服务器
    application 应用:在线人员统计、在线人员名单列表
    application、session、request   都需要跨多个页属性保存是有内存开销的,如果能使用 request 就不要使用session,能使用session 的就不要使用application
    实际上,四种属性范围,都是通过pageContext 对象完成的
    pageContext  默认情况下表示一个页面的保存范围,
    public abstract void setAttribute(String name,Object value) 上面用到的
    public abstract void setAttribute(String name,Object value,int scope)

    public static final int APPLICATION_SCOPE
    public static final int SESSION_SCOPE
    public static final int REQUEST_SCOPE
    public static final int PAGE_SCOPE
    总结:
    四种属性范围都是依靠 pageContext 展开的,pageContext.setAttribute(String name,Object value,int scope)
    如<% pageContext.setAttribute("name","wsz",PageContext.APPLICATION_SCOPE);%>
    但是在开发中,往往使用session、request 范围最多
     
     
  • 相关阅读:
    “能用”距离“好用”有多远?
    序列化到底是神马?
    新版Microsoft Azure Web管理控制台
    玩转Windows Azure存储服务——高级存储
    Windows 10 Threshold 2 升级记录
    玩转Windows Azure存储服务——网盘
    Windows Azure HDInsight 使用技巧
    Docker on Microsoft Azure
    Azure Linux VM Swap 分区
    Google Cloud Platform
  • 原文地址:https://www.cnblogs.com/xiaoqiangzhaitai/p/5637592.html
Copyright © 2020-2023  润新知