} } }

    Session简单实现购物功能

    添加时间:2013-6-11 点击量:

    1,IndexServlet.java 主页Servlet实现主页显示商品



        import java.io.IOException;  
    
    import java.io.PrintWriter;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Set;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    //首页:列出所有书
    public class IndexServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType(
    text/html;charset=UTF-8);
    PrintWriter out
    = response.getWriter();
    request.getSession();
    out.write(
    本网站有如下书:<br/>);
    Set
    <Map.Entry<String,Book>> set = DB.getAll().entrySet();
    for(Map.Entry<String,Book> me : set){
    Book book
    = me.getValue();
    String url
    = /day07/servlet/BuyServlet?id= + book.getId();
    url
    = response.encodeURL(url);
    out.println(book.getName()
    + <a href=+url+>购买</a><br/>);
    }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request, response);
    }

    }


    2,BuyServlet.java 该Servlet实现购买页面



        import java.io.IOException;  
    
    import java.util.ArrayList;
    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;
    public class BuyServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    String id
    = request.getParameter(id);
    Book book
    = (Book) DB.getAll().get(id); //获得用户想买的书
    HttpSession session = request.getSession();
    /Cookie cookie = new Cookie(JSESSIONID,session.getId());
    cookie.setMaxAge(3060);
    cookie.setPath(/day07);
    response.addCookie(cookie);
    /
    List list
    = (List) session.getAttribute(list); //获得用户用于保存所有书的容器
    if(list==null){
    list
    = new ArrayList();
    session.setAttribute(
    list, list);
    }
    list.add(book);
    //request.getRequestDispatcher(/servlet/ListCartServlet).forward(request, response);
    String url = response.encodeRedirectURL(/day07/servlet/ListCartServlet);//从头URL
    //System.out.println(url);
    response.sendRedirect(url);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request, response);
    }

    }


    3,ListCartServlet.java 实现将购买的商品经由过程Session保存发给ListCartServlet,显示你购买的商品



        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;
    public class ListCartServlet 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();
    List
    <Book> list = (List) session.getAttribute(list);
    if(list==null || list.size()==0){
    out.write(
    对不起,您还没有购买任何商品!!);
    return;
    }
    //显示用户买过的商品
    out.write(您买过如下商品:<br>);
    for(Book book : list){
    out.write(book.getName()
    + <br/>);
    }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    doGet(request, response);
    }

    }

    无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》
    分享到: