2.4.【案例】显示用户上次访问的时间
package blog.csdn.net.web.cookie;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 演示上一次访问的时间
*/
public class AccessServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取浏览器带来的所有cookie
Cookie[] cookies = request.getCookies();
//获取指定cookie
Cookie lastTimeCookie = getLastTimeCookie("lastTime",cookies);
if(null !=lastTimeCookie) {
//获取cookie中保存的上次访问时间
String lastTime = lastTimeCookie.getValue();
response.getWriter().println("上次访问时间是:"+lastTime);
//更新上次访问时间
String newTime = new Date().toLocaleString();
lastTimeCookie.setValue(newTime);
}else {
//创建cookie保存访问时间
String newTime = new Date().toLocaleString();
lastTimeCookie = new Cookie("lastTime",newTime);
response.getWriter().println("第一次访问时间是:"+newTime);
}
//设置cookie的缓存时间为10s
//lastTimeCookie.setMaxAge(10);
//设置cookie的访问路径,只有在设置的路径下有效
//lastTimeCookie.setPath("/access/hello");
//回写cookie给浏览器
response.addCookie(lastTimeCookie);
}
/**
* 获取指定的cookie
* @param name
* @param cookies
* @return
*/
private Cookie getLastTimeCookie(String name, Cookie[] cookies) {
if(null !=cookies) {
for(Cookie cookie : cookies) {
if(name.equals(cookie.getName()))
return cookie;
}
}
return null;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package blog.csdn.net.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
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;
/**
* 添加到购物车
*/
public class Add2CarServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取get请求的商品名,注意需要转码
String name = new String(request.getParameter("name").getBytes("iso8859-1"),"utf-8");
//获取session
HttpSession session = request.getSession();
//从session中获取购物车
Map carMap = (Map) session.getAttribute("car");
if(null ==carMap) {
//购物车为空,则创建并添加当前商品
carMap = new HashMap<>();
carMap.put(name, 1);
}else {
//购物车不为空,则判断当前添加的商品是否存在
if(carMap.containsKey(name)) {
//存在则累加个数
Integer count = carMap.get(name);
carMap.put(name, ++count);
}else {
//不存在则添加
carMap.put(name, 1);
}
}
//将购物车重新保存到session中
session.setAttribute("car", carMap);
//重定向到购物车
response.sendRedirect(request.getContextPath()+"/showcar.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
查看购物车
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
购物车
商品名称
商品数量
<%
//获取session中的购物车
Map carMap = (Map) session.getAttribute("car");
if(null == carMap){
%>
购物车空空如也
<%
}else{
for(String name : carMap.keySet()){
%>
<%=name %>
<%=carMap.get(name) %>
<%
}
}
%>
继续购物
清空购物车