JSP内置对象

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

<%@ pagecontentType="text/html; charset=GB2312" %>
<html>
       <head>
              <title>表单页</title>
       </head>
       <body>
       <form action="requestobject2.jsp"method="post">
       <p>用户名:<input type="text" name="username"/></p>
       <p>年龄:<input type="text" name="age"/></p>
       <input type="submit"value="提交" />
       </form>
       </body>
</html>


requestobject.jsp

<%@ pagecontentType="text/html; charset=GB2312" %>
<html>
       <head>
              <title>request 表单页1</title>
       </head>
       <body>
              <%
                     request.setCharacterEncoding("gb2312");
                     Stringusername=request.getParameter("username");
                     String_age=request.getParameter("age");
                     intage=Integer.parseInt(_age);
               %>
               用户名:<%=username %><br>
               年龄:<%=age %>
       </body>
</html>


requestobject2.jsp

<%@ pagecontentType="text/html; charset=GB2312" %>
<%@ pageimport="java.util.*" %>
<html>
       <head>
              <title>request 表单页2</title>
       </head>
       <body>
              <%
                     String param="";
                     request.setCharacterEncoding("gb2312");
                     Enumerationparams=request.getParameterNames(); //获取所有参数值 
              //循环输出所有参数值
               while(params.hasMoreElements()){
                    param=(String)params.nextElement();   //设置编码格式
                    //打印参数名
                    out.println("ParamName:"+param+"<br>");
                    //打印参数值
                    out.println("ParamValues:"+request.getParameter(param)+"<br>");
               }
               %>
       </body>
</html>


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

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

responseform.jsp:

<%@ pagecontentType="text/html; charset=GB2312" %>
<html>
       <head>
              <title>表单页</title>
       </head>
       <body>
       <form action="responseobject.jsp"method="post">
       <p>用户名:<input type="text" name="username"/></p>
       <p>国家:<input type="text" name="country"/></p>
       <input type="submit"value="提交" />
       </form>
       </body>
</html>


requestobject.jsp:

<%@ pagecontentType="text/html; charset=GB2312" %>
<html>
       <head>
              <title>response 表单页</title>
       </head>
       <body>
              <%
                     String str=null;
                     str=request.getParameter("username");
                     //判断用户名输入是否为空
                     if(str==null){
                            str="";
                     }
                     //使用ISO-8859-1字符集将str解码为字节序列,并将结果存储在字节数组中
                     byteb[]=str.getBytes("ISO-8859-1");
                     str=newString(b);    //将字节数组重组为字符串
                     if(str.equals("")){
                            response.sendRedirect("responseform.jsp");
                     }
                     else{
                            out.println("response示例");
                            out.println(str);
                     }
               %>
       </body>
</html>



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

outobject.jsp:

<%@ page contentType="text/html;charset=GB2312" %>
<html>
       <head>
              <title>out对象</title>
       </head>
       <body>
              <%
                     out.print("鲍礼彬");    //不换行
                     out.println("Hadoop");  //换行
                     //获取缓存区信息
               %>
               <br>
               <%
                    intallbuf=out.getBufferSize();   //获取缓存区大小
                    intremainbuf=out.getRemaining();  //获取剩余缓存区的大小
                    intusedbuf=allbuf-remainbuf;             //已用缓存区
                    out.println("已用缓存区大小:"+usedbuf);
                %>
       </body>
</html>


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

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

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

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

login.jsp:

<%@ page contentType="text/html;charset=GB2312" %>
<html>
       <head>
              <title>登陆页</title>
       </head>
       <body>
       <%
              Stringname="";
              //判断是否为新的session
              if(!session.isNew()){
                     name=(String)session.getAttribute("username");
                     //获取session中的username值
                     if(name==null){
                            name="";
                     }
              }
        %>
        <p>欢迎光临</p>
        <p>Session ID:<%=session.getId()%></p>
               <form action="check.jsp"method="post">
                     <p>用户名:<inputtype="text" name="username" value=<%=name%>></p>
                     <inputtype="submit" value="提交" />
              </form>
       </body>
</html>


check.jsp:

<%@ pagecontentType="text/html; charset=GB2312" %>
<html>
       <head>
              <title>进入邮箱</title>
       </head>
       <body>
              <%
                     String name=null;
                     name=request.getParameter("username");  //获取request中的username值
                     if(name!=null){
                            session.setAttribute("username",name);   //设置session的属性值
                     }
               %>
               <a href="login.jsp">登陆</a>     <ahref="loginout.jsp">注销</a>
               <br>
               <p>当前用户为:<%=name %></p>
               <p>邮箱中共有29封邮件</p>
       </body>
</html>


loginout.jsp:

<%@ pagecontentType="text/html; charset=GB2312" %>
<html>
       <head>
              <title>注销页面</title>
       </head>
       <body>
              <%
                     String     name=(String)session.getAttribute("username");  //获取request中的username值
                     session.invalidate();              //清空session
               %>
               <%=name %>,再见!
               <p><p>
               <a href="login.jsp">重新登陆</a>
       </body>
</html>



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

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

applicationobject.jsp:

<%@ page contentType="text/html;charset=GB2312" %>
<html>
       <head>
              <title>网站计数器</title>
       </head>
       <body>
              <%
                     Stringcount=(String)application.getAttribute("count"); //获取属性
                     if(count==null){
                            count="1";
                     }else{
                            count=Integer.parseInt(count)+1+"";
                     }
                     application.setAttribute("count",count);
               %>
               <%="<h1>到目前为止,网站访问人数为:"+count+"</h1><br>" %>
       </body>
</html>


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

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

pageContextobject.jsp:

<%@ page contentType="text/html;charset=GB2312" %>
<html>
       <head>
              <title>pageContext对象</title>
       </head>
       <body>
       <%
              pageContext.setAttribute("attributename","page_scope");
              request.setAttribute("attributename","request_scope");
              session.setAttribute("attributename","session_scope");
              application.setAttribute("attributename","application_scope");
             
              Stringstr1=(String)pageContext.getAttribute("attributename",pageContext.PAGE_SCOPE);
              Stringstr2=(String)pageContext.getAttribute("attributename",pageContext.REQUEST_SCOPE);
              Stringstr3=(String)pageContext.getAttribute("attributename",pageContext.SESSION_SCOPE);
              Stringstr4=(String)pageContext.getAttribute("attributename",pageContext.APPLICATION_SCOPE);
        %>

        attributename在不同范围内的取值:<br>
        <%="page范围:"+str1%><br>
        <%="request范围:"+str2%><br>
        <%="session范围:"+str3%><br>
        <%="application范围:"+str4%><br>
       </body>
</html>


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:

<%@ page contentType="text/html;charset=GB2312" errorPage="exceptionobject.jsp"%>
<html>
       <head>
              <title>exception对象</title>
       </head>
       <body>
              发生错误位置:<br>
              <%
                     int a=5;
                     intb=0;
               %>
               输出结果=<%=(a/b) %>    //出错地方
       </body>
</html>


exceptionobject.jsp:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值