码迷,mamicode.com
首页 > Web开发 > 详细

JSP内置对象

时间:2014-11-24 22:09:54      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

JSP内置对象

一、概述

为了简化Web应用程序的开发,在JSP中定义了一些JSP容器实现和管理的内置对象,这些对象可以直接在JSP页面中使用,而不需要JSP页面编写者对它们进行初始化。

二、JSP属性范围

在介绍内置对象之前,先介绍JSP中提供的4种属性的作用范围,分别是page、request、session、application。

1page范围:指所设置的属性仅在当前页面有效。使用pageContext的setAttribute()方法可以设置属性值,使用pageContext的getAttribute()方法可以获得属性值。

2request范围:指属性仅在一次请求范围内有效。使用request的setAttribute()方法可以获得属性值,使用request的getAttribute()方法可以获得属性值。

3session范围:指的是属性仅在浏览器与服务器进行一次会话的范围内有效,当和服务器断开连接后,属性就会失效。使用session的setAttribute()方法可以设置属性范围,使用session的getAttribute()方法可以获得属性值。

4application范围:指属性在整个Web的应用中都有效,直到服务器停止后才失效。使用application的setAttribute()方法可以设置属性值,同样application的getAttribute()方法可以获得属性值。

三、JSP内置对象

1request对象:用于获取客户端信息,例如我们在表单中填写的信息等。实际上,JSP容器会将客户端的请求信息封装在request对象中。在客户端发出请求时会创建request对象,在请求结束后,则会销毁request对象。

在request对象中提供了一系列的方法用来获取客户端的请求参数。

这些方法包括:getParameter、getParameterNames、getParameterValues、getParameterMap。

requestform.jsp

  1. <%@ pagecontentType="text/html; charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>表单页</title>  
  5.        </head>  
  6.        <body>  
  7.        <form action="requestobject2.jsp"method="post">  
  8.        <p>用户名:<input type="text" name="username"/></p>  
  9.        <p>年龄:<input type="text" name="age"/></p>  
  10.        <input type="submit"value="提交" />  
  11.        </form>  
  12.        </body>  
  13. </html>  


requestobject.jsp

  1. <%@ pagecontentType="text/html; charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>request 表单页1</title>  
  5.        </head>  
  6.        <body>  
  7.               <%  
  8.                      request.setCharacterEncoding("gb2312");  
  9.                      Stringusername=request.getParameter("username");  
  10.                      String_age=request.getParameter("age");  
  11.                      intage=Integer.parseInt(_age);  
  12.                %>  
  13.                用户名:<%=username %><br>  
  14.                年龄:<%=age %>  
  15.        </body>  
  16. </html>  


bubuko.com,布布扣

requestobject2.jsp

  1. <%@ pagecontentType="text/html; charset=GB2312" %>  
  2. <%@ pageimport="java.util.*" %>  
  3. <html>  
  4.        <head>  
  5.               <title>request 表单页2</title>  
  6.        </head>  
  7.        <body>  
  8.               <%  
  9.                      String param="";  
  10.                      request.setCharacterEncoding("gb2312");  
  11.                      Enumerationparams=request.getParameterNames(); //获取所有参数值   
  12.               //循环输出所有参数值  
  13.                while(params.hasMoreElements()){  
  14.                     param=(String)params.nextElement();   //设置编码格式  
  15.                     //打印参数名  
  16.                     out.println("ParamName:"+param+"<br>");  
  17.                     //打印参数值  
  18.                     out.println("ParamValues:"+request.getParameter(param)+"<br>");  
  19.                }  
  20.                %>  
  21.        </body>  
  22. </html>  


bubuko.com,布布扣

2response对象:包含了从JSP页面返回客户端的所有信息,其作用域是它所在的页面。response对象是javax.servlet.ServletResponse类的一个实例,它封装由JSP产生的响应,并返回客户端的响应请求。

response对象经常用于设置HTTP标题、添加cookie、设置响应内容的类型和状态、发送HTTP重定向和编码URL。通过response的sendRedirect(String url)方法可以实现重定向。

responseform.jsp:

  1. <%@ pagecontentType="text/html; charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>表单页</title>  
  5.        </head>  
  6.        <body>  
  7.        <form action="responseobject.jsp"method="post">  
  8.        <p>用户名:<input type="text" name="username"/></p>  
  9.        <p>国家:<input type="text" name="country"/></p>  
  10.        <input type="submit"value="提交" />  
  11.        </form>  
  12.        </body>  
  13. </html>  


requestobject.jsp:

  1. <%@ pagecontentType="text/html; charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>response 表单页</title>  
  5.        </head>  
  6.        <body>  
  7.               <%  
  8.                      String str=null;  
  9.                      str=request.getParameter("username");  
  10.                      //判断用户名输入是否为空  
  11.                      if(str==null){  
  12.                             str="";  
  13.                      }  
  14.                      //使用ISO-8859-1字符集将str解码为字节序列,并将结果存储在字节数组中  
  15.                      byteb[]=str.getBytes("ISO-8859-1");  
  16.                      str=newString(b);    //将字节数组重组为字符串  
  17.                      if(str.equals("")){  
  18.                             response.sendRedirect("responseform.jsp");  
  19.                      }  
  20.                      else{  
  21.                             out.println("response示例");  
  22.                             out.println(str);  
  23.                      }  
  24.                %>  
  25.        </body>  
  26. </html>  


bubuko.com,布布扣

bubuko.com,布布扣

3out对象:是一个缓冲的输出流,用来向客户端返回信息。它是javax.servlet.jsp.JspWriter的一个实例。由于向客户端输出时要先建立连接,所以总是采用缓冲输出的方式,out是缓冲输出流。

outobject.jsp:

  1. <%@ page contentType="text/html;charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>out对象</title>  
  5.        </head>  
  6.        <body>  
  7.               <%  
  8.                      out.print("鲍礼彬");    //不换行  
  9.                      out.println("Hadoop");  //换行  
  10.                      //获取缓存区信息  
  11.                %>  
  12.                <br>  
  13.                <%  
  14.                     intallbuf=out.getBufferSize();   //获取缓存区大小  
  15.                     intremainbuf=out.getRemaining();  //获取剩余缓存区的大小  
  16.                     intusedbuf=allbuf-remainbuf;             //已用缓存区  
  17.                     out.println("已用缓存区大小:"+usedbuf);  
  18.                 %>  
  19.        </body>  
  20. </html>  


bubuko.com,布布扣

4session对象:是会话对象,用来记录每个客户端的访问状态。

HTTP协议是一个无状态的协议。一个客户向服务器发出请求(request),然后服务器返回相应(response),此后连接就被关闭了。在这种情况下,可以采用会话(session)来记录连接的信息。

所谓会话指的是从一个客户端打开浏览器与服务器建立连接,到这个客户关闭浏览器与服务器断开连接的过程。

有了session对象,服务器就可以知道这是同一个客户完成的动作。

login.jsp:

  1. <%@ page contentType="text/html;charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>登陆页</title>  
  5.        </head>  
  6.        <body>  
  7.        <%  
  8.               Stringname="";  
  9.               //判断是否为新的session  
  10.               if(!session.isNew()){  
  11.                      name=(String)session.getAttribute("username");  
  12.                      //获取session中的username值  
  13.                      if(name==null){  
  14.                             name="";  
  15.                      }  
  16.               }  
  17.         %>  
  18.         <p>欢迎光临</p>  
  19.         <p>Session ID:<%=session.getId()%></p>  
  20.                <form action="check.jsp"method="post">  
  21.                      <p>用户名:<inputtype="text" name="username" value=<%=name%>></p>  
  22.                      <inputtype="submit" value="提交" />  
  23.               </form>  
  24.        </body>  
  25. </html>  


check.jsp:

  1. <%@ pagecontentType="text/html; charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>进入邮箱</title>  
  5.        </head>  
  6.        <body>  
  7.               <%  
  8.                      String name=null;  
  9.                      name=request.getParameter("username");  //获取request中的username值  
  10.                      if(name!=null){  
  11.                             session.setAttribute("username",name);   //设置session的属性值  
  12.                      }  
  13.                %>  
  14.                <a href="login.jsp">登陆</a>     <ahref="loginout.jsp">注销</a>  
  15.                <br>  
  16.                <p>当前用户为:<%=name %></p>  
  17.                <p>邮箱中共有29封邮件</p>  
  18.        </body>  
  19. </html>  


loginout.jsp:

  1. <%@ pagecontentType="text/html; charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>注销页面</title>  
  5.        </head>  
  6.        <body>  
  7.               <%  
  8.                      String     name=(String)session.getAttribute("username");  //获取request中的username值  
  9.                      session.invalidate();              //清空session  
  10.                %>  
  11.                <%=name %>,再见!  
  12.                <p><p>  
  13.                <a href="login.jsp">重新登陆</a>  
  14.        </body>  
  15. </html>  


bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

演示的效果是,当在登陆页输入非汉字用户名时,会出现上面第二个图,点击登陆,上次的用户名自动写在用户名框里;若选择注销,则再重新登陆时,用户名框为空。

5application对象:用于获取和设置Servlet的相关信息,它的生命周期是从服务器启动直到服务器关闭为止,一旦创建了一个application对象,该对象会一直存在,直到服务器关闭。Application中封装了JSP所在的Web应用中的信息。

applicationobject.jsp:

  1. <%@ page contentType="text/html;charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>网站计数器</title>  
  5.        </head>  
  6.        <body>  
  7.               <%  
  8.                      Stringcount=(String)application.getAttribute("count"); //获取属性  
  9.                      if(count==null){  
  10.                             count="1";  
  11.                      }else{  
  12.                             count=Integer.parseInt(count)+1+"";  
  13.                      }  
  14.                      application.setAttribute("count",count);  
  15.                %>  
  16.                <%="<h1>到目前为止,网站访问人数为:"+count+"</h1><br>" %>  
  17.        </body>  
  18. </html>  


bubuko.com,布布扣

如图,每刷新一次,人数加1

6pageContext对象:是一个比较特殊的对象,不仅可以设置page范围内的属性,还可以设置其他范围内的属性。通过它还可以访问本页面中所有其他对象。在JSP开发中,此对象使用的并不多。

pageContextobject.jsp:

  1. <%@ page contentType="text/html;charset=GB2312" %>  
  2. <html>  
  3.        <head>  
  4.               <title>pageContext对象</title>  
  5.        </head>  
  6.        <body>  
  7.        <%  
  8.               pageContext.setAttribute("attributename","page_scope");  
  9.               request.setAttribute("attributename","request_scope");  
  10.               session.setAttribute("attributename","session_scope");  
  11.               application.setAttribute("attributename","application_scope");  
  12.                
  13.               Stringstr1=(String)pageContext.getAttribute("attributename",pageContext.PAGE_SCOPE);  
  14.               Stringstr2=(String)pageContext.getAttribute("attributename",pageContext.REQUEST_SCOPE);  
  15.               Stringstr3=(String)pageContext.getAttribute("attributename",pageContext.SESSION_SCOPE);  
  16.               Stringstr4=(String)pageContext.getAttribute("attributename",pageContext.APPLICATION_SCOPE);  
  17.         %>  
  18.   
  19.         attributename在不同范围内的取值:<br>  
  20.         <%="page范围:"+str1%><br>  
  21.         <%="request范围:"+str2%><br>  
  22.         <%="session范围:"+str3%><br>  
  23.         <%="application范围:"+str4%><br>  
  24.        </body>  
  25. </html>  


bubuko.com,布布扣

7page对象:指的是当前JSP页面的本身,它是java.lang.Object类的对象通过它可以方便的调用Servlet类中定义的方法。在实际开发中page并不常用。

8config对象:ServletConfig类的一个实例,在Servlet初始化时,可以通过configServlet传递信息。所传递的信息可以是属性名和属性值构成的名值对,也可以是通过ServletContext对象传递的服务器的相关信息。

使用并不多,只在编写Servlet时,当需要重载Servletinit()方法时才会用到config对象。

9exception对象:exception对象是java.lang.Throwable类的对象,用来处理页面的错误和异常。在使用JSP开发时,习惯做法是在一个页面中使用page指令的errorPage属性,让该属性指向一个专门用于异常处理的页面。

如果在JSP页面中有未捕获的异常,则会生成exception对象,然后将该exception对象传送到page指令中设置的异常处理页面中,在异常处理页面中对exception对象进行处理。在异常处理页面中需要将其page指令的isErrorPage属性设置为true才可以使用exception对象。

exceptiontest.jsp:

  1. <%@ page contentType="text/html;charset=GB2312" errorPage="exceptionobject.jsp"%>  
  2. <html>  
  3.        <head>  
  4.               <title>exception对象</title>  
  5.        </head>  
  6.        <body>  
  7.               发生错误位置:<br>  
  8.               <%  
  9.                      int a=5;  
  10.                      intb=0;  
  11.                %>  
  12.                输出结果=<%=(a/b) %>    //出错地方  
  13.        </body>  
  14. </html>  


exceptionobject.jsp:

  1. <%@ page contentType="text/html;charset=GB2312" isErrorPage="true"%>  
  2. <%@ page import="java.io.PrintStream" %>  
  3. <html>  
  4.        <head>  
  5.               <title>输出错误页面</title>  
  6.        </head>  
  7.        <body>  
  8.               <%=exception.getMessage()%>   <!-- 返回exception对象的异常信息 -->  
  9.               <!-- 打印异常的栈反向跟踪轨迹 -->  
  10.               <%  
  11.                      exception.printStackTrace(newjava.io.PrintWriter(out));  
  12.                %>  
  13.        </body>  
  14. </html>  


bubuko.com,布布扣

JSP内置对象

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/baolibin528/p/4119684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!