练习cookie做一个浏览商品记录显示,第一次会显示浏览过的商品,然后在浏览第二次就出现空指针异常
2016-6-16 16:51:48 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [CookieDemo2] in context with path [/testHttp] threw exception
java.lang.NullPointerException
at cn.itcast.cookie.CookieDemo2.doGet(CookieDemo2.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:619)
CookieDemo2.java
package cn.itcast.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 CookieDemo2 extends HttpServlet {
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();
//1.输出网站所有商品
out.write("本网站有如下商品:
");
Map map = Db.getAll();
for(Map.Entry entry:map.entrySet()){
Book book = entry.getValue();
out.print(""+book.getName()+"
");
}
//2.显示用户浏览过的商品
out.print("
您曾经浏览过的商品:
");
Cookie cookies[] = request.getCookies();
for(int i=0;cookies!=null && i
if(cookies[i].getName().equals("bookHistory")){
String ids[] = cookies[i].getValue().split("\\,");
for(String id:ids){
Book book = (Book) Db.getAll().get(id);
out.print(book.getName()+"
");
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
class Db{
private static Map map = new LinkedHashMap();
static{
map.put("1",new Book("1","javaweb开发","老张","一本好书!!"));
map.put("2",new Book("2","jdbc开发","老张","一本好书!!"));
map.put("3",new Book("3","spring开发","老黎","一本好书!!"));
map.put("4",new Book("4","struts开发","老毕","一本好书!!"));
map.put("5",new Book("5","android开发","老黎","一本好书!!"));
}
public static Map getAll(){
return map;
}
}
class Book{
private String id;
private String name;
private String author;
private String description;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String id, String name, String author, String description) {
super();
this.id = id;
this.name = name;
this.author = author;
this.description = description;
}
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 getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
CookieDemo3.java
package cn.itcast.cookie;
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 CookieDemo3 extends HttpServlet {
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();
//1.根据用户带过来的id,显示相应商品的详细信息
String id = request.getParameter("id");
Book book = (Book) Db.getAll().get(id);
out.write(book.getId()+"
");
out.write(book.getName()+"
");
out.write(book.getAuthor()+"
");
out.write(book.getDescription()+"
");
//构建cookie,回写给浏览器
String cookieValue = buildCookie(id,request);
Cookie cookie = new Cookie("bookHistory",cookieValue);
cookie.setMaxAge(1*30*24*60*60);
cookie.setPath("/testHttp");
response.addCookie(cookie);
}
private String buildCookie(String id, HttpServletRequest request) {
String bookHistory = null;
Cookie cookies[] = request.getCookies();
for(int i=0;cookies!=null && i
if(cookies[i].getName().equals("bookHistory")){
bookHistory = cookies[i].getValue();
}
}
if(bookHistory==null){
return id;
}
LinkedList list = new LinkedList(Arrays.asList(bookHistory.split("//,")));
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+",");
}
sb.deleteCharAt(sb.length()-1).toString();
return null;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}