jsp实现购物程序

2016-01-29 12:06 10 1 收藏

jsp实现购物程序,jsp实现购物程序

【 tulaoshi.com - Java 】

  下面是我用JSP和数据库做的购物车的源程序:

注意:
1、重新计费部分还没有做好,大家自己动手吧!
2、下一版本将用session做。

//shop_cart.jsp

<%@ page contentType="text/html;charset=gb2312"%
<%@ page session="true" %
<%@ page language="java" import="java.sql.*" %
<jsp:useBean id="bka" scope="page" class="shop.bka" /
<%

String product_type;
String action;
int product_id;
int curpage;

//商品类型
if (request.getParameter("product_type")==null){
product_type="all";
}else{
product_type=request.getParameter("product_type");
}

//页数和商品类型参数,可以在“继续购物”时返回到上次购物的页面
if (request.getParameter("curpage")==null){
curpage=1;
}else{
curpage=java.lang.Integer.parseInt(request.getParameter("curpage"));
}

//动作
if (request.getParameter("action")==null){
action="view";
}else{
action=request.getParameter("action");
}

//商品编号
if (request.getParameter("product_id")==null){
product_id=0;
}else{
product_id=java.lang.Integer.parseInt(request.getParameter("product_id"));
}

int bbb;
bbb=1;

Integer num = new Integer(bbb);

//商店编号
session.putValue("shop_id",num);
//顾客username
session.putValue("guest_name","asp2001");

String guest_name=(String)session.getValue("guest_name");
Integer shop_id=(Integer)session.getValue("shop_id");


java.lang.String sql;
java.sql.ResultSet rs;


if (action.compareTo("add")==0) {

sql="select cart_quantity from shop_cart where cart_shop_id=" + shop_id

+ " and cart_guest_id='" + guest_name + "' and cart_product_id=" + product_id ;
rs = bka.executeQuery(sql);
if (rs.next()){
int cart_quantity;
cart_quantity=java.lang.Integer.parseInt(rs.getString("cart_quantity"))+1;
sql="update shop_cart set cart_quantity=" + cart_quantity + " where cart_shop_id="  

+ shop_id + " and cart_guest_id='" + guest_name + "' and cart_product_id=" + product_id ;
rs = bka.executeQuery(sql);}
else
{
sql="insert into shop_cart (cart_shop_id,cart_guest_id,cart_product_id,cart_quantity) values ('" + shop_id + "','"  

+ guest_name + "','" + product_id + "',1)";
rs = bka.executeQuery(sql);
}
}

if (action.compareTo("clear")==0) {
sql="delete from shop_cart where cart_shop_id=" + shop_id + " and cart_guest_id='" + guest_name + "'";
rs = bka.executeQuery(sql);
}

if (action.compareTo("delete")==0) {
sql="delete from shop_cart where cart_shop_id=" + shop_id + " and cart_guest_id='"

+ guest_name + "' and cart_product_id=" + product_id ;
rs = bka.executeQuery(sql);
}
%
<div align="center"<center

<table border="0" cellpadding="0" cellspacing="0" width="610"

height="2"
<tr
<td<form method="POST"

action="shop_cart.jsp?action=update&product_type=<%=product_type%&curp

age=<%=curpage%"
<table border="0" cellpadding="0" cellspacing="0" width="610"

height="2"
<tr
<td width="122" height="7"<div align="center"<center<table

border="1" cellpadding="2" cellspacing="0" width="100%" bordercolorlight="#FFB468"

bordercolordark="#FFFFFF" bgcolor="#FFB468"

来源:https://www.tulaoshi.com/n/20160129/1484621.html

延伸阅读
标签: Java JAVA基础
  JSP的login程序代码 <!-- Login Manager --> <JAVA TYPE="class"> public static boolean checkUserPermission(HttpSession mySession, HttpServletRequest request, HttpServletResponse response) { if (mySession.getValue("hasPermission") == null || !((Boolean) mySession.getValue ("hasPermission" )).booleanValue()...
标签: Java JAVA基础
<%@ page contentType="text/html;charset=gb2312"% <% String sCurrentLine; String sTotalString; sCurrentLine=""; sTotalString=""; java.io.InputStream l_urlStream; java.net.URL l_url = new java.net.URL("http://www.163.net/"); java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_ur...
标签: Web开发
我把ServletContextListener的实现类取名为ContextListener,在其内添加一个定时器,示例代码如下所示(为考虑篇幅,仅提供部分代码供读者参考):     代码:      private java.util.Timer timer = null;     public void contextInitialized(ServletContextEvent event) { &nbs...
标签: Web开发
静态的登录界面的设计login.htm,代码如下: html head     title系统登录/title     style type="text/css"... !-- .style1 {...}{     font-size: 18px;     font-weight: bold; } .style2 {...}{font-size: 24px} .style5 {...
标签: Web开发
  调用方法       img   src="http://...../getImg"           原理,在servlet中随机生成一个4位数字1000-9999       然后把这数字写入session       输出一个图片,上面写有这...

经验教程

471

收藏

5

精华推荐

JSP中图片验证的实现

JSP中图片验证的实现

张杰马玉涛

jsp 实现在线人数统计

jsp 实现在线人数统计

Chey_rey

7.3 在ASP中实现购物小车

7.3 在ASP中实现购物小车

真是王学刚

微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部