JSP教程(四)—— JSP内置对象(上)

1  JSP内置对象

1.1  内置对象简介

JSP内置对象是Web容器创建的一组对象,不使用“new”关键字就可以使用的内置对象。

1.2  九大内置对象

常用的五类内置对象:out、request、response、session、application。

不常用的四类内置对象:page、pageContext、exception、config。

1.3  Web程序的请求响应模式

  • 用户发送请求(request)
  • 服务器给用户响应(response)

1.4  缓冲区

缓冲区:Buffer。

所谓的缓冲区就是内存的一块区域,用来保存临时数据。

2  4种属性范围

在JSP中提供了4种属性的保存范围。具体如下:

  • page(pageContext):只在一个页面中保存属性,跳转之后无效。
  • request:只再一次请求中保存,服务器跳转后依然有效。
  • session:在一次会话范围中,无论何种跳转都可以使用,但是新开浏览器无法使用。
  • application:在整个服务器上保存,所有用户都可以使用。

以上的4个内置对象都支持以下属性操作方法:

  • public void setAttribute(String name,Object o)  :设置属性的名称及内容
  • public Object getAttribute(String name)  :根据属性名称取得属性
  • public void removeAttribute(String name)  :删除指定的属性

一些理解:

  • request表示客户端的请求。正常情况下,一次请求副去污只会给予一次回应,那么这时如果是服务器端跳转,请求的地址栏没有改变,所以也就相当于回应了一次;而如果地址栏改变了,就相当于是发出了第二次请求,则第一次请求的内容肯定就已经消失了,所以无法取得。
  • 每一个新的浏览器连接上服务器后就是一个新的session。
  • application范围的属性设置过多会影响服务器的性能。

3  out对象

out对象:

  • out对象是JspWriter类的实例,是向客户端输出内容常用的对象。

常用方法:

  • void println() 向客户端打印字符串
  • void clear() 清除缓冲区的内容,如果在FLUSH之后调用会抛出异常
  • void clearBuffer() 清除缓冲区的内容,如果在FLUSH之后调用不会抛出异常
  • void flush() 将缓冲区内容输出到客户端
  • int getBufferSize() 返回缓冲区以字节数的大小,如不设缓冲区则为0
  • int getRemaining() 返回缓冲区还剩余多少可用
  • boolean isAutoFlush() 返回缓冲区满时,是自动清空还是抛出异常
  • void close() 关闭输出流

实例1

 1 <body>
 2     <h1>OUT内置对象</h1>
 3     <%
 4         out.println("<h2>静夜思</h2>");
 5         out.println("床前明月光<br>");
 6         out.println("疑是地上霜<br>");
 7         out.println("举头望明月<br>");
 8         out.println("低头思故乡<br>");
 9     %>
10     缓冲区大小:<%= out.getBufferSize() %>byte<br>
11     缓冲区剩余大小:<%= out.getRemaining() %>byte<br>
12     是否清除缓冲区:<%= out.isAutoFlush() %><br>
13     <br>
14 </body>

输出结果:

实例2

 1 <body>
 2     <h1>OUT内置对象</h1>
 3     <%
 4         out.println("<h2>静夜思</h2>");
 5         out.println("床前明月光<br>");
 6         out.println("疑是地上霜<br>");
 7         out.flush();
 8         out.clear();//此处将抛出异常
 9         //out.clearBuffer();//不会抛出异常
10         out.println("举头望明月<br>");
11         out.println("低头思故乡<br>");
12     %>
13     缓冲区大小:<%= out.getBufferSize() %>byte<br>
14     缓冲区剩余大小:<%= out.getRemaining() %>byte<br>
15     是否清除缓冲区:<%= out.isAutoFlush() %><br>
16     <br>
17 </body>

输出结果:

(只会输出前两句)

4  get与post提交方式的区别(表单提交方式)

1 <form name="regForm" action="动作" method="提交方式"> 
2 </form>

表单有两种提交方式:get和post

  1. get:以明文的方式通过URL提交数据,数据在URL中可以看到。提交的数据最多不超过2KB。安全性较低但效率比post方式高。适合提交数据量不大,安全性不高的数据。比如:搜索、查询等功能。
  2. post:将用户提交的信息封装在HTML HEADER内。适合提交数据量大,安全性高的用户信息。比如:注册、修改、上传等功能。

get && post 方法提交数据:

 1 <body>
 2     <h1>用户登录</h1>
 3     <hr>
 4     <form action="dologin.jsp" name="loginForm" method="get">
 5     <!-- <form action="dologin.jsp" name="loginForm" method="post">  !-->
 6         <table>
 7             <tr>
 8                 <td>用户名</td>
 9                 <td><input type="text" name="username"/></td>
10             </tr>
11             <tr>
12                 <td>密码</td>
13                 <td><input type="password" name="password"/></td>
14             </tr>
15             <tr>
16                 <td colspan="2"><input type="submit" value="登录"></td>
17             </tr>
18         </table>
19     </form>
20 </body>
1 <body>
2     <h1>登录成功</h1>
3     <hr>
4 </body>

get方法运行结果:

(不安全)

post方法运行结果:

 

(安全)

5  request对象

request对象

  • 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequset类的实例。request对象具有请求域,即完成客户端的请求之前,该对象一直有效。

常用方法:

  • String getParameter(String name)返回name指定参数的参数值
  • String[] getParameterValues(String name)返回包含参数name的所有值的数组
  • void setAttributr(String,Object)存储此请求中的属性
  • object getAttribute(String name)返回指定属性的属性值
  • String getContentType()得到请求体MIME类型
  • String getProtocol()返回请求用的协议类型及版本号
  • String getServerName()返回接受请求的服务器主机名
  • int getServerPort()返回服务器接受此请求所用的端口号
  • StringCharacterEncoding()返回字符编码方式
  • void setCharacterEncoding()设置请求的字符编发方式
  • int getContentLength()返回请求体的长度(以字节数)
  • String getRemoteAddr()返回发送此请求的客户机IP地址
  • String getRealPath(String path)返回一虚拟路径的真实路径
  • String request.getContextPath()返回上下文路径

接收乱码解决

现阶段的开发中最好每一格JSP页面都写上编码设置。

1 request.setCharacterEncoding("utf-8");

实例1(按钮提交对象)

 1 <body>
 2     <h1>用户注册</h1>
 3     <hr>
 4     <form action="index.jsp" name="regForm" method="post">
 5         <table>
 6             <tr>
 7                 <td>用户名:</td>
 8                 <td><input type="text" name="username"/></td>
 9             </tr>
10             <tr>
11                 <td>爱好:</td>
12                 <td>
13                     <input type="checkbox" name="favorite" value="read">读书
14                     <input type="checkbox" name="favorite" value="music">音乐
15                     <input type="checkbox" name="favorite" value="movie">电影
16                     <input type="checkbox" name="favorite" value="internet">上网
17                 </td>
18             </tr>
19             <tr>
20                 <td colspan="2"><input type="submit" value="提交"></td>
21             </tr>
22         </table>
23     </form>
24 </body> 
 1 <body>
 2     <h1>REQUEST内置对象</h1>
 3     <% 
 4         request.setCharacterEncoding("utf-8");//解决中文乱码问题,但无法解决URL传递参数是中文的情况
 5     %>
 6     用户名:<%= request.getParameter("username") %><br>//单一的参数都可以使用getParameter()接收
 7     爱好:<%
 8         String[] favorites = request.getParameterValues("favorite");//而一组参数需要使用getParameterValues()接收
 9         for(int i=0;i<favorites.length;i++){
10             out.println(favorites[i]+"  ");
11         }   
12     %>
13 </body>

运行结果:

实例2(通过URL提交对象)

 1 <body>
 2     <h1>用户注册</h1>
 3     <hr>
 4     <form action="index.jsp" name="regForm" method="post">
 5         <table>
 6             <tr>
 7                 <td>用户名:</td>
 8                 <td><input type="text" name="username"/></td>
 9             </tr>
10             <tr>
11                 <td>爱好:</td>
12                 <td>
13                     <input type="checkbox" name="favorite" value="read">读书
14                     <input type="checkbox" name="favorite" value="music">音乐
15                     <input type="checkbox" name="favorite" value="movie">电影
16                     <input type="checkbox" name="favorite" value="internet">上网
17                 </td>
18             </tr>
19             <tr>
20                 <td colspan="2"><input type="submit" value="提交"></td>
21             </tr>
22         </table>
23     </form>
24     <br>
25     <br>
26     <a href="index.jsp?username=QiJunhui">测试URL传参数</a>
27 </body>
 1 <body>
 2     <h1>REQUEST内置对象</h1>
 3     <% 
 4         request.setCharacterEncoding("utf-8");//解决中文乱码问题,但无法解决URL传递参数是中文的情况
 5     %>
 6     用户名:<%= request.getParameter("username") %><br>
 7     爱好:<%
 8         if(request.getParameterValues("favorite")!=null){
 9             String[] favorites = request.getParameterValues("favorite");
10             for(int i=0;i<favorites.length;i++){
11                 out.println(favorites[i]+"  ");
12             }
13         }
14     %>
15 </body>

运行结果:

(中文会出现乱码)

【注】URL重写地址

1 动态页面地址?参数名称1=参数内容1&参数名称2=参数内容2&...

【注】URL传递参数中文乱码的解决方法:

  • 打开%:\Tomcat\conf\server.xml文件,添加如下内容,重启Tomcat服务器:

实例3(getParameter方法)

 1 <body>
 2     <h1>用户注册</h1>
 3     <hr>
 4     <form action="index.jsp" name="regForm" method="post">
 5         <table>
 6             <tr>
 7                 <td>用户名:</td>
 8                 <td><input type="text" name="username"/></td>
 9             </tr>
10             <tr>
11                 <td>爱好:</td>
12                 <td>
13                     <input type="checkbox" name="favorite" value="read">读书
14                     <input type="checkbox" name="favorite" value="music">音乐
15                     <input type="checkbox" name="favorite" value="movie">电影
16                     <input type="checkbox" name="favorite" value="internet">上网
17                 </td>
18             </tr>
19             <tr>
20                 <td colspan="2"><input type="submit" value="提交"></td>
21             </tr>
22         </table>
23     </form>
24     <br>
25     <br>
26     <a href="index.jsp?username=QiJunhui">测试URL传参数</a>
27 </body>
 1 <body>
 2     <h1>REQUEST内置对象</h1>
 3     <% 
 4         request.setCharacterEncoding("utf-8");//解决中文乱码问题,但无法解决URL传递参数是中文的情况
 5         request.setAttribute("password", 123456);
 6     %>
 7     用户名:<%= request.getParameter("username") %><br>
 8     爱好:<%
 9         if(request.getParameterValues("favorite")!=null){
10             String[] favorites = request.getParameterValues("favorite");
11             for(int i=0;i<favorites.length;i++){
12                 out.println(favorites[i]+"  ");
13             }
14         }
15     %>
16     密码:<%= request.getAttribute("password") %><br>
17 </body>

运行结果:

实例4(其他request方法)

 1 <body>
 2     <h1>用户注册</h1>
 3     <hr>
 4     <form action="index.jsp" name="regForm" method="post">
 5         <table>
 6             <tr>
 7                 <td>用户名:</td>
 8                 <td><input type="text" name="username"/></td>
 9             </tr>
10             <tr>
11                 <td>爱好:</td>
12                 <td>
13                     <input type="checkbox" name="favorite" value="read">读书
14                     <input type="checkbox" name="favorite" value="music">音乐
15                     <input type="checkbox" name="favorite" value="movie">电影
16                     <input type="checkbox" name="favorite" value="internet">上网
17                 </td>
18             </tr>
19             <tr>
20                 <td colspan="2"><input type="submit" value="提交"></td>
21             </tr>
22         </table>
23     </form>
24     <br>
25     <br>
26     <a href="index.jsp?username=QiJunhui">测试URL传参数</a>
27 </body>
 1 <body>
 2     <h1>REQUEST内置对象</h1>
 3     <% 
 4         request.setCharacterEncoding("utf-8");//解决中文乱码问题,但无法解决URL传递参数是中文的情况
 5         request.setAttribute("password", 123456);
 6     %>
 7     用户名:<%= request.getParameter("username") %><br>
 8     爱好:<%
 9         if(request.getParameterValues("favorite")!=null){
10             String[] favorites = request.getParameterValues("favorite");
11             for(int i=0;i<favorites.length;i++){
12                 out.println(favorites[i]+"  ");
13             }
14         }
15     %>
16     密码:<%= request.getAttribute("password") %><br>
17     请求体的MTME类型:<%= request.getContentType() %><br>
18     协议类型及版本号:<%= request.getProtocol() %><br> 
19     服务器主机名:<%= request.getServerName() %><br>
20     服务器端口号:<%= request.getServerPort() %><br>
21     请求文件的长度:<%= request.getContentLength() %><br>
22     请求客户端的IP地址:<%= request.getRemoteAddr() %><br>
23     请求的真实路径:<%= request.getRealPath("index.jsp") %><br>
24     请求的上下文路径:<%= request.getContextPath() %><br>
25 </body>

运行结果:

【注意】在进行表单参数接收时,如果用户没有输入文本框内容或者没有选择复选框内容,那么在使用getParameter()和getParameterValues()接收参数时,返回的内容是null,此时就有可能产生NullPointerExceotion,所以在使用时最好判断接收来的参数是否为null。

6  response对象

response对象

  • response对象包含了响应客户请求的有关信息,但在JSP中很少直接用到它。它是HttpServlrtResponse类的实例。response对象具有页面作用域,即访问一个页面时,该页面的response对象只能对这次访问有效,其它页面的response对象对当前页面无效。

常用方法:

  • String getCharcterEncoding()返回响应用的是何种字符编码
  • void setContentType(String type)设置响应的MIME类型
  • PrintWriter getWriter()返回可以向客户端输出字符的一个对象(注意比较:PrintWriter与内置out对象的区别
  • sendRedirect(java.lang.String location)重新定向客户端的请求

实例1

 1 <%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"%>
 2 <%
 3     response.setContentType("text/html;charset=utf-8");
 4     
 5     out.println("<h1>response内置方法</h1>");
 6     out.println("<hr>");
 7     //out.flush();//加此语句,让out对象提前输出与getWriter()
 8     
 9     PrintWriter outer = response.getWriter();//获取输出流对象
10     outer.println("大家好,我是response对象生成的输出流outer对象");//默认状态下,此输出提前于OUT对象
11     //response.sendRedirect("YHZC.jsp");//请求重定向,打开此页面,将直接挑战至别的页面
12 %>

运行结果:

请求转发与请求重定向

  • 请求重定向:客户端行为,response.sendRedirect(),从本质上讲等同于两次请求,前一次的请求对象不会保存,地址栏的URL地址会改变。
  • 请求转发:服务器行为,request.getRequestDispatcher().forward(req,resp);是一次请求,转发后请求对象会保存,地址栏的URL地址不会改变。

实例:

  1. 假设你去办理护照
  2. 重定向:你先去了A局,A局的人说:“这个是不归我们管,去B局”,然后,你就从A退了出来,自己乘车去了B局。
  3. 转发:你去了A局,A局看了以后,知道这个事情其实应该B局来管,但是他没有把你退回来,而是让你做一会儿,自己到后面办公室联系了B得人,让他们办好后,送了过来。
posted @ 2018-02-19 15:00  祁俊辉  阅读(364)  评论(0编辑  收藏  举报