会话管理 session实现多页面传输数据
以下面购物车几个页面传输数据为例html页面有index.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>首页</title> 6 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> 7 </head> 8 <body> 9 <ul> 10 <li><a href="fuzhuang.html">服装</a></li> 11 <li><a href="shipin.html">食品</a></li> 12 <li><a href="dianzi.html">电子</a></li> 13 </ul> 14 <input type="button" id="view" value="查看购物车"> 15 <script> 16 $("#view").click(function () { 17 $.get("getgoods",function (data) { 18 alert(data.goods); 19 }); 20 }); 21 </script> 22 </body> 23 </html>
后面是三个副页fuzhuang.html;shipin.html;dianzi.html
fuzhuang.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>服装</title> 6 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> 7 </head> 8 <body> 9 <form id="fuz"> 10 <input type="checkbox" name="fz" value="服装1"/>服装1 11 <input type="checkbox" name="fz" value="服装2"/>服装2 12 <input type="checkbox" name="fz" value="服装3"/>服装3 13 <input type="checkbox" name="fz" value="服装4"/>服装4 14 <input type="button" value="添加" id="add-btn"/> 15 <input type="button" value="查看购物车"/> 16 </form> 17 <a href="index.html">回到首页</a> 18 <script> 19 $("#add-btn").click(function () { 20 var d=$("#fuz").serialize(); 21 $.post("addList",d,function (data) { 22 alert(data.status); 23 }); 24 }); 25 </script> 26 </body> 27 </html>
dianzi.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>电子</title> 6 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> 7 </head> 8 <body> 9 <form id="dzs"> 10 <input type="checkbox" name="dz" value="电子1">电子1 11 <input type="checkbox" name="dz" value="电子2">电子2 12 <input type="checkbox" name="dz" value="电子3">电子3 13 <input type="checkbox" name="dz" value="电子4">电子4 14 <input type="button" value="添加" id="add-btn"> 15 <input type="button" value="查看购物车"> 16 </form> 17 <a href="index.html">回到首页</a> 18 <script> 19 $("#add-btn").click(function () { 20 var d=$("#dzs").serialize(); 21 $.post("addList",d,function (data) { 22 alert(data.status); 23 }); 24 }); 25 </script> 26 </body> 27 </html>
shipin.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>食品</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> </head> <body> <form id="sps"> <input type="checkbox" name="sp" value="食品1">食品1 <input type="checkbox" name="sp" value="食品2">食品2 <input type="checkbox" name="sp" value="食品3">食品3 <input type="checkbox" name="sp" value="食品4">食品4 <input type="button" value="添加" id="add-btn"> <input type="button" value="查看购物车"> </form> <a href="index.html">回到首页</a> <script> $("#add-btn").click(function () { var d=$("#sps").serialize(); $.ajax({url:"addList", type:"post", data:d, success:function (data) { alert(data.status) }}); }); </script> </body> </html>
Servlet中两个页面
Addlist.java
1 package control; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 import java.io.IOException; 10 import java.io.PrintWriter; 11 import java.util.ArrayList; 12 import java.util.List; 13 14 @WebServlet("/addList") 15 public class AddList extends HttpServlet { 16 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 17 request.setCharacterEncoding("utf-8"); 18 String[] goods=request.getParameterValues("fz"); 19 String[] goods1=request.getParameterValues("dz"); 20 String[] goods2=request.getParameterValues("sp"); 21 HttpSession session=request.getSession(); 22 List<String> listgoods=(List<String>) session.getAttribute("goods"); 23 if (listgoods==null){ 24 listgoods=new ArrayList<>(); 25 session.setAttribute("goods",listgoods); 26 } 27 if(goods!=null){for (String s:goods){ 28 System.out.println(s+" "); 29 listgoods.add(s); 30 }} 31 if(goods1!=null){for (String s:goods1){ 32 System.out.println(s+" "); 33 listgoods.add(s); 34 }} 35 if(goods2!=null){for (String s:goods2){ 36 System.out.println(s+" "); 37 listgoods.add(s); 38 }} 39 response.setCharacterEncoding("utf-8"); 40 response.setContentType("application/json;charset=utf-8"); 41 PrintWriter out=response.getWriter(); 42 out.write("{\"status\":\"添加成功\"}"); 43 out.close(); 44 45 } 46 47 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 48 49 } 50 }
Getgoods.java
1 package control; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 import java.io.IOException; 10 import java.io.PrintWriter; 11 import java.util.List; 12 13 @WebServlet("/getgoods") 14 public class Getgoods extends HttpServlet { 15 @Override 16 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 17 HttpSession session=req.getSession(); 18 List<String> goods=(List<String>) session.getAttribute("goods"); 19 resp.setContentType("application/json;charset=utf-8"); 20 PrintWriter out=resp.getWriter(); 21 if (goods==null){ 22 out.write("{\"goods\":"+null+"}"); 23 }else{ 24 Object[] arr=goods.toArray(); 25 String s="{\"goods\":["; 26 for (int i=0;i<arr.length;i++){ 27 s+="\""+arr[i]+"\""; 28 if (i<arr.length-1) 29 s+=","; 30 } 31 s+="]}"; 32 System.out.println(s); 33 out.write(s); 34 } 35 out.close(); 36 } 37 }