package com.itheima.session.app1;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.itheima.domain.Book;
//查看购物车中的所有商品
public class ShowCartServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//
HttpSession session=request.getSession(false);
if(session==null){
out.write("对不起,您还没有购买任何东东");
//session.setAttribute("JSESSIONID", "没有ID");
//out.write(session.getId());//输出Session的jJSESSIONID
System.out.println("Session====="+session);
return;
}
List<Book> cart=(List<Book>) session.getAttribute("cart");
if(cart==null){ out.write("对不起,您还没有购买任何商品!!!!"); return;}
if(cart==null && cart.size()<1){
out.write("对不起,您还没有购买任何商品!!!!");
out.write(session.getId());//输出Session的jJSESSIONID
System.out.println(session.getId());
return;
}
//进行URL重写
String url=request.getContextPath()+"/servlet/ClearCartServlet";
url=response.encodeURL(url);
out.write("您购买的商品如下:<a href='"+url+"'>清空购物车</a><br/>");
for(Book b:cart){
out.write(b.getName()+"<br/>");
out.write("输出Session的jJSESSIONID======"+session.getId());//输出Session的jJSESSIONID
System.out.println("输出Session的jJSESSIONID======"+session.getId());
}
//进行URL重写
String url2=request.getContextPath()+"/servlet/ShowBooksServlet";
url2=response.encodeURL(url2);
out.write("<br/><a href='"+url2+"'>继续购物</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
|
|