JSP内置对象 - application对象和各对象的作用域
Posted gjqdream
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP内置对象 - application对象和各对象的作用域相关的知识,希望对你有一定的参考价值。
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对象和各对象的作用域的主要内容,如果未能解决你的问题,请参考以下文章