欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Jsp+Servlet实现购物车功能

程序员文章站 2023-11-13 15:14:22
本文实例为大家分享了servlet实现购物车功能的具体代码,供大家参考,具体内容如下 (1)用servlet实现简单的购物车系统,项目结构例如以下:(新建web pr...

本文实例为大家分享了servlet实现购物车功能的具体代码,供大家参考,具体内容如下

(1)用servlet实现简单的购物车系统,项目结构例如以下:(新建web project项目  仅仅须要additemservlet , listitemservlet。exam403.jsp三个文件就可以。其它的不用管)

Jsp+Servlet实现购物车功能 

(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    学则商品 提交

Jsp+Servlet实现购物车功能

点击查看购物车:

Jsp+Servlet实现购物车功能

ok!

简单的购物车 到此结束!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。