zoukankan      html  css  js  c++  java
  • JSP内置对象

    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

  • 相关阅读:
    201871020225牟星源 《面向对象程序设计(java)》第一周学习总结
    201871020225牟星源《面向对象程序设计(java)》第十周学习总结
    201871020225牟星源 《面向对象程序设计(java)》课程学习进度条
    201871020225牟星源《面向对象程序设计(java)》第67周学习总结
    201871020225牟星源《面向对象程序设计(java)》第四周学习总结
    201871020225牟星源《面向对象程序设计(java)》第七周学习总结
    模拟赛 题目重修
    SP1026 FAVDICE Favorite Dice
    可是姑娘,你为什么要编程呢?
    javascript中offset、client、scroll的属性总结
  • 原文地址:https://www.cnblogs.com/GjqDream/p/11594217.html
Copyright © 2011-2022 走看看