用JSP完成图书信息查询功能

    科技2025-08-06  19

    目录

    结构图DBUtil.java代码Book.java代码BookSearchServlet.java代码web.xmlBookTest.java代码search.jsp页面代码bookInfo.jsp页面代码 页面效果图

    结构图

    DBUtil.java代码

    package org.liubingfeng.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { // 1:加载驱动程序 static { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 2:获取链接 public static Connection getConn() { Connection conn = null; try { conn = DriverManager .getConnection( "jdbc:sqlserver://localhost:1433;databaseName=practice1006", "sa", "123456"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } // 3:资源关闭 public static void close(Connection conn, PreparedStatement ps, ResultSet rs) { try { if (conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (ps != null) ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if (rs != null) rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

    Book.java代码

    package org.liubingfeng.bean; public class Book { /** * @param args */ private Integer id; private String name; private String isbn; private Float price; private String author; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Book() { super(); // TODO Auto-generated constructor stub } public Book(String name, String isbn, Float price, String author) { super(); this.name = name; this.isbn = isbn; this.price = price; this.author = author; } public Book(Integer id, String name, String isbn, Float price, String author) { super(); this.id = id; this.name = name; this.isbn = isbn; this.price = price; this.author = author; } @Override public String toString() { return "<tr>" +"<td>"+id+"</td>" +"<td>"+name+"</td>" +"<td>"+isbn+"</td>" +"<td>"+price+"</td>" +"<td>"+author+"</td>" +"</tr>"; } }

    BookSearchServlet.java代码

    package org.liubingfeng.action; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.liubingfeng.bean.Book; import org.liubingfeng.test.BookTest; public class BookSearchServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String bookName = request.getParameter("bookName"); Book book = BookTest.getBookByName(bookName); request.setAttribute("book", book); request.getRequestDispatcher("bookInfo.jsp").forward(request, response); } }

    web.xml

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>BookSearchServlet</servlet-name> <servlet-class>org.liubingfeng.action.BookSearchServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>BookSearchServlet</servlet-name> <url-pattern>/bookSearch</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

    BookTest.java代码

    package org.liubingfeng.test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.liubingfeng.bean.Book; import org.liubingfeng.util.DBUtil; public class BookTest { /** * @param args */ public static Book getBookByName(String bookName) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; conn = DBUtil.getConn(); String sql = "select * from book where name = ?"; Book book = new Book(); try { ps = conn.prepareStatement(sql); ps.setString(1, bookName); rs = ps.executeQuery(); if (rs.next()) { book = new Book(); book.setId(rs.getInt("id")); book.setName(rs.getString("name")); book.setIsbn(rs.getString("isbn")); book.setPrice(rs.getFloat("price")); book.setAuthor(rs.getString("author")); } else { book = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DBUtil.close(conn, ps, rs); return book; } }

    search.jsp页面代码

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'search.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String msg = (String)request.getAttribute("msg"); if(msg==null) msg=""; %> <form action="bookSearch" method="post"> 图书名称:<input type="text" name="bookName" /><span style="color: red"><%=msg %></span><br> <input type="submit" value="查询" /> </form> </body> </html>

    bookInfo.jsp页面代码

    <%@page import="org.liubingfeng.bean.Book"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'bookInfo.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% Book book = (Book) request.getAttribute("book"); %> <h1>查询结果</h1> <table border="1" cellpadding="0" cellspacing="0"> <tr> <th class="id">id</th> <th class="name">书籍名称</th> <th class="isbn">ISBN</th> <th class="price">价格</th> <th class="author">作者</th> </tr> <% if (book != null) { out.println(book.toString() + "<br>"); } else { request.setAttribute("msg", "很抱歉,没有查询到该图书"); request.getRequestDispatcher("search.jsp").forward(request, response); } %> </table> </body> </html>

    页面效果图

    输入数据库内没有的图书书名

    输入数据库内以存储的图书书名

    Processed: 0.012, SQL: 9