cookie實現使用者商品瀏覽記錄
阿新 • • 發佈:2018-11-25
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
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;
/**
*
* @author bg
*/
public class CookieDemo3 extends HttpServlet
{
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//1.輸出網站所有商品
out.write("網站所有商品:<br>");
Map<String, Book> map = Data.getAll();
for (Map.Entry<String, Book> entry : map.entrySet())
{
Book book = entry.getValue();
out.write("<a href='/shopcart/cookieDemo4?id=" + book.getId() + "' target=_blank>" + book.getName() + "</a><br/>");
}
//2.顯示使用者曾經看過的書
out.write("曾經瀏覽過的商品: <br>");
Cookie cookies[] = request.getCookies();
for (int i = 0; i < cookies.length; i++)
{
if (cookies[i].getName().equals("bookHistory"))
{
String[] ids = cookies[i].getValue().split("\\,");
for (int j = 0; i < ids.length && j < 3; j++)
{
Book book = Data.getAll().get(ids[j]);
out.write(book.getName() + "<br/>");
}
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
{
return "Short description";
}// </editor-fold>
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author bg
*/
public class CookieDemo4 extends HttpServlet
{
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//1. 根據id,顯示詳細資訊
String id = request.getParameter("id");
Book book = Data.getAll().get(id);
out.write(book.getId() + "<br/>");
out.write(book.getName() + "<br/>");
out.write(book.getAuthor() + "<br/>");
out.write(book.getDescription() + "<br/>");
//2.構建cookie,會送給瀏覽器
String cookieValue = buildCookie(id, request);
Cookie cookie = new Cookie("bookHistory", cookieValue);
cookie.setMaxAge(1 * 30 * 24 * 60 * 60);
cookie.setPath("/shopcart");
response.addCookie(cookie);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
{
return "Short description";
}// </editor-fold>
private String buildCookie(String id, HttpServletRequest request)
{
//bookHistory = null, 1
//bookHistory != null , 234, 3 -- 324
//bookHistory != null , 234, 1 -- 123
//bookHistory != null , 34, 1 -- 134
String bookHistory = null;
Cookie[] cookies = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++)
{
if (cookies[i].getName().equals("bookHistory"))
{
bookHistory = cookies[i].getValue();
}
}
if (bookHistory == null)
{
return id;
}
LinkedList<String> list = new LinkedList(Arrays.asList(bookHistory.split("\\,")));
if (list.contains(id))
{
list.remove(id);
} else
{
if (list.size() > 3)
{
list.removeLast();
}
}
list.addFirst(id);
StringBuilder sb = new StringBuilder();
for (String bid : list)
{
sb.append(bid + ",");
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.util.LinkedHashMap;
import java.util.Map;
/**
*
* @author bg
*/
public class Data
{
private static Map<String, Book> map = new LinkedHashMap();
static {
map.put("1", new Book("1", "java web開發", "老張", "一本好書"));
map.put("2", new Book("2", "java SE開發", "老張", "一本好書"));
map.put("3", new Book("3", "java J2EE開發", "老張", "一本好書"));
map.put("4", new Book("4", "java ME開發", "老張", "一本好書"));
}
public static Map<String, Book> getAll()
{
return map;
}
}
class Book
{
private String id;
private String name;
private String author;
private String description;
public Book()
{
}
public Book(String id, String name, String author, String description)
{
this.id = id;
this.name = name;
this.author = author;
this.description = description;
}
/**
* @return the id
*/
public String getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(String id)
{
this.id = id;
}
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the author
*/
public String getAuthor()
{
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author)
{
this.author = author;
}
/**
* @return the description
*/
public String getDescription()
{
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description)
{
this.description = description;
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
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;
/**
*
* @author bg
*/
public class CookieDemo3 extends HttpServlet
{
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//1.輸出網站所有商品
out.write("網站所有商品:<br>");
Map<String, Book> map = Data.getAll();
for (Map.Entry<String, Book> entry : map.entrySet())
{
Book book = entry.getValue();
out.write("<a href='/shopcart/cookieDemo4?id=" + book.getId() + "' target=_blank>" + book.getName() + "</a><br/>");
}
//2.顯示使用者曾經看過的書
out.write("曾經瀏覽過的商品: <br>");
Cookie cookies[] = request.getCookies();
for (int i = 0; i < cookies.length; i++)
{
if (cookies[i].getName().equals("bookHistory"))
{
String[] ids = cookies[i].getValue().split("\\,");
for (int j = 0; i < ids.length && j < 3; j++)
{
Book book = Data.getAll().get(ids[j]);
out.write(book.getName() + "<br/>");
}
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
{
return "Short description";
}// </editor-fold>
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author bg
*/
public class CookieDemo4 extends HttpServlet
{
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//1. 根據id,顯示詳細資訊
String id = request.getParameter("id");
Book book = Data.getAll().get(id);
out.write(book.getId() + "<br/>");
out.write(book.getName() + "<br/>");
out.write(book.getAuthor() + "<br/>");
out.write(book.getDescription() + "<br/>");
//2.構建cookie,會送給瀏覽器
String cookieValue = buildCookie(id, request);
Cookie cookie = new Cookie("bookHistory", cookieValue);
cookie.setMaxAge(1 * 30 * 24 * 60 * 60);
cookie.setPath("/shopcart");
response.addCookie(cookie);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
{
return "Short description";
}// </editor-fold>
private String buildCookie(String id, HttpServletRequest request)
{
//bookHistory = null, 1
//bookHistory != null , 234, 3 -- 324
//bookHistory != null , 234, 1 -- 123
//bookHistory != null , 34, 1 -- 134
String bookHistory = null;
Cookie[] cookies = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++)
{
if (cookies[i].getName().equals("bookHistory"))
{
bookHistory = cookies[i].getValue();
}
}
if (bookHistory == null)
{
return id;
}
LinkedList<String> list = new LinkedList(Arrays.asList(bookHistory.split("\\,")));
if (list.contains(id))
{
list.remove(id);
} else
{
if (list.size() > 3)
{
list.removeLast();
}
}
list.addFirst(id);
StringBuilder sb = new StringBuilder();
for (String bid : list)
{
sb.append(bid + ",");
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.util.LinkedHashMap;
import java.util.Map;
/**
*
* @author bg
*/
public class Data
{
private static Map<String, Book> map = new LinkedHashMap();
static {
map.put("1", new Book("1", "java web開發", "老張", "一本好書"));
map.put("2", new Book("2", "java SE開發", "老張", "一本好書"));
map.put("3", new Book("3", "java J2EE開發", "老張", "一本好書"));
map.put("4", new Book("4", "java ME開發", "老張", "一本好書"));
}
public static Map<String, Book> getAll()
{
return map;
}
}
class Book
{
private String id;
private String name;
private String author;
private String description;
public Book()
{
}
public Book(String id, String name, String author, String description)
{
this.id = id;
this.name = name;
this.author = author;
this.description = description;
}
/**
* @return the id
*/
public String getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(String id)
{
this.id = id;
}
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the author
*/
public String getAuthor()
{
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author)
{
this.author = author;
}
/**
* @return the description
*/
public String getDescription()
{
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description)
{
this.description = description;
}
}