声明:本文转自http://blog.csdn.net/mpfly/article/details/70195671点击打开链接
servlet文件
- package com.mi.servlet;
- 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 com.mi.beans.*;
- import java.sql.*;
- import java.util.ArrayList;
- public class List_book extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- Connection conn = null;
- Statement sta = null;
- ResultSet res = null;
- conn = DBConn.getConn();
- //声明一个ArrayList.用来存放Book类中的数据
- ArrayList<Book> list = new ArrayList<Book>();
- try {
- sta = conn.createStatement();
- String sql = "select * from book;";
- res = sta.executeQuery(sql);
- while (res.next()) {
- //建立了一个实体类,用来存放从数据库中拿到的数据
- Book book = new Book();
- book.setName(res.getString("name"));
- book.setAuthor(res.getString("author"));
- book.setPrice(res.getString("price"));
- book.setCountry(res.getString("country"));
- list.add(book);
- }
- //将list数据打包
- request.setAttribute("list", list);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- //将list数据发送到.jap文件中
- request.getRequestDispatcher("ListBook.jsp").forward(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
jsp文件
- <strong><span style="font-size:18px;"><%@page import="com.mi.beans.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 'ListBook.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">
- -->
- <script>
- </script>
- </head>
- <body>
- <!-- 用jsp语句,将servlet传过来的list数据拿到,并放到一个list中 -->
- <%
- ArrayList list = (ArrayList) request.getAttribute("list");
- %>
- <!-- 声明一个表格,这是表头 -->
- <h2 align = "center">图书列表</h2>
- <table border = 1px align = "center">
- <tr>
- <th>图书名称</th>
- <th>图书作者</th>
- <th>图书价格</th>
- <th>所属国家</th>
- </tr>
- <!-- 继续使用jsp语句 循环放入存放于list中的Book实体类中的数据 -->
- <%
- for(int i = 0;i<list.size();i++){
- Book book =(Book) list.get(i);%>
- <tr>
- <th><%=book.getName() %></th>
- <th><%=book.getAuthor()%></th>
- <th><%=book.getPrice()%></th>
- <th><%=book.getCountry()%></th><br>
- <!-- 此处设置了一个修改<a>标签,做修改操作.并将上面拿到的数据传给update.jsp,当进入修改页面的时候,原来的数据会显示 -->
- <th><a href="update.jsp?name=<%=book.getName()%>&author=<%=book.getAuthor()%>&
- &country=<%=book.getCountry()%>&price=<%=book.getPrice()%>">修改</a>
- <!-- 删除操作,只把name字段传给Delete_Servlet.java,用来做删除操作 -->
- <a href="Delete_Servlet?de_name=<%=book.getName()%>" onclick="confirm('确定删除该条记录?')">删除</a>
- </th>
- <% }
- %>
- </table>
- </body>
- </html></span></strong><span style="font-size:24px;">
- </span>