Session机制二(案例)
Posted 曹军
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Session机制二(案例)相关的知识,希望对你有一定的参考价值。
一:案例一(简易购物车)
1.目录结构
2.step1.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 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>Insert title here</title> 8 </head> 9 <body> 10 <h2>Step1:选择要购买的图书</h2> 11 <form action="<%=request.getContextPath() %>/processStep1" method="post"> 12 <table border="1" cellpadding="10" cellspacing="0"> 13 <tr> 14 <td>书名</td> 15 <td>购买</td> 16 </tr> 17 <tr> 18 <td>JAVA</td> 19 <td><input type="checkbox" name="book" value="JAVA"></td> 20 </tr> 21 <tr> 22 <td>Oracle</td> 23 <td><input type="checkbox" name="book" value="Oracle"></td> 24 </tr> 25 <tr> 26 <td>Struct2</td> 27 <td><input type="checkbox" name="book" value="Struct2"></td> 28 </tr> 29 <tr> 30 <td colspan="2"> 31 <input type="submit" value="Submit"/> 32 </td> 33 </tr> 34 </table> 35 </form> 36 </body> 37 </html>
3.ProcessStep1Servlet.java
1 package javaweb; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 /** 11 * Servlet implementation class ProcessStep1Servlet 12 */ 13 @WebServlet("/processStep1") 14 public class ProcessStep1Servlet extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 /** 18 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 19 */ 20 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 //获取选中的图书的信息 22 String[] books=request.getParameterValues("book"); 23 24 //将图书放进HttpSession中 25 request.getSession().setAttribute("books", books); 26 27 //重定向页面到shopcart/step2.jsp 28 //这里使用的绝对路径,同理,在jsp里也需要使用相对路径 29 response.sendRedirect(request.getContextPath()+"/shopCart/step2.jsp"); 30 } 31 32 }
4.step2.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 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>Insert title here</title> 8 </head> 9 <body> 10 <h2>Step2:请输入信用卡的信息</h2> 11 <form action="<%=request.getContextPath()%>/processStep2" method="post"> 12 <table border="1" cellpadding="10" cellspacing="0"> 13 <tr> 14 <td colspan="2">寄送信息</td> 15 </tr> 16 <tr> 17 <td>姓名</td> 18 <td><input type="text" name="name" /></td> 19 </tr> 20 <tr> 21 <td>地址</td> 22 <td><input type="text" name="address" /></td> 23 </tr> 24 <tr> 25 <td colspan="2">信用卡信息</td> 26 </tr> 27 <tr> 28 <td>种类</td> 29 <td><input type="radio" name="cardType" value="Visa" />Visa <input 30 type="radio" name="cardType" value="Master" />Master</td> 31 </tr> 32 <tr> 33 <td>卡号</td> 34 <td><input type="text" name="cardNum" /></td> 35 </tr> 36 <tr> 37 <td colspan="2"><input type="submit" value="Submit" /></td> 38 </tr> 39 </table> 40 </form> 41 </body> 42 </html><%@ page language="java" contentType="text/html; charset=utf-8" 43 pageEncoding="utf-8"%> 44 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 45 <html> 46 <head> 47 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 48 <title>Insert title here</title> 49 </head> 50 <body> 51 <h2>Step2:请输入信用卡的信息</h2> 52 <form action="<%=request.getContextPath()%>/processStep2" method="post"> 53 <table border="1" cellpadding="10" cellspacing="0"> 54 <tr> 55 <td colspan="2">寄送信息</td> 56 </tr> 57 <tr> 58 <td>姓名</td> 59 <td><input type="text" name="name" /></td> 60 </tr> 61 <tr> 62 <td>地址</td> 63 <td><input type="text" name="address" /></td> 64 </tr> 65 <tr> 66 <td colspan="2">信用卡信息</td> 67 </tr> 68 <tr> 69 <td>种类</td> 70 <td><input type="radio" name="cardType" value="Visa" />Visa <input 71 type="radio" name="cardType" value="Master" />Master</td> 72 </tr> 73 <tr> 74 <td>卡号</td> 75 <td><input type="text" name="cardNum" /></td> 76 </tr> 77 <tr> 78 <td colspan="2"><input type="submit" value="Submit" /></td> 79 </tr> 80 </table> 81 </form> 82 </body> 83 </html>
5.ProcessStep2Servlet.java
1 package javaweb; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 /** 11 * Servlet implementation class ProcessStep2Servlet 12 */ 13 @WebServlet("/processStep2") 14 public class ProcessStep2Servlet extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 /** 18 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 19 */ 20 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 //获取请求参数name address cardType cardNum 22 String name=request.getParameter("name"); 23 String address=request.getParameter("address"); 24 String cardType=request.getParameter("cardType"); 25 String cardNum=request.getParameter("cardNum"); 26 27 //将参入放进一个对象中 28 Customer customer=new Customer(name, address, cardType, cardNum); 29 30 //将请求信息存入HtppSession中 31 request.getSession().setAttribute("customer", customer); 32 33 //重定向到confirm.jsp 34 response.sendRedirect(request.getContextPath()+"/shopCart/confirm.jsp"); 35 } 36 37 }
6.conform.jsp
1 <%@page import="javaweb.Customer"%> 2 <%@ page language="java" contentType="text/html; charset=utf-8" 3 pageEncoding="utf-8"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 Customer customer=(Customer)session.getAttribute("customer"); 13 String[] books=(String[])session.getAttribute("books"); 14 %> 15 <h2>Step3:请确认</h2> 16 <table> 17 <tr> 18 <td>顾客姓名</td> 19 <td><%=customer.getName() %></td> 20 </tr> 21 <tr> 22 <td>顾客地址</td> 23 <td><%=customer.getAddress() %></td> 24 </tr> 25 <tr> 26 <td>顾客卡号</td> 27 <td><%=customer.getCardNum() %></td> 28 </tr> 29 <tr> 30 <td>顾客类型</td> 以上是关于Session机制二(案例)的主要内容,如果未能解决你的问题,请参考以下文章14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段