会话管理 session实现多页面传输数据
2024-08-29 13:29:05
以下面购物车几个页面传输数据为例html页面有index.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>
<ul>
<li><a href="fuzhuang.html">服装</a></li>
<li><a href="shipin.html">食品</a></li>
<li><a href="dianzi.html">电子</a></li>
</ul>
<input type="button" id="view" value="查看购物车">
<script>
$("#view").click(function () {
$.get("getgoods",function (data) {
alert(data.goods);
});
});
</script>
</body>
</html>
后面是三个副页fuzhuang.html;shipin.html;dianzi.html
fuzhuang.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="fuz">
<input type="checkbox" name="fz" value="服装1"/>服装1
<input type="checkbox" name="fz" value="服装2"/>服装2
<input type="checkbox" name="fz" value="服装3"/>服装3
<input type="checkbox" name="fz" 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=$("#fuz").serialize();
$.post("addList",d,function (data) {
alert(data.status);
});
});
</script>
</body>
</html>
dianzi.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="dzs">
<input type="checkbox" name="dz" value="电子1">电子1
<input type="checkbox" name="dz" value="电子2">电子2
<input type="checkbox" name="dz" value="电子3">电子3
<input type="checkbox" name="dz" 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=$("#dzs").serialize();
$.post("addList",d,function (data) {
alert(data.status);
});
});
</script>
</body>
</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
package control; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List; @WebServlet("/addList")
public class AddList extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String[] goods=request.getParameterValues("fz");
String[] goods1=request.getParameterValues("dz");
String[] goods2=request.getParameterValues("sp");
HttpSession session=request.getSession();
List<String> listgoods=(List<String>) session.getAttribute("goods");
if (listgoods==null){
listgoods=new ArrayList<>();
session.setAttribute("goods",listgoods);
}
if(goods!=null){for (String s:goods){
System.out.println(s+" ");
listgoods.add(s);
}}
if(goods1!=null){for (String s:goods1){
System.out.println(s+" ");
listgoods.add(s);
}}
if(goods2!=null){for (String s:goods2){
System.out.println(s+" ");
listgoods.add(s);
}}
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
PrintWriter out=response.getWriter();
out.write("{\"status\":\"添加成功\"}");
out.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
}
Getgoods.java
package control; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; @WebServlet("/getgoods")
public class Getgoods extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session=req.getSession();
List<String> goods=(List<String>) session.getAttribute("goods");
resp.setContentType("application/json;charset=utf-8");
PrintWriter out=resp.getWriter();
if (goods==null){
out.write("{\"goods\":"+null+"}");
}else{
Object[] arr=goods.toArray();
String s="{\"goods\":[";
for (int i=0;i<arr.length;i++){
s+="\""+arr[i]+"\"";
if (i<arr.length-1)
s+=",";
}
s+="]}";
System.out.println(s);
out.write(s);
}
out.close();
}
}
最新文章
- inux环境PHP7.0安装
- leetcode_question_73 Set Matrix Zeroes
- VS2003转VS2010 fatal error C1189: #error
- cocos2dx CCControlSlider
- 11gOCP 1z0-052 :2013-09-11 MGR_ROLE role........................................................A66
- The Swift Programming Language-官方教程精译Swift(2)基础知识
- 水晶报表在vs2010 ;WPF环境下的尝试
- css字体设置
- mongodb 查询时没有索引报错(too much data for sort() with no index)
- CentOS7下搭建hadoop2.7.3完全分布式
- webservice07#契约优先#webservice实现简单的动态web项目
- webpack基础
- ArcGIS for JavaScript学习(二)Server发布服务
- react-conponent-secondesElapsed
- [bzoj1692][队列变换]
- struct放入list中按照某字段排序
- 获取href连接并跳转
- LeetCode 884 Uncommon Words from Two Sentences 解题报告
- 【java】之深入理解JVM
- JSP属性的四种保存范围(page request session application)
热门文章
- Linux更改ssh端口号,很easy!
- 访问数据库需要注意的问题 c#
- 关于Vue 兄弟组件通信
- Ansible工作架构和原理
- (转)神舟飞船上的计算机使用什么操作系统,为什么是自研发不是 Linux?
- TCP/IP协议之http和https协议
- python中的字符串内置方法小结
- arm-none-linux-gnueabi-gcc No such file or directory这个错误的解决方法
- [CodeForces954G]Castle Defense(二分答案+差分)
- 牛客暑假多校第一场J-Different Integers