码迷,mamicode.com
首页 > 移动开发 > 详细

JSP内置对象 - application对象和各对象的作用域

时间:2019-09-26 21:03:35      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:ssi   div   getattr   idea   请求   settings   tor   页面   set   

1、application对象的常用方法:

<table>
        <tr>
<%--            获取当前Web服务器的版本信息--%>
            <td>getServerInfo</td>
            <td><%=application.getServerInfo()%></td>
        </tr>
        <tr>
<%--            获取某一资源的路径--%>
            <td>getResource</td>
            <td><%=application.getResource("/application_test.jsp")%></td>
        </tr>
        <tr>
<%--            获取根目录的路径--%>
            <td>getRealPath</td>
            <td><%=application.getRealPath("/")%></td>
        </tr>
        <tr>
<%--            获取当前Web应用程序的名称--%>
            <td>getServletContextName</td>
            <td><%=application.getServletContextName()%></td>
        </tr>
</table>

 

2、application、request、pageContext、session对象的作用域

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/26
  Time: 19:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>各对象的作用域</title>
</head>
<body>
<%
    if (application.getAttribute("accessCount") == null) {
        application.setAttribute("accessCount", new Integer(1));
    } else {
        Integer accessCount = (Integer)application.getAttribute("accessCount");
        accessCount += 1;
        application.setAttribute("accessCount", accessCount);
    }
%>
<%
    if (request.getAttribute("accessCount") == null) {
        request.setAttribute("accessCount", new Integer(1));
    } else {
        Integer accessCount = (Integer)request.getAttribute("accessCount");
        accessCount += 1;
        request.setAttribute("accessCount", accessCount);
    }
%>
<%
    if (pageContext.getAttribute("accessCount") == null) {
        pageContext.setAttribute("accessCount", new Integer(1));
    } else {
        Integer accessCount = (Integer)pageContext.getAttribute("accessCount");
        accessCount += 1;
        pageContext.setAttribute("accessCount", accessCount);
    }
%>
<%
    if (session.getAttribute("accessCount") == null) {
        session.setAttribute("accessCount", new Integer(1));
    } else {
        Integer accessCount = (Integer)session.getAttribute("accessCount");
        accessCount += 1;
        session.setAttribute("accessCount", accessCount);
    }
%>
<%--application对象的作用域:开始于服务器的启动,终止于服务器的关闭--%>
application: <%=application.getAttribute("accessCount")%><br />
<%--request对象的作用域:该页面和从该页面转向的页面--%>
request: <%=request.getAttribute("accessCount")%><br />
<%--pageContext对象的作用域:该页面--%>
pageContext: <%=pageContext.getAttribute("accessCount")%><br />
<%--session对象的作用域:浏览器第一次向服务器发送请求到存储时间结束或显式调用invalidate方法--%>
session: <%=session.getAttribute("accessCount")%><br />
<%--<%request.getRequestDispatcher("index.jsp").forward(request,response);%>--%>
</body>
</html>

index.jsp :

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/26
  Time: 19:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>index.jsp页面</title>
  </head>
  <body>
  <%
      if (request.getAttribute("accessCount") == null) {
          request.setAttribute("accessCount", new Integer(1));
      } else {
          Integer accessCount = (Integer)request.getAttribute("accessCount");
          accessCount += 1;
          request.setAttribute("accessCount", accessCount);
      }
  %>
  <%=request.getAttribute("accessCount")%>
  </body>
</html>

 

参考文档:

1)https://www.cnblogs.com/mark5/p/11075493.html

2)https://wenku.baidu.com/view/d98cea41336c1eb91b375d07.html

3)https://www.e-learn.cn/content/qita/2362810

4)https://www.cnblogs.com/chao521/p/9189484.html

JSP内置对象 - application对象和各对象的作用域

标签:ssi   div   getattr   idea   请求   settings   tor   页面   set   

原文地址:https://www.cnblogs.com/GjqDream/p/11594217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!