1 / 29

第七章

第七章. JSP 隐式对象. 回顾. JSP 脚本元素包括: JSP 表达式、 JSP S criptlet 和 JSP 声明 JSP 表达式包括在 <% =... %> 标签内, JSP S criptlet 包括在 <%...%> 内, JSP 声明包括在 <%!...%> 内 JSP 指令有 page 、 include 和 taglib 指令 JSP 指令包括在 <%@ 和 %> 标签内

otis
Download Presentation

第七章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第七章 JSP 隐式对象

  2. 回顾 • JSP 脚本元素包括: JSP 表达式、JSP Scriptlet和 JSP声明 • JSP 表达式包括在 <%=... %> 标签内, JSP Scriptlet包括在<%...%>内, JSP声明包括在<%!...%>内 • JSP 指令有 page、include和 taglib指令 • JSP 指令包括在 <%@和 %> 标签内 • page 指令用于设置 JSP页面的属性;include 属性用于将其他文件嵌入 JSP页面;Taglib指令允许用户创建自定义标签

  3. 目标 • 了解隐式对象的分类及组成 • 运用输入/输出对象:request、response 和 out进行JSP编程 • 运用作用域通信对象:session、application和pageContext进行JSP编程 • 理解 Servlet 相关对象:page 和 config • 理解错误对象:exception

  4. 作用域通信对象 Servlet 对象 错误对象 request response out session pageContext application page config exception 隐式对象 2-1 • JSP 隐式对象是 Web 容器加载的一组类 • 它不像一般的Java对象那样用”new”去获取实例,而是可以直接在JSP页面使用的对象 • 隐式对象的名称是 JSP 的保留字 • JSP 使用 Java 定义的隐式对象来访问网页的动态内容 隐式对象 输入/输出对象

  5. JSP 页面 请求 请求 客户端 响应 响应 服务器 隐式对象 2-2 隐式对象

  6. 输入/输出对象 • 控制页面的输入和输出 • 访问与所有请求和响应有关的数据 • 输入和输出对象包括request、response 和 out

  7. 服务器 客户端 JSP 页面 请求信息 请求 request 对象 4-1 • 表示客户端对网页的请求 • 实现 javax.servlet.http.HttpServletRequest接口 • 使用 HTTP 协议处理客户端的请求 表示并存储客户端的请求 request 对象

  8. request 对象 4-2 • request对象常用方法 • String getParameter(String name) 根据页面表单组件名称获取请求页面提交数据 • String[ ] getParameterValues (String name) 获取页面请求中一个表单组件对应多个值时的用户的请求数据

  9. request 对象 4-3 • 使用request对象进行JSP编程 需求:某网站注册时,需要输入注册信息,请编写JSP页面供用户输入,并获取用户输入的数据。注册信息包括用户名、密码、确认密码、性别 、学历和你从哪里知道本网站等主题。 分析: 1、编写一个JSP页面,提供用户输入的表单组件。 2、编写另一个JSP页面,获取用户提交的请求数据,并 显示出来。

  10. request 对象 4-4 • 使用request对象进行JSP编程 <%! public String formatStr(String str){ return str==null?"":str; } %> <% String name = formatStr(request.getParameter("name")); String pwd = formatStr(request.getParameter("pwd")); String confirm_pwd = formatStr(request.getParameter("confirm_pwd")); String gender = formatStr(request.getParameter("gender")); String xueli = formatStr(request.getParameter("xueli")); String[] channels = request.getParameterValues("channel"); %> request对象 演示:示例 1

  11. 服务器 客户端 从服务器中检索的信息 JSP 页面 响应 response 对象 3-1 • 处理 JSP 生成的响应 • 将响应发送给客户端 • 实现javax.servlet.http.HttpServletResponse 接口 • 使用 HTTP 协议将响应发送给客户端 存储信息,以在响应客户端 请求时发送此信息 Response 对象

  12. response 对象 3-2 • response 对象常用方法 • void setContentType (String name) 设置作为响应生成的内容的类型和字符编码 • void sendRedirect (String name) 发送一个响应给浏览器,指示其应请求另一个URL

  13. response 对象 3-3 • 使用response对象进行JSP编程 <%@ page contentType="text/html; charset=GBK" %> <html> <head> <title>response对象</title> </head> <body> <% response.setContentType("text/html;charset=GBK"); response.sendRedirect("response2.jsp"); %> </body> </html> 使用response对象 重定向 演示:示例 2

  14. 服务器 客户端 JSP 页面 创建输出流 请求 在网页上显示输出流 输出流 out 对象 2-1 • 表示输出流 • javax.servlet.jsp.JspWriter 类的实例 • 使用 write()、print() 和 println() 方法 创建输出流以显示信息 out 对象

  15. out 对象 2-2 • 使用out对象进行JSP编程 使用out对象输出 <html> <body> <% out.print(“欢迎来到 JSP:隐式对象”) ; out.write(“欢迎来到 JSP:隐式对象”) ; %> … </body> </html> 演示:示例 3

  16. 作用域通信对象 • 隐式对象的作用域包括page、request、session 和 application • 作用域通信对象包括 session、application 和 pageContext

  17. session 对象 2-1 • Web 服务器为单个用户发送的多个请求创建会话 • 存储有关用户会话的所有信息 • javax.servlet.http.HttpSession 接口的实例 • session 对象最常用的方法有: • void setAttribute(String name,Object value) • 以键/值的方式,将一个对象的值存放到session 中 • void getAttribute(String name) • 根据名称去获取session中存放对象的值

  18. session 对象 2-2 <% session.setAttribute(“name”, “admin”); %> <% if(session.getAttribute(“name”)!=null){ String name = (String) session.setAttribute(“name”); } %>

  19. Application对象 2-1 • 表示 JSP 页面所属的应用程序 • 应用程序的 JSP 页面组合起来形成一个应用程序 • javax.servlet.ServletContext接口实例 • application对象最常用的方法有: • void setAttribute(String name,Object value) 以键/值的方式,将一个对象的值存放到application中 • void getAttribute(String name) 根据名称去获取application中存放对象的值

  20. application 对象 2-2 <% application.setAttribute(“ChartRecord”, “hihi”); %> <% if(application.getAttribute(“ChartRecord”)!=null){ String chartRecord = (String) application.getAttribute(“ChartRecord”); } %>

  21. pageContext对象 2-1 • 使用户可以访问页面作用域中定义的所有隐式对象 • 它的作用范围是在同一页面 • javax.servlet.jsp.PageContext 类的实例

  22. pageContext对象 2-2 • pageContext对象最常用的方法有: • void setAttribute(String name,Object value) 以键/值的方式,将一个对象的值存放到pageContext中 • void getAttribute(String name) 根据名称去获取pageContext中存放对象的值

  23. 作用域通信对象的使用 pageContext 对象 <html> <body> <h1>session、application和 pageContext</h1> <% Integer count = (Integer)pageContext.getAttribute("pageCount"); pageContext.setAttribute("pageCount", new Integer(count.intValue()+1)); Integer count2 = (Integer)session.getAttribute("sessionCount"); session.setAttribute("sessionCount",new Integer(count2.intValue()+1)); Integer count3 = (Integer)application.getAttribute("appCount"); application.setAttribute("appCount",new Integer(count3.intValue()+1)); %> 对象使用了 getAttribute()方法 <%@ page language="java" %> <% if (pageContext.getAttribute("pageCount")==null) { pageContext.setAttribute("pageCount", new Integer(0)); } if (session.getAttribute("sessionCount")==null) { session.setAttribute("sessionCount",new Integer(0)); } if (application.getAttribute("appCount")==null) { application.setAttribute("appCount",new Integer(0)); } %> 对象使用了 setAttribute()方法 session 对象 <b>页面计数= </b> <%=pageContext.getAttribute("pageCount")%> <br/><b>会话计数= </b> <%=session.getAttribute("sessionCount")%> <br/><b>应用程序计数= </b> <%=application.getAttribute("appCount")%> <br/><b>时间= </b> <%=newjava.sql.Time(System.currentTimeMillis()) %> </body> </html> application 对象 演示:示例 3

  24. Servlet 对象 • JSP 引擎为每个 JSP 生成一个 Servlet • Servlet对象提供了访问 Servlet信息的方法和变量 • Servlet 对象包括 • page • config

  25. page 对象 • 使用 page 对象可以访问 Servlet 类的所有变量和方法 • java.lang.Object类的一个实例 <% @ page info=”我的信息” contentType=”text/html;charset=GBK”%> <html> <body> <%=((javax.servlet.jsp.HttpJspPage)page).getServletInfo()%> </body> </html>

  26. config 对象 • 存储在编译 JSP 页面的过程中创建的 Servlet 的信息 • javax.servlet.ServletConfig 接口的实例 • 提供了检索 Servlet 初始化参数的方法 config对象 获取初始化参数 String propertyFile =(String)config.getInitParameter(”PropertyFile”);

  27. Exception对象 2-1 • exception对象用于处理 JSP 页面中的错误 • exception 对象用于访问执行 JSP 的过程中引发的异常 • exception 对象是 java.lang.Throwable 类的实例 <%@ page isErrorPage="true" %> <html> <head> <title>隐式对象</title> </head> <body> <h1>隐式对象:异常</h1> 已检测到以下错误:br> <b><%= exception %></b><br> <% exception.printStackTrace(out); %> </body> <html> 指定网页中发生的错误

  28. Exception对象 2-2 指定当前页面为错误页面 <%@ page isErrorPage="true" %> <html> <head><title> 处理错误 </title></head> <body> <% if ( exception != null ) { out.write(“\n 发生错误。\n"); } Else { out.write(“\n 您已访问此页面, 但是没有可用的错误信息\n"); } %> </body> </html> 验证错误 在发生错误时显示消息 在未发生错误时显示消息

  29. 总结 • JSP隐式对象一般分为输入/输出对象、作用域通信对象 、Servlet 对象 和错误对象四类 • JSP 中的隐式对象包括 page、config、request、response、out、session、application、pageContext 和 exception • JSP 定义的作用域包括 page、request、session 和 application

More Related