一:View addBook.jsp
1 <%@page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8" import="java.util.*" import="nuc.sw.EL.bean.EL"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>添加书本</title> 8 </head> 9 <body> 10 <form action="ELServlet" method="post"> 11 <table align="left" > 12 <tr> 13 <td>书名:</td> 14 <td><input type="text" name="bookName"></td> 15 </tr> 16 <tr> 17 <td>作者:</td> 18 <td><input type="text" name="authorName"></td> 19 </tr> 20 <tr> 21 <td>定价:</td> 22 <td><input type="text" name="bookPrice"></td> 23 </tr> 24 <tr> 25 <td><input type="submit" value="添加"></td> 26 </tr> 27 </table> 28 </form> 29 30 31 <% 32 List all=(List)session.getAttribute("booklist"); 33 if(all!=null) 34 { 35 %> 36 <table align="right" border="1"> 37 <caption>全部书籍信息</caption> 38 <tr> 39 <td>书名:</td> 40 <td>作者:</td> 41 <td>定价:</td> 42 </tr> 43 <% 44 Iterator<EL> iterAlone=((ArrayList<EL>)session.getAttribute("booklist")).iterator(); 45 Iterator<EL> iter=all.iterator(); 46 while(iter.hasNext()){ 47 pageContext.setAttribute("book",iter.next()); 48 %> 49 50 <tr> 51 <td>${book.bookName} </td> 52 <td>${book.authorName} </td> 53 <td>${book.bookPrice} </td> 54 </tr> 55 <% 56 } 57 %> 58 59 </table> 60 <% 61 } 62 %> 63 </body> 64 </html>
二:nuc.sw.EL.bean Book.java
1 package nuc.sw.EL.bean; 2 3 public class EL { 4 private String bookName; 5 private String authorName; 6 private float bookPrice; 7 public String getBookName() { 8 return bookName; 9 } 10 public void setBookName(String bookName) { 11 this.bookName = bookName; 12 } 13 public String getAuthorName() { 14 return authorName; 15 } 16 public void setAuthorName(String authorName) { 17 this.authorName = authorName; 18 } 19 public Float getBookPrice() { 20 return bookPrice; 21 } 22 public void setBookPrice(Float bookPrice) { 23 this.bookPrice = bookPrice; 24 } 25 26 }
三:nuc.sw.EL.servlet RLServlet.java
1 package nuc.sw.EL.servlet; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import nuc.sw.EL.bean.EL; 13 14 /** 15 * Servlet implementation class ELServlet 16 */ 17 public class ELServlet extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public ELServlet() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 /** 29 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 30 */ 31 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 // TODO Auto-generated method stub 33 response.getWriter().append("Served at: ").append(request.getContextPath()); 34 } 35 36 /** 37 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 38 */ 39 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 40 // TODO Auto-generated method stub 41 //doGet(request, response); 42 //接受表单传过来的数据,并封装到bean中 43 request.setCharacterEncoding("utf-8"); 44 EL el=new EL(); 45 el.setBookName(request.getParameter("bookName")); 46 el.setAuthorName(request.getParameter("authorName")); 47 el.setBookPrice(Float.parseFloat(request.getParameter("bookPrice"))); 48 //request.setAttribute("el", el); 49 //集合模拟数据库,所以不需要连接数据库。 50 //新建集合 的话则一直都是新的,而且每个集合里都只有一个数据。所以需要把list放到一次会话中,即Session范围。 51 List<EL> list; 52 //从session中获取保存的信息。 53 //如果为空,则新建集合,书籍信息存入集合。 54 //Session为jsp的内置对象,如果在servlet中调用需要使用request.getSession(). 55 if(request.getSession().getAttribute("booklist")==null){ 56 list= new ArrayList<EL>(); 57 list.add(el); 58 } 59 //假设不为空,直接获取list,并将书籍信息添加。 60 61 else{ 62 list=(ArrayList<EL>)request.getSession().getAttribute("booklist"); 63 list.add(el); 64 } 65 //将集合放入session中 66 request.getSession().setAttribute("booklist", list); 67 //request.getRequestDispatcher("showBookInfo.jsp").forward(request, response); 68 //sevlet不擅长页面显示,所以就需要跳转。 69 request.getRequestDispatcher("addBook.jsp").forward(request, response); 70 } 71 72 }
四:结果显示
时间: 2025-01-05 14:32:56