Servlet/Jsp實現購物車

來源:互聯網
上載者:User

標籤:snippet   app   xhtml   host   分享   oid   rect   red   gravity   

(1)用servlet實現簡單的購物車系統,項目結構例如以下:(建立web Project項目  僅僅須要AddItemServlet , ListItemServlet。exam403.jsp三個檔案就可以。其它的不用管)

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGxnZW4xNTczODc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >

(2)exam403.jsp代碼例如以下:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>無標題文檔</title></head><body><form id="form1" name="form1" method="post" action="/servletProject/addItem">  <label></label>  商品:  <select name="itemID" id="itemID">    <option value="洗衣粉">洗衣粉</option>    <option value="香皂">香皂</option>    <option value="食用油">食用油</option>  </select>  <p>數量:    <label>    <input name="quantity" type="text" id="quantity" value="1" />    </label>    <label>    <input type="submit" name="Submit" value="提交" />    </label>    <a href="/servletProject/listItem">查看購物車</a></p></form></body></html>

(3)AddItemServlet代碼例如以下:

package com.lc.shoppingCar;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;public class AddItemServlet extends HttpServlet {    protected void doGet(HttpServletRequest request,HttpServletResponse response)                         throws ServletException,java.io.IOException   {      ServletContext application=getServletContext() ;      ServletConfig config=getServletConfig() ;      response.setContentType("text/html;charset=gb2312");      PrintWriter out=response.getWriter();       HttpSession session =request.getSession();      request.setCharacterEncoding("gb2312");            //讀取表單傳入的商品ID及數量     String id=request.getParameter("itemID");  String num=request.getParameter("quantity"); if(id!=null && num.length()!=0) {  //從Sessionn中讀取購物車HashMap shoppingCar=(HashMap)session.getAttribute("shoppingCar");    if(shoppingCar==null)        shoppingCar=new HashMap();    //將商品加入到購物車中    String onum=(String)shoppingCar.get(id);    if(onum==null)        shoppingCar.put(id,num);    else    {       int n1=Integer.parseInt(num);   int n2=Integer.parseInt(onum);   String result=String.valueOf(n1+n2);   shoppingCar.put(id,result);     }            //將購物車寫回session中儲存     session.setAttribute("shoppingCar",shoppingCar);    }   else  //假設傳入的商品ID號為空白或數量為空白。顯示提示資訊     System.out.print("商品ID號為空白會或數量為空白!");  //返回商品列表頁  response.sendRedirect("/servletProject/exam403.jsp");         }      protected void doPost(HttpServletRequest request,HttpServletResponse response)                                throws ServletException,java.io.IOException  {     doGet(request,response);  }}

(4)ListItemServlet代碼例如以下:

package com.lc.shoppingCar;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;public class ListItemServlet extends HttpServlet {    protected void doGet(HttpServletRequest request,HttpServletResponse response)                         throws ServletException,java.io.IOException   {      ServletContext application=getServletContext() ;      ServletConfig config=getServletConfig() ;      response.setContentType("text/html;charset=gb2312");      PrintWriter out=response.getWriter();       HttpSession session =request.getSession();      request.setCharacterEncoding("gb2312");             //從session中擷取購物車HashMap shoppingCar=(HashMap)session.getAttribute("shoppingCar");  //顯示購物車中的內容   if(shoppingCar!=null)   {      Set show=shoppingCar.entrySet();      Iterator it=show.iterator();      while(it.hasNext())      {        out.print(it.next()+"<br>");       }   }   else     out.print("購物車為空白。");       }      protected void doPost(HttpServletRequest request,HttpServletResponse response)                                throws ServletException,java.io.IOException  {     doGet(request,response);  }}

(5)實現效果例如以下:

訪問:http://localhost:8080/servletProject/exam403.jsp    學則商品 提交 


點擊查看購物車:


OK!

簡單的購物車 到此結束!

Servlet/Jsp實現購物車

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.