zoukankan      html  css  js  c++  java
  • JSP的隐式对象(page,config,pageContext,exception)

    1.page对象

    这个对象就是页面实例的引用。它可以被看做是整个JSP页面的代表,page 对象就是this对象的同义词,它是 java.lang.Object 类的实例

    1.1page对象常用方法

    方法说明
    class getClass() 返回当前 Object 的类
    int hashCode() 返回 Object 的 hash 代码
    String toString() 把 Object 对象转换成 String 类的对象
    boolean equals(Object obj) 比较对象和指定的对象是否相等
    void copy (Object obj) 把对象拷贝到指定的对象中
    Object clone() 复制对象(克隆对象)
    void notify() 唤醒一个等待的线程
    void notifyAll() 唤醒所有等待的进程
    void wait(int timeout) 使一个线程处于等待直到被唤醒

    1.2 page对象简单例子

    page.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>page对象应用</title>
    </head>
    <body>
    <h2> page对象应用</h2>
    <%
    Object obj;
    obj=null;
    %>
    返回当前页面所在类:<%=page.getClass()%> <br>
    返回当前页面的 hash 代码:<%=page.hashCode()%> <br>
    转换成 String 类的对象:<%=page.toString()%> <br>
    比较1:<%=page.equals(obj) %> <br>
    比较2:<%=page.equals(this) %>
    </body>
    </html>

    运行结果

    2.config对象

    config对象是 javax.servlet.ServletConfig 类的实例,表示 Servlet 的配置信息。开发者可以在 web.xml 文件中为应用程序环境中的 Servlet 程序和 JSP 页面提供初始化参数。

    config 对象的常用方法

    方法说明
    ServletContext getServletContext() 返回所执行的 Servlet 的环境对象
    String getServletName() 返回所执行的 Servlet 的名字
    String getInitParameter(String name) 返回指定名字的初始参数值
    Enumeration getlnitParameterName() 返回该 JSP 中所有初始参数名,一个枚举

    config 对象简单示例——创建一个Servlet名为TestServlet,输出Servlet的名称

    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class Test2Servlet
     */
    @WebServlet("/TestServlet")
    public class TestServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                     //设置输入格式为UTF-8
                    request.setCharacterEncoding("UTF-8");
                    //设置内容类型为UTF-8
                    response.setContentType("text/html;charset=utf-8");
                    String name = this.getServletConfig().getServletName();
                    PrintWriter out = response.getWriter();
                    out.write("servlet的名称为:"+name);
                    
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

    运行结果:

    3.pageContext对象(转载http://c.biancheng.net/view/1495.html

    pageContext对象是javax.servlet.jsp.PageContext 类的实例,用来代表整个JSP页面。可以用它访问本页中所有的其他对象,如 session、application、config、out 等对象的属性。

     pageContext 对象是 javax.servlet:jsp.pageContext 类的一个实例,它的创建和初始化都是由容器来完成的,JSP 页面里可以直接使用 pageContext 对象的句柄,pageContext 对象的 getXxx()、setXxx() 和 findXxx() 方法可以根据不同的对象范围实现对这些对象的管理。

      pageContext对象的常用方法

    方法说明
    void forward(String relativeUrlPath) 把页面转发到另一个页面或者 Servlet 组件上
    Exception getException() 返回当前页的 Exception 对象
    ServletRequest getRequest() 返回当前页的 request 对象
    ServletResponse getResponse() 返回当前页的 response 对象
    ServletConfig getServletConfig() 返回当前页的 ServletConfig 对象
    HttpSession getSession() 返回当前页的 session 对象
    Object getPage() 返回当前页的 page 对象
    ServletContext getServletContext() 返回当前页的 application 对象
    public Object getAttribute(String name) 获取属性值
    Object getAttribute(String name,int scope) 在指定的范围内获取属性值
    void setAttribute(String name,Object attribute) 设置属性及属性值
    void setAttribute(String name,Object obj,int scope) 在指定范围内设置属性及属性值
    void removeAttribute(String name) 删除某属性
    void removeAttribute(String name,int scope) 在指定范围内删除某属性
    void invalidate() 返回 servletContext 对象,全部销毁

    pageContext 对象的主要作用是提供一个单一界面,以管理各种公开对象(如 session、application、config、request、response 等),提供一个单一的 API 来管理对象和属性。

    pageContext 对象简单示例1

    pageContext.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>pageContext 对象获取不同范围属性</title>
    </head>
    <body>
    <%
    request.setAttribute("info","value of request scope");
    session.setAttribute("info","value of request scope");
    application.setAttribute("info","value of application scope");
    %>
    利用 pageContext 取出以下范围内各值(方法一):<br>
    request 设定的值:<%=pageContext.getRequest().getAttribute("info") %> <br>
    session 设定的值:<%=pageContext.getSession().getAttribute("info") %> <br>
    application 设的值:<%=pageContext.getServletContext().getAttribute("info") %> <hr>
    利用pageContext取出以下范围内各值(方法二):<br>
    范围1(page)内的值:<%=pageContext.getAttribute("info",1) %> <br>
    范围2(request)内的值:<%=pageContext.getAttribute("info",2) %> <br>
    范围3(session)内的值:<%=pageContext.getAttribute("info",3) %> <br>
    范围4(application)内的值:<%=pageContext.getAttribute("info",4) %> <hr>
    利用 pageContext 修改或删除某个范围内的值:
    <% pageContext.setAttribute("info","value of request scope is modified by pageContext",2); %> <br>
    修改 request 设定的值:<br>
    <%=pageContext.getRequest().getAttribute("info") %> <br>
    <% pageContext.removeAttribute("info"); %>
    删除 session 设定的值:<%=session.getAttribute("info") %>
    </body>
    </html>

    运行结果

     pageContext 对象简单示例2——与request对象比较

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>pageContext对象与request对象比较</title>
    </head>
    <body>
      <%
      //可以得到当前页面的request对象
       int port1= pageContext.getRequest().getLocalPort();//获取request对象
       int port2= request.getLocalPort();
       
       out.print(port1+"<br>");
       out.print(port2+"<br>");
       
       //测试 对象作用域的应用范围
       request.setAttribute("user", "zhangsan");
       pageContext.setAttribute("user", "lisi");
      %>
     <!-- 1.在同一个JSP页面中,它们都能访问 --> 
      <%=request.getAttribute("user") %><br>
      <%=pageContext.getAttribute("user") %>
      
       <!-- 2.转发到其他的JSP页面中 --> 
      <%
      //请求转发
      //request.getRequestDispatcher("requestDis.jsp").forward(request, response);
      %>
    </body>
    </html>
    View Code

    运行结果:

     去掉第2点请求转发的注释,添加requestDis.jsp页面如下:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <%=request.getAttribute("user")%><br>
     <%=pageContext.getAttribute("user") %>
    </body>
    </html>
    View Code

    运行结果为:

      pageContext 对象简单示例3——与session、application对象比较

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>计数器</title>
    </head>
    <body>
    <%
    //分别创建3个计数器
    //1.页面计数
    if(pageContext.getAttribute("pageCount")==null)
    {
        pageContext.setAttribute("pageCount",new Integer(0));
    }
    
    //2.会话计数
    if(session.getAttribute("sessionCount")==null)
    {
        session.setAttribute("sessionCount",new Integer(0));
    }
    
    //3.整站计数
    if(application.getAttribute("applicationCount")==null)
    {
        application.setAttribute("applicationCount",new Integer(0));
    }
    
    //页面计数,
    Integer count1 = (Integer)pageContext.getAttribute("pageCount");
    pageContext.setAttribute("pageCount",new Integer(count1.intValue()+1));
    
    //会话计数
    Integer count2 = (Integer)session.getAttribute("sessionCount");
    session.setAttribute("sessionCount",new Integer(count2.intValue()+1));
    
    //整站计数,所有的JSP页面的访问量
    Integer count3 = (Integer)application.getAttribute("applicationCount");
    application.setAttribute("applicationCount",new Integer(count3.intValue()+1));
    
    %>
    
    <b>页面计数</b>
    <%=pageContext.getAttribute("pageCount") %><br>
    <b>会话计数</b>
    <%=session.getAttribute("sessionCount") %><br>
    <b>整站计数</b>
    <%=application.getAttribute("applicationCount") %><br>
    
    </body>
    </html>
    View Code

    运行结果:

    4.exception对象(转载:https://www.iteye.com/blog/qq-24665727-2279609

    异常处理(只有在声明为错误页面才有exception对象)

    4.1定义错误页面
      <%@ page isErrorPage="true" %>

    test.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@ page errorPage="error.jsp"%>  
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>测试页面</title>
    </head>
    <body>
     <%  
            int a = 1, b = 0;  
        %>  
        <%=a / b%>  
        <a href="index7.jsp">这是一个超连接</a>  
    </body>
    </html>

    error.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page isErrorPage="true" %>  
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>错误信息页面</title>
    </head>
    <body>
    你访问的页面飞走了!!  
      
    <%  
    String msg = exception.getMessage();  
    %>  
      
    <%=msg %>  
    </body>
    </html>

    运行结果

    参考文章:

    https://blog.csdn.net/CCCrunner/article/details/81052526

    http://c.biancheng.net/view/1494.html

    http://c.biancheng.net/view/1495.html

    https://www.iteye.com/blog/qq-24665727-2279609

    转载文章链接已标明,如有侵权请告知。文章仅作为知识记忆所用,如有错误,敬请指正。
  • 相关阅读:
    OpenVINO Model Server的服务化部署——step3(django服务构建)
    (5)名称空间 namespace 和 using 声明
    (4)#include 指令
    (3)注释
    (2)简单的程序
    (1)Hello World
    javaScript 错误学习 -- throw、try 、catch和 finally
    js 如何在数字前面自动补零,生成序列号、单据号
    vs2015项目运行出现“无法启动IIS Express Web服务器”,如何解决
    Sql Server 2008 如何将数据表导出Excel文件?
  • 原文地址:https://www.cnblogs.com/YorkZhangYang/p/12498038.html
Copyright © 2011-2022 走看看