Servlet向JSP传值

来源:互联网 发布:中老年春秋连衣裙淘宝 编辑:程序博客网 时间:2024/05/19 08:05

声明:本文转自http://blog.csdn.net/mpfly/article/details/70195671点击打开链接


servlet文件

[html] view plain copy
  1. package com.mi.servlet;  
  2.   
  3. import java.io.IOException;  
  4.   
  5.   
  6. import java.io.PrintWriter;  
  7.   
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12. import com.mi.beans.*;  
  13. import java.sql.*;  
  14. import java.util.ArrayList;  
  15. public class List_book extends HttpServlet {  
  16.   
  17.       
  18.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  19.             throws ServletException, IOException {  
  20.         Connection conn = null;  
  21.         Statement sta = null;  
  22.         ResultSet res = null;  
  23.         conn = DBConn.getConn();  
  24.         //声明一个ArrayList.用来存放Book类中的数据  
  25.         ArrayList<Book> list = new ArrayList<Book>();  
  26.         try {  
  27.             sta = conn.createStatement();  
  28.             String sql = "select * from book;";  
  29.             res = sta.executeQuery(sql);  
  30.             while (res.next()) {  
  31.                 //建立了一个实体类,用来存放从数据库中拿到的数据  
  32.                 Book book = new Book();  
  33.                 book.setName(res.getString("name"));  
  34.                 book.setAuthor(res.getString("author"));  
  35.                 book.setPrice(res.getString("price"));  
  36.                 book.setCountry(res.getString("country"));  
  37.                 list.add(book);  
  38.             }  
  39.             //将list数据打包  
  40.             request.setAttribute("list", list);  
  41.               
  42.         } catch (SQLException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.           
  46.         //将list数据发送到.jap文件中  
  47.         request.getRequestDispatcher("ListBook.jsp").forward(request, response);  
  48.     }  
  49.   
  50.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  51.             throws ServletException, IOException {  
  52.         doGet(request, response);  
  53.     }  
  54.   
  55. }  


jsp文件

[html] view plain copy
  1. <strong><span style="font-size:18px;"><%@page import="com.mi.beans.Book"%>  
  2. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'ListBook.jsp' starting page</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.     <!-- 
  21.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  22.     -->  
  23.     <script>  
  24.     </script>  
  25.   </head>  
  26.   <body>  
  27.         <!-- 用jsp语句,将servlet传过来的list数据拿到,并放到一个list中 -->  
  28.     <%   
  29.         ArrayList list = (ArrayList) request.getAttribute("list");  
  30.     %>  
  31.     <!-- 声明一个表格,这是表头 -->  
  32.     <h2 align = "center">图书列表</h2>  
  33.     <table border = 1px align = "center">  
  34.         <tr>  
  35.             <th>图书名称</th>  
  36.             <th>图书作者</th>  
  37.             <th>图书价格</th>  
  38.             <th>所属国家</th>  
  39.         </tr>  
  40.         <!-- 继续使用jsp语句 循环放入存放于list中的Book实体类中的数据 -->  
  41.         <%  
  42.             for(int i = 0;i<list.size();i++){  
  43.                 Book book =(Book) list.get(i);%>  
  44.                 <tr>  
  45.                 <th><%=book.getName() %></th>  
  46.                 <th><%=book.getAuthor()%></th>  
  47.                 <th><%=book.getPrice()%></th>  
  48.                 <th><%=book.getCountry()%></th><br>  
  49.                   
  50.                 <!-- 此处设置了一个修改<a>标签,做修改操作.并将上面拿到的数据传给update.jsp,当进入修改页面的时候,原来的数据会显示 -->  
  51.                 <th><a href="update.jsp?name=<%=book.getName()%>&author=<%=book.getAuthor()%>&  
  52.                 &country=<%=book.getCountry()%>&price=<%=book.getPrice()%>">修改</a>   
  53.                 <!-- 删除操作,只把name字段传给Delete_Servlet.java,用来做删除操作 -->  
  54.                 <a href="Delete_Servlet?de_name=<%=book.getName()%>" onclick="confirm('确定删除该条记录?')">删除</a>  
  55.                 </th>  
  56.         <% }  
  57.          %>  
  58.     </table>  
  59.   </body>  
  60. </html></span></strong><span style="font-size:24px;">  
  61. </span>