【Tomcat】JSP使用Session、Cookie实现购物车
购物界面shop.jsp
初始页面
添加商品后,在session中设置属性,重定向回到shop.jsp,然后根据session的内容显示结果
Cookie设置setMaxAge可以延长session的寿命
清空购物车就是清除session
<%@ page import="javax.jms.Session" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.URLDecoder" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/8/12
Time: 10:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>购物</title>
<style>
#big{
margin-left: 200px;
}
.pro img,.pro2 img{
width: 200px;
height: 200px;
display: block;
margin: 20px;
}
a{
border: 2px solid black;
border-radius: 5px;
font-weight: bold;
font-size: 16px;
color: black;
}
a:link{
text-decoration: none;
}
a:visited{
color: black;
}
a:hover{
color: green;
}
a:active{
text-decoration: underline;
}
.pro a{
margin-left: 20px;
background-color: pink;
}
.pro2 a{
margin-right: 20px;
background-color: greenyellow;
}
.pro,.pro2{
float: left;
text-align: center;
}
.rec{
position: relative;
top: 20px;
padding: 20px;
width: 80%;
height: 400px;
border: solid 2px gray;
clear: both;
}
span{
font-family: "Adobe 黑体 Std R";
font-weight: bold;
}
#empty{
background-color: red;
}
</style>
</head>
<body>
<div id="big">
<div class="pro" name="book"><img src="img/book01.jpg">
<span>书</span><a href="addpro?id=1">加入购物车</a></div>
<div class="pro"><img src="img/cloth01.jpg">
<span>衣服</span><a href="addpro?id=2">加入购物车</a></div>
<div class="pro" name="mod"><img src="img/mod01.jpg">
<span>口红</span><a href="addpro?id=3">加入购物车</a></div>
<div class="rec">
<span>我的购物车</span>
<a href="addpro?id=del" id="empty">清空购物车</a><br>
<%
String[] strs={"书","衣服","口红"};
HttpSession session1 = request.getSession();
Enumeration<String> names = session1.getAttributeNames();
while (names.hasMoreElements()){
String n=names.nextElement();
Object attribute = session1.getAttribute(n);
if(n.equals("1")){
%>
<div class="pro2" name="book"><img src="img/book01.jpg">
<a href="addpro?id=1">数量+1</a><span><%=strs[0]+"有"+session1.getAttribute(n)+"件"%></span></div>
<%
}else if(n.equals("2")){
%>
<div class="pro2" name="book"><img src="img/cloth01.jpg">
<a href="addpro?id=2">数量+1</a><span><%=strs[1]+"有"+session1.getAttribute(n)+"件"%></span></div>
<%
}else if(n.equals("3")){
%>
<div class="pro2" name="book"><img src="img/mod01.jpg">
<a href="addpro?id=3">数量+1</a><span><%=strs[2]+"有"+session1.getAttribute(n)+"件"%></span></div>
<%
}
System.out.println(n+"有"+attribute+"件");
}
System.out.println("-----------");
%>
</div>
</div>
</body>
</html>
Servlet
package com.blb.cookie.shop;
import jdk.nashorn.internal.ir.CallNode;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;
@WebServlet("/addpro")
public class AddProServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String proid = request.getParameter("id");
//获取session
HttpSession session = request.getSession();
if ("del".equals(proid)) {
session.invalidate();
} else {
String sid = session.getId();
Cookie cookie1 = new Cookie("JSESSIONID", sid);
cookie1.setMaxAge(60 * 60);
response.addCookie(cookie1);
Enumeration<String> names = session.getAttributeNames();
Boolean hava = false;
if (!names.hasMoreElements()) {
session.setAttribute(proid, 1);
}
while (names.hasMoreElements()) {
String n = names.nextElement();
// System.out.println("name:" + n);
if (n.equals(proid)) {
hava = true;
Object attribute = session.getAttribute(n);
// System.out.println("atrr:" + attribute);
int a = (Integer) attribute;
int b = a + 1;
// System.out.println("attr++:" + b);
session.removeAttribute(n);
session.setAttribute(n, b);
}
}
if (!hava) {
session.setAttribute(proid, 1);
}
/* System.out.println(proid);
System.out.println(session);
System.out.println("addpro*******");*/
}
response.sendRedirect("shop.jsp");
}
}
Victory!