JSP内置对象作用域

JSP内置对象作用域

四个jsp内置对象:page、request、session、application

作用域

1.内置对象

page 作⽤域:对应的内置对象是 pageContext。

request 作⽤域:对应的内置对象是 request。

session 作⽤域:对应的内置对象是 session。

application 作⽤域:对应的内置对象是 application。

2.作用范围

作用范围:page < request < session < application

page 只在当前⻚⾯有效。

request 在⼀次请求内有效。

session 在⼀次会话内有效。

application 对应整个 WEB 应⽤的。

简单示例:网站访问量统计

分析,不同用户可能用不同的浏览器访问,排除了page、request、session,所以选择application

<%
    //这个例子只要TOMCAT开着就能统计,不同浏览器轮流访问也能统计



    Integer count=(Integer)application.getAttribute("count");
    //如果count=null  就是第一次访问
    //注意这里是两个等于号
    if(count==null){
        count=0;
        application.setAttribute("count",count);
    }
    count=(Integer)application.getAttribute("count");
    count++;
    application.setAttribute("count",count);
%>
<h1>您是当前的第<%=count%>位访客</h1>
原文地址:https://www.cnblogs.com/wind-and-sky/p/14212883.html