Cookie&Seesion
Cookie&Seesion
Cookie
商品浏览记录
Jsp里面使用java代码
jap(Java Server Pager) 最终会翻译成一个类,就是一个Servlet.
1.定义全局变量
<%! int a = 99%>
2.定义局部变量
<% int b = 999 %>
<!--在jsp页面上显示a和b的值-->
<%=a %>
<%=b %>
Cookie总结
1.cookie是服务器给客户端发送过来的一小份数据,并且保存在客户端上.
2.获取cookie request.getCookie();
添加cookie response.addCookie();
3.Cookie分类
会话cookie:默认情况下关闭了浏览器,那么Cookie就会消失
持久cookie:在一定时间内有效,并且会保存在客户端上.
cookie.setMaxAge(0);//立即删除
cookie.setMaxAge(1000);//1000秒
Cookie的安全问题
1.Cookie会保存在客户端上,所以有安全隐患问题;
2.Cookie的大小与个数有限制;
Session(会话)
Session是基于Cookie的一种会话机制.Cookie是服务器返回一小份数据给客户端,并且存放在客户端上.Session是数据存放在服务器端.
常用API
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
//获取会话id
String id = session.getId();
System.out.println("会话id:"+id);
//存值
session.setAttribute(arg0, arg1);
//取值
session.getAttribute(arg0);
//移除值
session.removeAttribute();
}
Session何时创建,何时销毁?
创建:
如果有在servlet里面调用了request.getSession()时就创建了Seesion,
销毁:
Session是存放在服务器的内存中的一份数据.当然可以持久化.redis.即使关了服务器,Session也不会销毁.
1.关闭服务器;
2.session会话时间过期,默认有效期:30分钟
案例:简单购物车.
product_list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="CarServlet?id=0"><h3>Iphone7</h3></a><br/>
<a href="CarServlet?id=1"><h3>小米 mix3</h3></a><br/>
<a href="CarServlet?id=2"><h3>vivo xpro</h3></a><br/>
<a href="CarServlet?id=3"><h3>三星note8</h3></a><br/>
<a href="CarServlet?id=4"><h3>魅族7</h3></a><br/>
<a href="CarServlet?id=5"><h3>华为荣耀20</h3></a><br/>
</body>
</html>
CarServlet.java
package nike.hu;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class CarServlet
*/
public class CarServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//1.获取要购买商品的id
int id = Integer.parseInt( request.getParameter("id"));
String[] names = {"Iphone7","小米mix3","vivoxpro","三星note8","魅族7","华为荣耀20",};
//取到id对应的商品名称
String name = names[id];
//2.获取购物车存放东西的session
Map<String,Integer> map =(Map<String,Integer>) request.getSession().getAttribute("cart");
//session里面没有存放过任何东西
if(map == null){
map = new LinkedHashMap<String,Integer>();
request.getSession().setAttribute("cart", map);
}
//判断购物车里面有没有该商品
if(map.containsKey(name)){
int value=map.get(name);
map.put(name, value+1);
}else{
map.put(name, 1);
}
//输出界面(跳转)
response.getWriter().write("<a href='product_list.jsp'><h3>继续购物</h3></a><br/>");
response.getWriter().write("<a href='cart.jsp'><h3>去购物车结算</h3></a>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
cart.jsp
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>你的购物车商品如下:</h2>
<%
//先获取到map
Map<String,Integer> map =(Map<String,Integer>) session.getAttribute("cart");
//遍历map
if(map!=null){
for(String key:map.keySet()){
int value = map.get(key);
%>
<h3>名称:<%=key %> 数量:<%=value %></h3>
<%
}
}
%>
<a href="ClearCartServlet"><h4>清空购物车</h4></a>
</body>
</html>
ClearCartServlet.java
package nike.hu;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class ClearCartServlet
*/
public class ClearCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
//1.从session中移除某一个数据
//session.removeAttribute("cart");
//2.强制干掉会话,里面存放的任何数据就都没有了
session.invalidate();
response.sendRedirect("cart.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}