Servlet/Jsp实现购物车
(1)用servlet实现简单的购物车系统,项目结构例如以下:(新建web Project项目 仅仅须要AddItemServlet , ListItemServlet。exam403.jsp三个文件就可以。其它的不用管)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGxnZW4xNTczODc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
(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实现购物车的更多相关文章
- Servlet&&Jsp 概述
主题 Servlet的作用 构建动态网页 Servlet代码初探 Servlet与其他技术的对比 Jsp的作用 Servlet的作用 Servlet是在web服务器或应用服务器上用来动态生成html的 ...
- 面试题:servlet jsp cook session 背1
一.Servlet是什么?JSP是什么?它们的联系与区别是什么? Servlet是Java编写的运行在Servlet容器的服务端程序,狭义的Servlet是指Servlet接口,广义的Servlet是 ...
- NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
今天调试SSM框架项目后台JSOn接口,报出来一个让人迷惑的错误:NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config 上网查了一下别人的博 ...
- MVC开发模式之Servlet+jsp+javaBean
Servlet+jsp+JavaBean组合开发是一种MVC开发模式,控制器Controller采用Servlet.模型Model采用JavaBean.视图View采用JSP. 1.Web开发的请求- ...
- springMVC: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config
springMVC开发web的时候,报错:java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 原因:未引入jstl ...
- Struts框架——(一)用Servlet + JSP演示Struts基本原理
一. 用Servlet + JSP演示Struts基本原理 struts是开源项目.它通过采用 Java Servlet/JSP 技术,实现了基于Java EE Web应用的MVC的应用框架.Stru ...
- Javabean+servlet+JSP(html)实例应用
大家都知道Javabean+servlet+JSP是最简单的MVC模式.的确,在一个小型的项目中,这个模式完全够用. 它优雅并且简洁.加上jQueryui的完美展示效果,让这个模式看起来非常合适.当然 ...
- 报错:严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [/index.jsp (line: 13, column: 20) No tag "textfiled" defined in tag library imported with prefix
严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [ ...
- servlet+jsp+java实现Web 应用
servlet+jsp+java实现Web 应用 用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中.程序的结构很简单,也很清楚,本文主要记录下大概的开发过程和环 ...
随机推荐
- [转]软件版本号扫盲——Beta RC Preview release等
1.软件版本阶段说明 *Alpha版:此版本表示该软件在此阶段主要是以实现软件功能为主,通常只在软件开发者内部交流,一般而言,该版本软件的Bug较多,需要继续修改. *Beta版:该版本相对于α版 ...
- HDU 5296 Annoying problem dfs序 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Description Coco has a tree, w ...
- PAT甲级1095. Cars on Campus
PAT甲级1095. Cars on Campus 题意: 浙江大学有6个校区和很多门.从每个门口,我们可以收集穿过大门的汽车的进/出时间和车牌号码.现在有了所有的信息,你应该在任何特定的时间点告诉在 ...
- SecureCRT也能和Xshell一样批量导入主机
在Xshell可以像这样一个文件批量导入主机: https://blog.netsarang.com/91/importing-csv-formatted-host-information-to-xs ...
- gropf
http://blog.csdn.net/yetyongjin/article/details/7717464 带-pg编译程序$ gcc -pg -o test test.c先运行程序$ ./tes ...
- linq Distinct 去除重复数据
转载:http://www.cnblogs.com/ldp615/archive/2011/08/01/distinct-entension.html 只可惜linq默认不支持.Distinct(p ...
- ionic开发环境搭建之ios
前言 公司在做完ionic androud版后就开始做ios版,虽然ios的坑我觉得比起androud少了很多,但是作为第一次接触ios的我来说,环境实在太麻烦,从搭环境到打包一个正式版的ios ap ...
- Entity Framework 6 vs NHibernate 4
This article is dedicated to discussing the latest releases of the NHibernate and Entity Framework. ...
- Javascript:拦截所有AJAX调用,重点处理服务器异常
背景 上篇文章http://www.cnblogs.com/happyframework/p/3241063.html介绍了如何以AOP的形式处理服务器异常,这让服务器端的编程逻辑变的非常整洁,本文介 ...
- MNI模板和Talairach 模板的对比
The MNI brain and the Talairach atlas SPM 96 and later use standard brains from the Montreal Neurolo ...