JSP内置对象(下)

 


JSP中共有9大内置对象:

  1. out对象
  2. requset对象
  3. response对象
  4. session对象
  5. pageContext对象
  6. application对象
  7. config对象
  8. page对象
  9. exception对象

JSP内置对象(上)http://wxmimperio.coding.io/?p=280 


5. pageContext对象

pageContext对象被封装成javax.servlet.jsp.pageContext接口,他为JSP页面包装页面的上下文,管理对属于JSP中特殊可见部分中已命名对象的访问。它的创建和初始化都是由容器完成的,pageContext对象可以访问到本页面所在的session,也可以去本页面 所在的application的某一属性,pageContext对象相当于页面中所有功能的集成。JSP页面里可以直接使用pageContext对象的句柄,pageContext对象的getXXX()、setXXX()和findXXX()方法可以用来根据不同的对象范围实现对这些对象的管理。

常用的方法:

  • forward(java.lang,String relativeUrlPath):把页面重定向到另外一个页面或者Servlet组件上
  • getRequset():返回当前的requset对象
  • getResponse():返回当前的response对象
  • getServletConfig():返回当前页面的ServletConfig对象
  • getServletContext():返回ServletContext对象,这个对象对所有的页面都是共享的
  • getSession():返回当前页面的Session对象
  • setAttribute():用来设置默认页面范围,或特定对象范围之中的已命名对象
  • getAttribute(java.lang.String name[,int scope]):scope参数是可选的,用来检索一个特定的已命名的对象范围,并且还可以通过调用getAttributeNamesInScope()方法,检索对某个特定范围的每个属性String字符串名称的枚举

代码:

pageContextForm.jsp

  • <form method="post" action="pageContextObject.jsp">  
  •     <table>  
  •         <tr>  
  •             <td>name:</td>  
  •             <td><input type="text" name="name"></td>  
  •         </tr>  
  •         <tr colspan="2">  
  •             <td><input type="submit" value="提交"></td>  
  •         </tr>  
  •     </table>  
  • </form>  

pageContextObject.jsp

  • <%  
  •     //通过pageContext.getRequest方法来获取request对象  
  •     ServletRequest req = pageContext.getRequest();  
  •     String name = req.getParameter("name");  
  •     out.println("name="+name+"<br>");  
  •     //pageContext.getServletConfig方法  
  •     out.println("ServletConfig="+ pageContext.getServletConfig());  
  •     //pageContext.setAttribute方法  
  •     pageContext.setAttribute("username",name);  
  •     //pageContext.getServletContext方法  
  •     pageContext.getServletContext().setAttribute("sharevalue","多个页面共享");  
  •     //pageContext.getSession方法  
  •     pageContext.getSession().setAttribute("sessionvalue","只有在session中才是共享的");  
  •     out.println("<br>pageContext.getAttributte('username')=");  
  •     //pageContext.getAttribute方法  
  •     out.println(pageContext.getAttribute("username"));  
  •     /*//pageContext.forward方法 
  •     pageContext.forward("pageContext2.jsp");*/  
  • %>  
  • <html>  
  • <head>  
  •     <title>pageContext内置对象</title>  
  • </head>  
  • <body>  
  •     <a href="pageContext2.jsp">NEXT--></a>  
  • </body>  
  • </html>  

pageContext2.jsp

  • <%  
  •     out.println("<br>pageContext.getAttribute('username')=");  
  •     out.println(pageContext.getAttribute("username"));  
  •     out.println("<br>pageContext.getSession.getAttribute('sessionvalue')=");  
  •     out.println(pageContext.getSession().getAttribute("sessionvalue"));  
  •     out.println("<br>pageContext.getServletContext.getAttribute('sharevalue')=");  
  •     out.println(pageContext.getServletContext().getAttribute("sharevalue"));  
  • %>  

PS:

  • pageContext属性默认在当前页面时共享的
  • session中的属性在当前session中是共享的
  • ServletContext对象中的属性对所有页面都是共享的

6.application对象

application对象为多个应用程序保存信息,对于一个容器而言,每个用户都共享使用一个application对象,在任何地方对application对象属性的操作,都将影响到其他用户对此的访问,这和session对象是不同的。启动服务器后就会自动创建application对象,这个对象会一直保存,直到服务器关闭,application对象是ServletContext类的一个实例。

常用的方法:

  • getAttributeNames():返回所有的application对象的属性的名字,其结果是一个枚举类型的实例
  • getServletInfo():返回Servlet编译器的版本信息
  • getAttribute(String name):返回由name指定的名字的application对象的属性值
  • setAttribution(String name,Object object):设置由name指定名字的application对象的属性值object

代码:(简单计数器)

applicationCount.jsp

  • <%  
  •     //计数器设置  
  •     int count = 0;  
  •     /*counter_name为计数器的名字 
  •     对于不同的页面,使用不同的计数器名字,这样就可以为不同的页面进行计数*/  
  •     String counter_name = request.getParameter("counter_name");  
  •     try {  
  •         //application.getAttribute方法  
  •         count = Integer.parseInt((application.getAttribute(counter_name).toString()));  
  •     } catch (Exception e) {  
  •     }  
  •     out.println("此页面访问了"+count+"次");  
  •     count++;  
  •     //application.setAttribute方法  
  •     application.setAttribute(counter_name,new Integer(count));  
  •     application.setAttribute("username","admin");  
  •     application.setAttribute("password","123456");  
  • %>  

applicationObject.jsp

  • <body>  
  • <%--通过include指令把appicationCount页面代码引入--%>  
  • <jsp:include page="appicationCount.jsp">  
  •     <jsp:param name="counter_name" value="applicationObject"></jsp:param>  
  • </jsp:include>  
  • <br>从application获得所有属性=  
  • <%  
  •     Enumeration e = application.getAttributeNames();  
  •     while (e.hasMoreElements()) {  
  •         out.println(e.nextElement()+"&nbsp;&nbsp;");  
  •     }  
  • %>  
  • <br>Servlet编译器的版本信息:<%=getServletInfo()%>  
  • </body>  

7.config对象

config对象被封装成javax.servlet.ServletConfig接口,他表示Servlet的配置,在一个Servlet初始化时,jsp引擎向他传递信息用的,此信息包括Servlet初始化时所要用到的参数以及服务器的有关信息。

常用的方法:

  • getServletContext():返回执行者的Servlet上下文
  • getServletName():返回Servlet的名字
  • getInitParameter(String name):返回名字为name的初始参数的值
  • getInitParameterNames():返回这个JSP的所有的初始参数的名字

代码:(上面application对象写的计数器有个缺陷,就是当服务器重启后,计数器就会从0重新开始,通过在服务器设置计数的初始值来解决

configObject.jsp

  • <%  
  •     int org = 0;  
  •     int count = 0;  
  •     try {  
  •         //config.getInitParameter方法  
  •         org = Integer.parseInt(config.getInitParameter("counter"));  
  •     } catch (Exception e) {  
  •         out.println("org:" + e);  
  •     }  
  •     try {  
  •         count = Integer.parseInt((application.getAttribute("config_counter").toString()));  
  •     } catch (Exception e) {  
  •         out.println("config_counter"+e);  
  •     }  
  •     if(count<org) count = org;  
  •     out.println("<br>此页面已经访问了"+count+"次<br>");  
  •     count++;  
  •     application.setAttribute("config_counter",new Integer(count));  
  • %>  
  • Servlet的名字是:<%=config.getServletName()%><br>  
  • Servlet的上下文是:<%=config.getServletContext()%><br>  
  • 这个JSP的所有的初始参数的名字=  
  • <%  
  •     Enumeration e = config.getInitParameterNames();  
  •     while (e.hasMoreElements()) {  
  •         out.println(e.nextElement()+"&nbsp;&nbsp;");  
  •     }  
  • %> 

web.xml配置文件

  • <servlet>  
  •     <servlet-name>config_counter</servlet-name>  
  •     <jsp-file>/InnerObject/configObject.jsp</jsp-file>  
  •     <init-param>  
  •         <param-name>counter</param-name>  
  •         <param-value>1000</param-value>  
  •     </init-param>  
  • </servlet>  
  • <servlet-mapping>  
  •     <servlet-name>config_counter</servlet-name>  
  •     <url-pattern>/InnerObject/config_counter</url-pattern>  
  • </servlet-mapping>  

PS:

通过configObject.jsp直接访问得不到初始化的counter参数,计数从0开始;而通过servlet配置地址config_counter访问计数直接从初始的1000开始,原因是当通过config_counter访问时,他是作为一个Servlet组件来运行,而直接通过JSP访问不同。


8.page对象

page对象是java.lang.Object类的一个实例,类似于java中的this对象,他指的是JSP页面本身,通过这个可以对他进行访问,只有在JSP页面的范围之内才是合法的。

常用的方法:

  • hashCode():返回此Object的hash码
  • equals(Object object):判断两个Object对象是否相等
  • toString():把Object对象转换为String对象

代码:

  • <body>  
  •     当前页面的hash码:<%=page.hashCode()%><br>  
  •     当前页面的字符串描述:<%=page.toString()%><br>  
  •     page和this的比较:<%=page.equals(this)%><br>  
  • </body>  

9.exception对象

exception对象是java.lang.Throwable类的一个实例,他指的是运行时的异常,也就是被调用的错误页面的结果,只有在错误页面(isErrorPage=true指令)中才可以使用。

常用的方法:

  • getMessage():返回异常信息的描述
  • toString():关于异常信息的简短描述
  • printStackTrace():显示异常及其栈的轨迹

代码:

exceptionObject.jsp

  • <%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="exception.jsp" %>  
  • <html>  
  • <head>  
  •     <title></title>  
  • </head>  
  • <body>  
  • <%  
  •     //异常  
  •     System.out.println(100/0);  
  • %>  
  • </body>  
  • </html>  

exception.jsp

  • <%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>  
  • <html>  
  • <head>  
  •     <title></title>  
  • </head>  
  • <body>  
  •     返回异常信息的描述:<%=exception.getMessage()%><br>  
  •     关于异常信息的简短描述:<%=exception.toString()%><br>  
  •     显示异常及其栈的轨迹:<%exception.printStackTrace();%><br>  
  • </body>  
  • </html> 

 

 

 


参考:JSP应用开发详解(第三版)、慕课网 

posted @ 2015-03-05 02:13  wxm-Imperio  阅读(347)  评论(0编辑  收藏  举报