package day07;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
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 cookieDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//根据用户带过来的id,显示商品的详细信息
response.setCharacterEncoding(“utf-8”);
response.setContentType(“text/html;charset = utf-8”);
PrintWriter out = response.getWriter();
String id = request.getParameter(“id”);
book b = (book) Db.getAll().get(id);
out.write(b.getId()+”<br/>”);
out.write(b.getName()+”<br/>”);
out.write(b.getAuthor()+”<br/>”);
out.write(b.getDescription()+”<br/>”);
//构建cookie,会写给浏览器
String cookieValue = buildcookie(id,request);
Cookie cookie = new Cookie(“bookhistory”,cookieValue);
cookie.setMaxAge(1*30*24*3600);
cookie.setPath(“/day07”);
response.addCookie(cookie);
}
private String buildcookie(String id, HttpServletRequest request) {
//用户没有带cookie
//bookhistory = null 1 1
//bookhistory = 2,1,5 访问1 返回1,2,5
//bookhistory = 2,5,4 访问1 返回1,2,5
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();
}
}
LinkedList list = new LinkedList(Arrays.asList(bookhistory.split(“//,”)));
if(bookhistory == null) {
return id;
}
if(list.contains(id)) {
list.remove(id);
} else {
if(list.size() > 3) {
list.removeLast();
}
}
list.addFirst(id);
StringBuffer sb = new StringBuffer();
for(String bid : list) {
sb.append(bid+”,”);
return sb.deleteCharAt(sb.length()-1).toString();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
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 cookieDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//根据用户带过来的id,显示商品的详细信息
response.setCharacterEncoding(“utf-8”);
response.setContentType(“text/html;charset = utf-8”);
PrintWriter out = response.getWriter();
String id = request.getParameter(“id”);
book b = (book) Db.getAll().get(id);
out.write(b.getId()+”<br/>”);
out.write(b.getName()+”<br/>”);
out.write(b.getAuthor()+”<br/>”);
out.write(b.getDescription()+”<br/>”);
//构建cookie,会写给浏览器
String cookieValue = buildcookie(id,request);
Cookie cookie = new Cookie(“bookhistory”,cookieValue);
cookie.setMaxAge(1*30*24*3600);
cookie.setPath(“/day07”);
response.addCookie(cookie);
}
private String buildcookie(String id, HttpServletRequest request) {
//用户没有带cookie
//bookhistory = null 1 1
//bookhistory = 2,1,5 访问1 返回1,2,5
//bookhistory = 2,5,4 访问1 返回1,2,5
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();
}
}
LinkedList list = new LinkedList(Arrays.asList(bookhistory.split(“//,”)));
if(bookhistory == null) {
return id;
}
if(list.contains(id)) {
list.remove(id);
} else {
if(list.size() > 3) {
list.removeLast();
}
}
list.addFirst(id);
StringBuffer sb = new StringBuffer();
for(String bid : list) {
sb.append(bid+”,”);
return sb.deleteCharAt(sb.length()-1).toString();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
解决方案
20
LinkedList list = new LinkedList(Arrays.asList(bookhistory.split("//,"))); //这句改成 LinkedList<String> list = new LinkedList(Arrays.asList(bookhistory.split("//,")));
20
原因是你LinkedList list = new LinkedList(Arrays.asList(bookhistory.split(“//,”)));的时候没有指定泛型,
foreach的时候不能按照String类型获取。
第一中方式是按章楼上说的
第二种是for(String bid : list)list改为for(Object bid : list)list
foreach的时候不能按照String类型获取。
第一中方式是按章楼上说的
第二种是for(String bid : list)list改为for(Object bid : list)list