简单购物车!
写了个小练习 下面这段servlet代码作为首页 首先访问 HttpSession session = request.getSession(); 创建了一个session
package com.mth.shopping;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ListBook extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Map<String, Book> map = DB.getMap();
session.setAttribute("map", map);
String url=response.encodeRedirectURL("/Session/BookIndex.jsp");
System.out.println(url);
response.sendRedirect(url);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
但是在另外一个servlet里面 HttpSession session = request.getSession(false); 竟然获取到空的session
package com.mth.shopping;
import java.io.IOException;
import java.util.ArrayList;
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;
import javax.servlet.http.HttpSession;
public class BuyBookServlet extends HttpServlet {
// private List<Book> list = null;
@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String bookId = request.getParameter("id");
Book book = DB.getMap().get(bookId);
HttpSession session = request.getSession(false);
List<Book> list = (List<Book>) session.getAttribute("list");
if (list == null) {
list = new ArrayList<Book>();
session.setAttribute("list", list);
}
list.add(book);
String url = response.encodeRedirectURL("/Session/ListCart");
response.sendRedirect(url);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
请教各位大神 为什么 ?
严重: Servlet.service() for servlet BuyBookServlet threw exception
java.lang.NullPointerException
at com.mth.shopping.BuyBookServlet.doGet(BuyBookServlet.java:23)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.mth.filter.EncodeFilter.doFilter(EncodeFilter.java:25)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
------解决方案--------------------
session.setAttribute("map", map);放进去session名字是map, List<Book> list = (List<Book>) session.getAttribute("list"); 取的时候取list?
还有为什么list为空的时候
if (list == null) {
list = new ArrayList<Book>();
session.setAttribute("list", list);
}
又把list设置进去?那你取它还有什么意义?
------解决方案--------------------