『JavaWeb--クッキーを利用してユーザーの履歴閲覧を記録する』

6350 ワード

コードは少ないが、多くの問題に直面している.いろいろ勉強になった気もします.
//    ,“  ”Servlet
package com.fenghuo.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

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 BuyServlet extends HttpServlet {

	/**
	 *  Title:  cookie           
	 *  Copyright: Copyright (c) 2012
	 *  @author:   
	 *  @version 1.0 2012-09-14
	 */
	private static final long serialVersionUID = 4408943836189873374L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//      
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
	
		out.print("        :(      )
"); // ” “ Map map = DB.getAll(); // , id ShowObject out.print("=========================
"); for (int i = 1; i < map.size()+1; i++){ out.print(""+map.get(i+"").getName()+"
"); } // out.print(" :
"); // cookie , cookie Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++){ // cookie if (cookies[i].getName().equals("historyCookie")){ String[] ids = cookies[i].getValue().split("\\,"); // cookie id, for (String id : ids){ out.print(""+map.get(id).getName()+"
"); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } // class DB{ private static LinkedHashMap map = new LinkedHashMap(); static{ map.put("1", new MyObject("1", " ", "12.0", " !")); map.put("2", new MyObject("2", " ", "5.0", " !")); map.put("3", new MyObject("3", " ", "8.0", " , !")); map.put("4", new MyObject("4", " ", "6.0", " !")); map.put("5", new MyObject("5", " ", "18.0", " !")); } public static Map getAll(){ return map; } } class MyObject{ private String id; private String name; private String price; private String describe; public MyObject(String id, String name, String price, String describe) { super(); this.id = id; this.name = name; this.price = price; this.describe = describe; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } }
//          Servlet
package com.fenghuo.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Map;

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 ShowObject extends HttpServlet {

	/**
	 *  Title:         ,    cookie
	 *  Copyright: Copyright (c) 2012
	 *  @author:   
	 *  @version 1.0 2012-09-14
	 */
	private static final long serialVersionUID = 2894544204585860934L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//      
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		//  Url  id
		String id = request.getParameter("id");
		
		//  ”   “   
		Map map = DB.getAll();
		//  id       
		MyObject myobj = (MyObject) map.get(id);
		
		out.write("       :"+myobj.getName()+"
"); out.write(" :"+myobj.getPrice()+"
"); out.write(" :
"); out.write(myobj.getDescribe()); // cookie String cookieValue = buildCookie(id, request);// cookie Cookie cookie = new Cookie("historyCookie", cookieValue); cookie.setMaxAge(1*24*3600); cookie.setPath("/myweb"); response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) { String historyCookie = null; // cookie Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++){ if (cookies[i].getName().equals("historyCookie") ){ historyCookie = cookies[i].getValue(); } } // id if (historyCookie == null){ return id; } LinkedList list = new LinkedList( Arrays.asList((historyCookie.split("\\,")))); // id if (list.contains(id)){ list.remove(id); }else{ if (list.size() >= 5){ list.removeLast(); } } list.addFirst(id); StringBuffer sb = new StringBuffer(); for (String sid : list){ sb.append(sid + ","); } sb.deleteCharAt(sb.length()-1); return sb.toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

初めて商品を閲覧してクッキーを生成
表示履歴ブラウズの更新
次の商品を閲覧する
表示の更新
最初に閲覧した商品を再閲覧して前に移動します
複数の商品を閲覧する