1 / 26

第八章

第八章. JavaBean 和 JSP 中 的标准动作. 回顾. JSP 隐式对象是预定义的类,可被嵌入 JSP 表达式和 Scriplet 隐式对象通过 JSP 容器导入到 JSP 页面中 request 、 response 和 out 对象等输入和输出对象用于显示和检索网页中的信息 session 、 application 和 pageContext 等作用域通信和控制对象向 JSP 页面提供页面环境的访问权 page 对象用于表示 Servlet , 而 config 对象用于存储 Servlet 的初始化参数. 目标.

pia
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. 第八章 JavaBean 和 JSP 中 的标准动作

  2. 回顾 • JSP隐式对象是预定义的类,可被嵌入 JSP 表达式和 Scriplet • 隐式对象通过 JSP 容器导入到 JSP 页面中 • request、response和 out 对象等输入和输出对象用于显示和检索网页中的信息 • session、application和 pageContext 等作用域通信和控制对象向 JSP 页面提供页面环境的访问权 • page对象用于表示 Servlet, 而 config对象用于存储 Servlet的初始化参数

  3. 目标 • 运用 JavaBean • 运用 JSP 标准动作

  4. JavaBean • JavaBean 是可重用组件 JavaBean的优点 可重用组件 可在多个应用程序中使用 可以跨平台

  5. JSP页面查询数据 查询数据: <% Connection conn = null; Statement stmt = null; ResultSet rs = null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:accp", "", ""); stmt = conn.createStatement(); rs = stmt.executeQuery("select * from Customers"); while (rs.next()) { out.print(rs.getString(1)); out.print(rs.getString(2)); } rs.close(); stmt.close(); conn.close(); %>

  6. JSP页面添加数据 添加数据: <% Connection conn = null; Statement stmt = null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:accp", "", ""); stmt = conn.createStatement(); stmt.executeUpdate("insert into Customers(customerID,CompanyName) values('accp','JADE BIRD')"); stmt.close(); conn.close(); %>

  7. JavaBean package accp ; import java.sql.*; public class Conn { Connection conn = null ; public Connection getConn(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn= DriverManager.getConnection("jdbc:odbc:accp","",""); }catch(Exception e){ System.out.println(e.printStackTrace()); } return conn ; } }

  8. JSP 使用JavaBean • 标准动作用于: • 将 JavaBean 嵌入 JSP 页面 • 设置和获取 JavaBean 的属性 • 将用户请求转发给其他页面 • 将其他页面的内容嵌入当前页面 • 标准动作可以为空标签及容器标签 • 标准动作中的属性区分大小写 • JSP 中的标准动作使用 <jsp>作为前缀 JSP 页面 JavaBean JSP 可使用 JSP 标准动作调用 JavaBean 组件并访问属性

  9. useBean动作 2-1 ... <head> <jsp:useBean id="BeanID" class="MyBean" scope="page"/> </head> ... 创建引用并嵌入现有的 Bean id 属性创建对 class属性中所述类的引用

  10. useBean 动作 2-2 • 范围指定 JavaBean 在 JSP 页面中的可用性。各种范围 选项如下:

  11. Get()和Set()方法 2-1 • get() 和 set() 方法用于访问 JavaBean 的属性 访问 JavaBean的属性 JavaBean 的属性 Get()方法 Set()方法 定义了公有方法 定义了公有方法 Get()方法返回值 Set()方法给属性赋值

  12. Get()和Set()方法 2-2 • import java.io.*; • public class MyBean • { • private String name; • public MyBean() • { • } • public String getName() • { • return name; • } • public void setName(String myname) • { • name = myname; • } • } 将 name属性声明为私有属性 不带参数的默认构造函数 get() 方法返回一个值 Set() 方法设置一个值

  13. JSP 标准动作 • JSP 标准动作包括: JSP 标准动作 <jsp:useBean> <jsp:getProperty> <jsp:include> <jsp:forward> <jsp:setProperty>

  14. setProperty动作 ... <head> <jsp:useBean id="BeanID" class="MyBean" scope="session"/> <jsp:setProperty name=“BeanID” property=“name” value=“示例"/> </head> ... name属性指定对 JavaBean 类的引用 设置 JavaBean 在 JSP 页面中的属性 指定 JavaBean的属性名称

  15. getProperty动作 ... <head> <jsp:useBean id="BeanID" class="MyBean" scope="page"/> <jsp:setProperty name="BeanID" property=“name” value=“示例"/> </head> <body> <jsp:getProperty name="BeanID" property="name"/> </body> ... Name 属性指定对 JavaBean类的引用 Property属性指定 JavaBean的属性名称 获取 JavaBean中指定的属性的值

  16. 创建 JavaBean • 在 JavaBean中使用 get() 和 set()方法 public class simpleBean { private String name = null; private int age = 0; public simpleBean() { } public String getName() { return name; } public void setName(String username) { name = username; } }

  17. 在JSP中使用 JavaBean <html> <head> <title> 在 JSP 中使用 JavaBean </title> <jsp:useBean id="BeanId" class="example5.simpleBean" scope="application"/> <jsp:setProperty name="BeanId" property="name" value="Andrew"/> <jsp:setProperty name="BeanId" property="age" value="25"/> </head> <body> 姓名:<jsp:getProperty name="BeanId" property="name"/> <br/> 年龄:<jsp:getProperty name="BeanId" property="age"/> </body> </html> • 使用 useBean、setProperty和 getProperty 动作 演示:示例 5

  18. Forward 动作 3-1 ... <body> <jsp:forward page="Second.jsp"/> </body> ... page 属性指定其他页面的地址 将用户的请求转发给其他 JSP 页面

  19. Forward 动作 3-2 • 创建一个 JSP 页面 • 使用 forward 动作转发用户的请求 <html> <head> <title> 转发此页面 </title> </head> <body> <jsp:forward page="requestedpage.jsp"/> </body> </html> 演示:示例 7

  20. Forward 动作 3-3 • 创建一个 JSP 页面 • 用户将被转至此页面 <html> <head> <title>将请求转发到此处</title> </head> <body> 此页面从 <br /> <b>forward.jsp</b>中收到一个转发的请求, <br>此页面是 <b>requestedpage.jsp</b>中的输出结果, <br>但 URL 用于 <b>forward.jsp</b> </body> </html> 演示:示例8

  21. Include动作 5-1 ... <body> <jsp:include page="OtherPage.jsp" flush="true"/> </body> ... 将其他 JSP 中的内容或页面嵌入到指定的页面 page 属性指定其他页面的地址

  22. Include 动作 5-2 • 创建一个 JSP 页面,用以接受参数并显示结果 • 使用 getParameter()方法 . . . <% String tablename=request.getParameter("tablename"); String sqlquery=request.getParameter("sqlquery"); String whereclause=request.getParameter("producttype"); out.println(“<b>以执行 Sql 查询: </b>"+sqlquery+" "+ tablename+" where ProductType='"+whereclause+"'<br/><br/>"); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:accp","",""); PreparedStatement s=con.prepareStatement(sqlquery+" "+ tablename+" where CustomerID='"+whereclause+"'"); ResultSet rs=s.executeQuery(); while(rs.next()) { out.println(" "+rs.getString(1)+" "+rs.getString(2)+" $"+rs.getString(4)+"<br/>"); } } %> . . . 示例:示例 9

  23. Include 动作 5-3 • 创建一个 JSP 页面,用以嵌入另一个 JSP 页面,并为嵌入的页面设置参数 • 使用 include 动作和 param元素方法 <body> <jsp:include page="includeFileNeedingAParameter.jsp" flush="true"> <jsp:param name="tablename" value=“Customers"/> <jsp:param name="sqlquery" value="Select * from"/> <jsp:param name=“producttype” value=“ANATR"/> </jsp:include> 以上文本来自 includeFileNeedingAParameter.jsp. </body> 演示:示例 10

  24. Include 动作 5-4 • 创建一个 HTML 页面 • 该页面将被嵌入示例 6 <html> <head> <title>欢迎您来到汽车专柜</title> </head> <body> <h1 align="center">欢迎您来到汽车专柜</h1> </body> </html> 演示:示例 11

  25. Include 动作 5-5 • 创建一个 JSP 页面 • 使用 include 动作嵌入 HTML 页面 <html> <head> <title>使用 include 动作</title> </head> <body> <jsp:include page="welcome.html" flush="true"/> 此文本将在显示完 <b>welcome.html</b>的内容后显示 </body> </html> 演示:示例 12

  26. 总结 • JavaBean是可在多个应用程序中重复使用的组件 • JSP标准动作用于插入文件、Bean 组件以及将用户转至其他网页 • forward动作用于将用户的请求转发给其他页面 • include 动作用于将一个页面嵌入当前 JSP 页面

More Related