session+cookies实现购物车功能,javaweb开发
展开全部
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'shop2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
Map<String,Integer> itm=(Map<String,Integer>)session.getAttribute("item");
if(itm==null){
itm=new HashMap<String,Integer>();
itm.put("电视", 1);
itm.put("苹果", 0);
itm.put("香蕉", 1);
itm.put("衣服", 0);
}
String [] buy=request.getParameterValues("c");
for(String b:buy){
if(b.equals("电视")){
int num=itm.get("电视");
itm.put(b, num+1);
}
if(b.equals("苹果")){
int num=itm.get(b);
itm.put(b, num+1);
}
if(b.equals("香蕉")){
int num=itm.get(b);
itm.put(b, num+1);
}
if(b.equals("衣服")){
int num=itm.get(b);
itm.put(b, num+1);
}
}
session.setAttribute("item", itm);
%>
你所购买的物品<br>
电视:<%=itm.get("电视") %>本<br>
苹果:<%=itm.get("苹果") %>个<br>
香蕉:<%=itm.get("香蕉") %>个<br>
衣服:<%=itm.get("衣服") %>件<br>
<p><a href="shop.jsp">再次购买</a></p>
</body>
</html>
下面是购买页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'shop.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
欢迎光临本店! <br>
<form action="shop2.jsp" method="post">
<input name="c" type="checkbox" value="电视" />电视
<input name="c" type="checkbox" value="苹果" />苹果
<input name="c" type="checkbox" value="香蕉" />香蕉
<input name="c" type="checkbox" value="衣服" />衣服
<img alt="dddd" src="<%=request.getAttribute("path")%>">
<p> <%=request.getAttribute("yy")%></p>
<input name="" type="submit" value="确认购买" />
</form>
</body>
</html>
下面是使用cookie的
添加Cookie
Cookie uname1=new Cookie("lname",username);
uname1.setMaxAge(24*3600);
Cookie upwd1=new Cookie("lpwd",pwd);
upwd1.setMaxAge(24*3600);
response.addCookie(uname1);
response.addCookie(upwd1);
添加session
session.setAttribute("log_name", username);
以下代码是对cookie和session的数据操作!
<%
String uname="";
String upwd="";
Cookie[] cookies=request.getCookies(); //一request获范围获取一个
cookie实例
if(cookies!=null){
for(Cookie c: cookies){
if("lname".equals(c.getName())){ //判断cookie里面的名字是否等于这
个
uname=c.getValue(); //如果等于就获取它的值
}
if("lpwd".equals(c.getName())){
upwd=c.getValue();
}
}
String sql2="select * from member where username='"+uname+"'";
ResultSet rs2=null;
rs2=DBHelper.executeQuery(sql2);
boolean is=true;
while(rs2.next()){
String pp=rs2.getString("upwd");
if(pp.equals(upwd)){
is=false;
}
}
if(session.getAttribute("log_name")!=null){ //判断是否有slog_name这
个session
uname=(String)session.getAttribute("log_name");
is=false;
}
if(is){
response.sendRedirect("index1.jsp");
}
}
%>
<%
application.setAttribute("log", uname);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'shop2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
Map<String,Integer> itm=(Map<String,Integer>)session.getAttribute("item");
if(itm==null){
itm=new HashMap<String,Integer>();
itm.put("电视", 1);
itm.put("苹果", 0);
itm.put("香蕉", 1);
itm.put("衣服", 0);
}
String [] buy=request.getParameterValues("c");
for(String b:buy){
if(b.equals("电视")){
int num=itm.get("电视");
itm.put(b, num+1);
}
if(b.equals("苹果")){
int num=itm.get(b);
itm.put(b, num+1);
}
if(b.equals("香蕉")){
int num=itm.get(b);
itm.put(b, num+1);
}
if(b.equals("衣服")){
int num=itm.get(b);
itm.put(b, num+1);
}
}
session.setAttribute("item", itm);
%>
你所购买的物品<br>
电视:<%=itm.get("电视") %>本<br>
苹果:<%=itm.get("苹果") %>个<br>
香蕉:<%=itm.get("香蕉") %>个<br>
衣服:<%=itm.get("衣服") %>件<br>
<p><a href="shop.jsp">再次购买</a></p>
</body>
</html>
下面是购买页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'shop.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
欢迎光临本店! <br>
<form action="shop2.jsp" method="post">
<input name="c" type="checkbox" value="电视" />电视
<input name="c" type="checkbox" value="苹果" />苹果
<input name="c" type="checkbox" value="香蕉" />香蕉
<input name="c" type="checkbox" value="衣服" />衣服
<img alt="dddd" src="<%=request.getAttribute("path")%>">
<p> <%=request.getAttribute("yy")%></p>
<input name="" type="submit" value="确认购买" />
</form>
</body>
</html>
下面是使用cookie的
添加Cookie
Cookie uname1=new Cookie("lname",username);
uname1.setMaxAge(24*3600);
Cookie upwd1=new Cookie("lpwd",pwd);
upwd1.setMaxAge(24*3600);
response.addCookie(uname1);
response.addCookie(upwd1);
添加session
session.setAttribute("log_name", username);
以下代码是对cookie和session的数据操作!
<%
String uname="";
String upwd="";
Cookie[] cookies=request.getCookies(); //一request获范围获取一个
cookie实例
if(cookies!=null){
for(Cookie c: cookies){
if("lname".equals(c.getName())){ //判断cookie里面的名字是否等于这
个
uname=c.getValue(); //如果等于就获取它的值
}
if("lpwd".equals(c.getName())){
upwd=c.getValue();
}
}
String sql2="select * from member where username='"+uname+"'";
ResultSet rs2=null;
rs2=DBHelper.executeQuery(sql2);
boolean is=true;
while(rs2.next()){
String pp=rs2.getString("upwd");
if(pp.equals(upwd)){
is=false;
}
}
if(session.getAttribute("log_name")!=null){ //判断是否有slog_name这
个session
uname=(String)session.getAttribute("log_name");
is=false;
}
if(is){
response.sendRedirect("index1.jsp");
}
}
%>
<%
application.setAttribute("log", uname);
%>
合力亿捷
2023-07-25 广告
2023-07-25 广告
要快速搭建在线客服系统,可以考虑以下步骤:1. 确定需求:首先需要确定自己的需求和目标,例如需要什么类型的客服、需要支持多少用户、需要什么样的沟通工具等等。2. 选择合适的平台:选择一个合适的在线客服系统平台,可以选择公有云或私有云部署方式...
点击进入详情页
本回答由合力亿捷提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询