【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");
}
}
【Tomcat】JSP使用Session、Cookie实现购物车的更多相关文章
- 超全面的JavaWeb笔记day11<JSP&Session&Cookie&HttpSession>
1.JSP 2.回话跟踪技术 3.Cookie 4.HttpSession JSP入门 1 JSP概述 1.1 什么是JSP JSP(Java Server Pages)是JavaWeb服务器端的动态 ...
- JSP + Session Cookie详解
篇幅较大,对JSP进行了非常详细的讲解,并解释了Session和Cookie的实现原理 ,预计看完需要20分钟左右,慢慢享受吧 JSP概述 掌握了servlet后,就可以利用servlet来开发动态页 ...
- 【JSP】Cookie的使用及保存中文,并用Cookie实现购物车功能
Cookie是服务器存放在客户端的一些数据,比如密码,以及你曾经访问过的一些数据. 设置Cookie //设置cookie Cookie cookie = new Cookie("TOM&q ...
- Tomcat 中的 Session 和 Cookie
HTTP 是一种无状态通信协议,每个请求之间相互独立,服务器不能识别曾经来过的请求.而对于 Web 应用,它的活动都是依赖某个状态的,比如用户登录,此时使用 HTTP 就需要它在一次登录请求后,有为后 ...
- javaweb学习——session和Cookie实现购物车功能
1.创建Book类,实现对图书信息的封装. package cn.it.sessionDemo.example1; import java.io.Serializable; /** * 该类实现对图书 ...
- session cookie原理及应用
一.术语session在我的经验里,session这个词被滥用的程度大概仅次于transaction,更加有趣的是transaction与session在某些语境下的含义是相同的. session,中 ...
- 会话跟踪session cookie
会话跟踪 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在 ...
- Session&Cookie 简介及使用
Cookie cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 或其它语言来创建和取回 cookie ...
- java:Session(概述,三层架构实例(实现接口封装JDBC),Session实现简单购物车实例)
1.Session概述: Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存 ...
- session & cookie(li)
Session & Cookie 一.定义 Session,用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间.Cookie,由服务器端生成,发送 ...
随机推荐
- Spring官网阅读(九)Spring中Bean的生命周期(上)
文章目录 生命周期回调 1.Bean初始化回调 2.Bean销毁回调 3.配置默认的初始化及销毁方法 4.执行顺序 5.容器启动或停止回调 Lifecycle 接口 LifecycleProcesso ...
- mybatis与hibernate运行流程比较
hibernate长时间没用,感觉生疏了,正好借这篇文章整合下知识,顺便复习比较下两种框架. 概述: Mybatis和hibernate不同,它不完全是一个ORM框架,因为MyBatis需要程序员自己 ...
- Numpy-np.random.normal()正态分布
X ~ :随机变量X的取值和其对应的概率值P(X = ) 满足正态分布(高斯函数) 很多随机现象可以用正态分布描述或者近似描述 某些概率分布可以用正态分布近似计算 正态分布(又称高斯分布)的概率密度函 ...
- 【Kafka】自定义分区策略
自定义分区策略 思路 Command+Option+shift+N 调出查询页面,找到producer包的Partitioner接口 Partitioner下有一个DefaultPartitioner ...
- Day_10【常用API】扩展案例1_利用人出生日期到当前日期所经过的毫秒值计算出这个人活了多少天
分析以下需求,并用代码实现: 1.从键盘录入一个日期字符串,格式为 xxxx-xx-xx,代表该人的出生日期 2.利用人出生日期到当前日期所经过的毫秒值计算出这个人活了多少天 package com. ...
- 关于Fragment的点击切换数据滞留问题
场景再现:当我使用tabLayout + Fragment 切换不同的fragment时,出现了数据重复显示的问题: 思考逻辑: - 每次切换fragment都会重新获取数据,但是list集合是全局的 ...
- leeCode刷题 lc184
Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id. +----+-------+--------+--------------+| Id ...
- Mysql 常用函数(23)- sign 函数
Mysql常用函数的汇总,可看下面系列文章 https://www.cnblogs.com/poloyy/category/1765164.html sign 的作用 返回参数的符号 sign 的语法 ...
- SpringMVC 拦截返回值,并自定义
有关取代mvc:annotation-driven使用自定义配置请看: http://blog.csdn.net/cml_blog/article/details/45222431 1.在项目开发中, ...
- 2020年腾讯实习生C++面试题&持续更新中(1)
2020年腾讯实习生C++面试题&持续更新中(1) 腾讯面试整理(1) 最近大三的学生找实习生的同学非常多,给大家分享一篇腾讯实习生的面试题,关于面试题,会持续更新~~~ 也算是今天开通博客的 ...