以数据库中查找图书信息,并将信息显示在jsp页面当中为例,下面贴上代码片段:
(1)在index.jsp页面代码body中只要添加如下一段代码:
<a href="FindServlet">查看所有图书</a>
(2)FindServlet.java类代码
1 package com.lyq.bean; 2 import java.io.IOException; 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import java.util.ArrayList; 9 import java.util.List; 10 import javax.servlet.ServletException; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 public class FindServlet extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 try { 19 // 加载数据库驱动,注册到驱动管理器 20 Class.forName("com.mysql.jdbc.Driver"); 21 // 数据库连接字符串 22 String url = "jdbc:mysql://localhost:3306/db_database10"; 23 // 数据库用户名 24 String username = "root"; 25 // 数据库密码 26 String password = "111"; 27 // 创建Connection连接 28 Connection conn = DriverManager.getConnection(url,username,password); 29 // 获取Statement 30 Statement stmt = conn.createStatement(); 31 // 添加图书信息的SQL语句 32 String sql = "select * from tb_books"; 33 // 执行查询 34 ResultSet rs = stmt.executeQuery(sql); 35 // 实例化List对象 36 List<Book> list = new ArrayList<Book>(); 37 // 判断光标向后移动,并判断是否有效 38 while(rs.next()){ 39 // 实例化Book对象 40 Book book = new Book(); 41 // 对id属性赋值 42 book.setId(rs.getInt("id")); 43 // 对name属性赋值 44 book.setName(rs.getString("name")); 45 // 对price属性赋值 46 book.setPrice(rs.getDouble("price")); 47 // 对bookCount属性赋值 48 book.setBookCount(rs.getInt("bookCount")); 49 // 对author属性赋值 50 book.setAuthor(rs.getString("author")); 51 // 将图书对象添加到集合中 52 list.add(book); 53 } 54 // 将图书集合放置到request之中 55 request.setAttribute("list", list); 56 rs.close(); // 关闭ResultSet 57 stmt.close(); // 关闭Statement 58 conn.close(); // 关闭Connection 59 } catch (ClassNotFoundException e) { 60 e.printStackTrace(); 61 } catch (SQLException e) { 62 e.printStackTrace(); 63 } 64 // 请求转发到book_list.jsp request.getRequestDispatcher("book_list.jsp").forward(request, response); 65 } 66 }
(3)图书信息展示页面book_list.jsp代码
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.List" %> 4 <%@ page import="com.java.Book" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>查看所有图书</title> 10 <style type="text/css"> 11 h2{ 12 margin:0px; 13 } 14 div,td{ 15 font-size:12px; 16 } 17 </style> 18 </head> 19 <body> 20 <div style="text-align:center;"> 21 <table width="45px" border="1px" height="180px"> 22 <tr bgcolor="white"> 23 <td colspan="4"> 24 <h2>所有图书信息</h2> 25 </td> 26 </tr> 27 <tr bgcolor="#e1ffc1"> 28 <td><b>图书名称</b></td> 29 <td><b>图书价格</b></td> 30 <td><b>图书数量</b></td> 31 <td><b>图书作者</b></td> 32 </tr> 33 <% 34 //获取图书信息集合 35 List<Book> list = (List<Book>)request.getAttribute("list"); 36 //判断集合是否有效 37 if(list == null || list.size() < 1){ 38 out.print("没有数据"); 39 }else{ 40 //遍历图书集合中的数据 41 for(Book book:list){ 42 %> 43 <tr bgcolor="white"> 44 <td><%= book.getName() %></td> 45 <td><%= book.getPrice() %></td> 46 <td><%= book.getBookcout() %></td> 47 <td><%= book.getAuthor() %></td> 48 </tr> 49 <% 50 } 51 } 52 %> 53 </table> 54 </div> 55 </body> 56 </html>
(4)最后web.xml中Servlet的配置信息为:
1 <servlet> 2 <servlet-name>FindServlet</servlet-name> 3 <servlet-class>com.java.FindServlet</servlet-class> 4 </servlet> 5 <servlet-mapping> 6 <servlet-name>FindServlet</servlet-name> 7 <url-pattern>/FindServlet</url-pattern> 8 </servlet-mapping>