利用Cookie记录用户的浏览记录:(网站的真实案例)

index.jsp

<%@page import="com.henu.demo.util.CookieUtil"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires",-10);

%>







Player Menu


       保罗乔治
       伦纳德
       芦苇
       黑贝
     
浏览过的球员:清空浏览记录 <% Cookie[] cs = request.getCookies(); Cookie c = CookieUtil.findCookieByName("product", cs); if(c != null){ String value = c.getValue(); String[] ss = value.split(","); for(String s : ss){ %> <% } } %>

ViewServlet:

package com.henu.demo.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.henu.demo.util.CookieUtil;

public class ViewServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取服务器传来的id
		String id = request.getParameter("id");
		//获取请求中的cookie
		Cookie[] cs = request.getCookies();
		if (cs == null) {//第一次访问
			Cookie c = new Cookie("product", "id");
			c.setMaxAge(10000);
			c.setPath("/");
			response.addCookie(c);
		}else {
			//在cs中寻找getName()<<>>prodduct的cookie
			Cookie cookie = CookieUtil.findCookieByName("product", cs);
			//如果为空,则设置一个cookie,使index.jsp获取
			if (cookie == null) {
				Cookie c = new Cookie("product", id);
				c.setMaxAge(10000);
				c.setPath("/");//可在同一应用服务器内共享方法
				response.addCookie(c);
			}else {
				//如果不为空,则拼接product的value值,如:1 ,2
				String value = cookie.getValue();
				//拆开做判断,为了不添加同样的球员,相同则不添加
				String[] ss = value.split(",");
				boolean flag = this.checkId(ss, id);//当flag为false  则添加
				if (!flag) {
					Cookie c = new Cookie("product", value+","+id);
					c.setMaxAge(10000);
					c.setPath("/");
					response.addCookie(c);
				}
			}
		}
		//将id设置到request域中
		request.setAttribute("id", id);
		/*
		 * 当使用转发请求时,你会发现点击一次,图片并不显示,当再次点击时,会出现上次的请求内容
		 * 原因在于,第一次请求转发时,index.jsp并没有转发,而在第二次请求转发的时候,因为请求
		 * 是从index.jsp中发出,所以此时刷新jsp,那么为什么得到的是第一次的内容,原因在
		 * 于第一次转发请求时,这时的index.jsp已经发生改变,只是未得到刷新。
		 * 所以使用重定向解决此类问题,当然也可以这样
		 * 当第一次请求转发可以调到另一个jsp中,然后在jsp中再做一次请求转发,即可。
		 * 
		 * 这时你会发现,归根结底是请求不同的原因。
		 */
//		request.getRequestDispatcher("index.jsp?aaa="+Math.random()).forward(request, response);
		response.sendRedirect("index.jsp");
	}
	// 判断是否点过某个球员
	public boolean checkId(String[] ss,String id){
		boolean flag = false;
		for(String s:ss){
			if(s.equals(id)){
				flag = true;
				break;
			}
		}
		return flag;
	}
}

CookieUtil

package com.henu.demo.util;

import javax.servlet.http.Cookie;

public class CookieUtil {

	public static Cookie findCookieByName(String name,Cookie[] cs){
		if(cs!=null){
			for(Cookie c:cs){
				if(name.equals(c.getName())){
					return c;
				}
			}
		}
		return null;
	}
}

ClearServlet

package com.henu.demo.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.henu.demo.util.CookieUtil;
/**
 * @author George
 * @description  删除所有浏览记录
 */
public class ClearServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cookie[] cs = request.getCookies();
		Cookie c = CookieUtil.findCookieByName("product", cs);
		if (c!=null) {
			c.setPath("/");
			c.setMaxAge(0);
			response.addCookie(c);
			response.sendRedirect("index.jsp");
		}
	}
}

 

你可能感兴趣的:(【JAVA】JavaWeb)