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

      本篇继前两篇内置对象,继续记录JSP中的其他的内置对象:application,page,pageContext,config,exception

      application内置对象

      该对象相当于JAVA中的全局静态对向,在服务器启动后,就一直存在,知道服务器关闭。

      该对象中的值,在整个服务器开启期间都是所有用户共享的,因此通常用于做一些用户登录次数统计和服务相关信息的操作。

      常用的方法包括:

      setAttribute()

      getAttribute()

      getAttributeNames()

      getServerInfo()

      使用方式如下:

        <%
            application.setAttribute("name","xingoo");
            application.setAttribute("age","32");
        %>
        name:<%=application.getAttribute("name") %><br>
        application中的属性:<br><%
            Enumeration attributes = application.getAttributeNames();
            while(attributes.hasMoreElements()){
                String name = attributes.nextElement().toString();
                out.println(name+":&nbsp;&nbsp;"+application.getAttribute(name)+"<br>");
            }
        %><br>
        JSP(servlet)引擎版本号:<%=application.getServerInfo() %>

      样例代码:

    <%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"%>
        <%-- 
        language 脚本使用的语言
        import 加载类文件
        contentType 编码方式
         --%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>application内置对象</title>
    </head>
    <body>
        <h1>application</h1>
        <%
            application.setAttribute("name","xingoo");
            application.setAttribute("age","32");
        %>
        name:<%=application.getAttribute("name") %><br>
        application中的属性:<br><%
            Enumeration attributes = application.getAttributeNames();
            while(attributes.hasMoreElements()){
                String name = attributes.nextElement().toString();
                out.println(name+":&nbsp;&nbsp;"+application.getAttribute(name)+"<br>");
            }
        %><br>
        JSP(servlet)引擎版本号:<%=application.getServerInfo() %>
    </body>
    </html>
    View Code

      运行效果:

      可以看到application默认保存了一些服务器相关的信息

      page内置对象

      page对象用于JSP页面本身的引用,相当于一个this指针。
      因为JSP本身会解析成一个java类,这个page对象,就是该类的一个类对象。

      常用方法就是类的基本方法:

        class getClass()返回此object类
        int hashCode() hash码
        boolean equals(Object obj) 是否与制定的对象相等
        void copy(Object obj) 拷贝
        Object clone() 克隆
        String toString() 
        void notify() 唤醒线程
        void notifyAll() 唤醒所有线程
        void wait(int timeout) 使一个线程处于等待直到timeout结束或被唤醒
        void wait() 使一个线程处于等待直到被唤醒

      比如,在页面中引用:

    pageObject:<%=page.getClass() %>

      可以得到如下的结果:

    pageObject:class org.apache.jsp.jspPage_jsp

      这是因为jsp页面,会被解析成:【JSP页面名字】_jsp.java 

      pageContext内置对象

      这个对象很强大,通过它可以得到session,page,application,request,response等对象。

      常用的方法:

        JspWriter getOut()回去输出流
        HttpSession getSession() 返回当前HttpSession对象
        Object getPage() 返回Object对象
        ServletRequest getRequest()
        ServletResponse getResponse()
        void setAttribute
        Object getAttribute()
        int getAttributeScope(String name)返回属性的作用范围
        void forward(String relativeUrlPath) 使当前页面重导到另一页面
        void include(String relativeUrlPath)当前位置包含另一文件

      config内置对象

      这个对象用于初始化Servlet时,传递一些必要的信息参数等,比如服务器的相关信息都会以ServletContext对象进行传递,通过该对象可以获得ServletContext的引用。

      常用的方法:

        ServketContext getServletContext()返回服务器相关信息对象
        String getInitParameter(String name)返回初始化参数值
        Enumeration getInitParameterNames() 返回Servlet初始化需要的参数

      exception内置对象

      这个对象时异常对象,如果要使用该对象,需要注意:

      1 在使用界面使用 errorPage="错误处理页面.jsp" ,指定错误处理的JSP

      2 在错误处理的JSP页面中,设定 isErrorPage="true"

      参考样例如下:

      在使用界面,指定错误处理的界面

    <%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"
        errorPage="exception.jsp"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>exception内置对象</title>
    </head>
    <body>
        <h1>JSP</h1>
        <%
            System.out.println(100/0);//抛出运行时异常,算术异常
        %>
    </body>
    </html>

      在错误处理界面,进行一场的捕获:

    <%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"
        isErrorPage="true"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>exception内置对象</title>
    </head>
    <body>
        <h1>JSP</h1>
        异常消息是:<%=exception.getMessage() %><br>
        异常字符串描述:<%=exception.toString() %><br>
    </body>
    </html>

      可得到如下的错误信息:

  • 相关阅读:
    我的vim开发环境搭建:C/C++/Go,持续更新中
    MFC的组合框(ComboBox)控件切换下拉样式
    回顾下杂乱的10月
    C++将整型数据转换成大端或小端存储顺序
    C/C++动态分配连续空间,下标越界导致的free():invalid next size问题
    O(n)空间复杂度,打印杨辉三角形的前n行
    C指针笔试题,蛋疼的多重指针运算,谭浩强的阴影
    2017滴滴出行笔试题:异或和为0的最大区间个数
    manjaro安装
    关于top命令
  • 原文地址:https://www.cnblogs.com/xing901022/p/4356120.html
Copyright © 2011-2022 走看看