【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,由服务器端生成,发送 ...
随机推荐
- Cassandra 简介
Cassandra是云原生和微服务化场景中最好的NoSQL数据库.我信了~ 1. Cassandra是什么 高可用性和可扩展的分布式数据库 Apache Cassandra™是一个开源分布式数据,可提 ...
- Linux内核驱动学习(八)GPIO驱动模拟输出PWM
文章目录 前言 原理图 IO模拟输出PWM 设备树 驱动端 调试信息 实验结果 附录 前言 上一篇的学习中介绍了如何在用户空间直接操作GPIO,并写了一个脚本可以产生PWM.本篇的学习会将写一个驱动操 ...
- 实现简单网页rtmp直播:nginx+ckplayer+linux
一.安装nginx 安装带有rtmp模块的nginx服务器(其它支持rtmp协议的流媒体服务器像easydarwin.srs等+Apache等web服务器也可以),此处使用nginx服务器,简单方便. ...
- springboot controller templates html
首先声明: @Controller注解的类必须要在启动类的子集目录下,否则无法扫描 本文要求: 通过controller层跳转页面到html页面(本篇用到thymeleaf模板) 项目结构展示: 第一 ...
- JPA 分页处理
1.要实现jpa分页管理首先得要正确配置jpa环境,在spring环境中的配置如下: 开启注解功能 <bean class="org.springframework.orm.jpa.s ...
- java ->IO流_序列化流与反序列化流
序列化流与反序列化流 用于从流中读取对象的操作流 ObjectInputStream 称为 反序列化流 用于向流中写入对象的操作流 ObjectOutputStream 称为 序列化流(对象 ...
- 关于python中第三方库安装方法和问题解决
一.安装方法 方法一: 1.管理员身份启动命令行(运行--->cmd) 2.pip install 库的绝对路径和库的详细名称 :或者运用cd命令跳转到下载好的库所在的位置然后pip insta ...
- vue绑定数据之前 会看到源代码
http://blog.csdn.net/fengjingyu168/article/details/72915468 VUE绑定数据闪现问题 问题描述如下: 1.在HTML中使用Vue为div绑定数 ...
- Deno会在短期内取代Node吗?
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://blog.bitsrc.io/what-is-deno-and-will-it-r ...
- dom4j——使用dom4j生成xml
使用org.dom4j.Element 创建xml /** * 生成Service.xml文件 * @param tran 交易对象 * @param filePath 文件夹路径 */ public ...