Java Servlet实现购物车(Session实例)

10/25/2009来源:JSP技巧人气:22236

1.jsp代码:(购物form)

--
    Document   : testsession
    Created on : 2009-10-9, 15:05:55
    Author     : lucifer
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         <center>
              <h1>电器商场</h1>
         <form method="post" action="SessionServlet">
              <input type="checkbox" name="item" value="0">电视机<br>
              <input type="checkbox" name="item" value="1">电冰箱<br>
              <input type="checkbox" name="item" value="2">电脑<br>
              <input type="submit" name="submit" value="加入购物车">
         </form>
         </center>
    </body>
</html>

 

 

 

2.SessionServlet:


package MyServlet;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 *
 * @author lucifer
 */
public class SessionServlet extends HttpServlet {

    PRotected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String[] items = {"电视机","电冰箱","电脑"};
        HttpSession sess = request.getSession(true);
        Integer itemCount = (Integer)sess.getValue("itemCount");
        if(itemCount == null){
             itemCount = new Integer(0);
        }
        try {
            String[] itemSelected;
            String itemName;
            itemSelected = request.getParameterValues("item");
            if(itemSelected != null){
                 for(int i = 0;i < itemSelected.length;i++){
                      itemName = itemSelected[i];
                      itemCount = new Integer(itemCount.intValue() + 1);
                      sess.putValue("Item" + itemCount,itemName);
                      sess.putValue("itemCount",itemCount);
                 }
            }
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SessionServlet</title>"); 
            out.println("</head>");
            out.println("<body>");
            out.println("<center><h1>您购物车中的东西为:</h1>");
            out.println("共挑选了" + itemCount + "件电器<br>");
            for(int i = 1;i <= itemCount.intValue();i++){
                 String item = (String)sess.getValue("Item" + i);
                 out.println(items[Integer.parseInt(item)]);
                 out.println("<br>");
            }
            out.println("<a href = \"ShowCart.jsp\">继续挑选</a>");
            out.println("</center></body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}