当前位置:文档之家› 制作一个简单的电子商务网站

制作一个简单的电子商务网站

制作一个简单的电子商务网站
制作一个简单的电子商务网站

电子商务大作业

作业要求

制作一个简单的电子商务网站,具有以下功能:

1) 能进行用户注册、登录。用户信息保存在数据库中。

2) 能对商品信息进行维护:增加、删除、修改。商品信息保存在数据库中。

3) 实现简单的购物车功能,能对所选择的商品进行列表显示,并对价格进行统计。

纸质报告要求:

1) 实现过程说明

2) 数据库设计说明

3) 运行效果

4) 主要源代码

一、创建用户注册、登录。用户信息保存在数据库中

1) 创建数据库表

在MySQL 中创建一个名为homeworks 的数据库,并在该数据库中创建一张名为User的表格。字段名数据类型

2) 安装所需的第三方软件包

在testapp/WEB-INF 新建lib 目录,并将以下需要的第三方软件包拷贝到lib 目录下:jstl.jar、standard.jar、mysql-connector-java-5.0.7-bin.jar。

3) 配置 JDBC 数据源

web.xml页面类容如下:

xmlns:xsi=""

xsi:schemaLocation="

"

version="2.5">

javax.servlet.jsp.jstl.sql.dataSource

jdbc:mysql://localhost:3306/homeworks?user=root&password=root,com.mysql.jdbc.D river

Servlet and JSP Examples.

Servlet and JSP Examples

Test

Test

A test Servlet

test.ServletTest

Test

/Test

4) 建立和信息录入相关的 JSP 页面

Index.jsp:网站首页

Search in User Database

Welcome to my website home page

if you are a member please click login

if not then click on the registration


register.jsp:用于注册用户信息的录入。

<%@ page contentType="text/html" %>

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="fn" uri="" %>

User Entry Form

Registered User Interface

Please enter information about a user below:

Login Name:

${fn:escapeXml(loginNameError)}
Password:

value="${fn:escapeXml(param.password)}">

${fn:escapeXml(passwordError)}
First Name:

value="${fn:escapeXml(param.firstName)}">

${fn:escapeXml(firstNameError)}
Last Name:

value="${fn:escapeXml(https://www.doczj.com/doc/4c8710042.html,stName)}">

${fn:escapeXml(lastNameError)}
Email Address:

value="${fn:escapeXml(param.emailAddress)}">

${fn:escapeXml(emailAddressError)}(Use format )

validate.jsp:用于验证录入的用户信息

程序代码如下:

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="fmt" uri="" %>

value="Login missing" />

value="Password missing" />

value="First Name missing" />

value="Last Name missing" />

value="Email Address missing" />

store.jsp:用于将录入的信息保存到数据库中。

程序代码如下:

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="sql" uri="" %>

<%@ taglib prefix="fmt" uri="" %>

<%--

See if the user is already defined. If not, insert the info, else update it.

--%>

SELECT * FROM User

WHERE LoginName = ?

<%--

Deal with the date values: parse the register date and create a Date object from it, and create a new variable to hold the current date. --%>

INSERT INTO User

(LoginName, Password, FirstName, LastName, EmailAddress)

VALUES(?, ?, ?, ?, ?)

UPDATE User

SET Password = ?,

FirstName = ?,

LastName = ?,

EmailAddress = ?,

WHERE LoginName = ?

<%-- Get the new or updated data from the database --%>

SELECT * FROM User

WHERE LoginName = ?

<%-- Redirect to the confirmation page --%>

confirmation.jsp:用于显示已保存到数据的信息。

程序代码如下:

<%@ page contentType="text/html" %>

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="fn" uri="" %>

User Info Stored

This is the information stored in the homeworks database:

${fn:escapeXml(column.key)}:

${fn:escapeXml(column.value)}

二、用户登陆

login.jsp注册用户登陆界面

<%@ page contentType="text/html" %>

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="fn" uri="" %>

Search in User Database

Registered users Landing Interface

login Name:

Password:

Find.jsp在数据库中检索登陆界面所录入的信息<%@ taglib prefix="sql" uri="" %>

<%--

Execute query, with wildcard characters added to the parameter values used in the search criteria

--%>

SELECT loginName,Password FROM User WHERE loginName LIKE ?

AND Password LIKE ?

ORDER BY loginName

List.jsp显示用户登陆界面

<%@ page contentType="text/html" %>

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="sql" uri="" %>

<%@ taglib prefix="fn" uri="" %>

Result

Sorry, no user were found.

Welcome to the user:

${fn:escapeXml(row.loginName)}

Next You could management books information

三、商品信息

在数据库homeworks

Bookindex 书籍管理首页

Search in User Database

Management Books Information

IF you want to add books infomation and updata please check AddBook

IF you want to Management books infomation please check Management


Addbooks.jsp 添加书籍与修改页面信息:

<%@ page contentType="text/html" %>

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="fn" uri="" %>

User Entry Form

Please add or updata book informations:

Name:

value="${fn:escapeXml(https://www.doczj.com/doc/4c8710042.html,)}">

${fn:escapeXml(NameError)}
Price:

value="${fn:escapeXml(param.Price)}">

${fn:escapeXml(PriceError)}

validate1.jsp用于验证录入的书籍信息

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="fmt" uri="" %>

value="Name missing" />

value="Price missing" />

show.jsp将录入的书籍信息保存道数据库中

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="sql" uri="" %>

<%@ taglib prefix="fmt" uri="" %>

<%--

See if the user is already defined. If not, insert the

info, else update it.

--%>

SELECT * FROM books

WHERE Name = ?

<%--

Deal with the date values: parse the register date and create a Date

object from it, and create a new variable to hold the current date. --%>

value="${param.addbooksDate}"var="parsedaddbooksDate" pattern="yyyy-MM-dd" />

INSERT INTO books

(Name, Price)

VALUES(?, ?)

UPDATE books

SET Price = ?

WHERE Name = ?

<%-- Get the new or updated data from the database --%>

SELECT * FROM books

WHERE Name = ?

<%-- Redirect to the confirmation page --%>

Success.jsp显示书籍信息添加成功

<%@ page import="java.util.*" %>

<%@ page contentType="text/html;

charset=gb2312" %>

Search in User Database

Now time is: <%=new java.util.Date()%>

Add or updata the success of books information

search.jsp查找数据库中已保存的信息

<%@ taglib prefix="sql" uri="" %>

<%--

Execute query, with wildcard characters added to the parameter values used in the search criteria

--%>

SELECT * FROM books

WHERE Name LIKE ?

AND Price LIKE ?

ORDER BY Name

List1.jsp:用于显示查询到的所有数据。

<%@ page contentType="text/html" %>

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="sql" uri="" %>

<%@ taglib prefix="fn" uri="" %>

Result

Sorry, no books were found.

The following homeworks were found:

NamePriceDeleteDetail
${fn:escapeXml(https://www.doczj.com/doc/4c8710042.html,)}${fn:escapeXml(row.Price)}

value="${fn:escapeXml(https://www.doczj.com/doc/4c8710042.html,)}">

detail

delete.jsp删除数据库中保存的数据

<%@ taglib prefix="sql" uri="" %>

<%@ taglib prefix="c" uri="" %>

DELETE FROM books

WHERE Name = ?

四、购物车

catalog.jsp文件用于显示所有可供购买商品的列表、购物车内当前的物品和价格信息<%@ page language="java" contentType="text/html" %>

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="fmt" uri="" %>

<%@ taglib prefix="fn" uri="" %>

Product Catalog

Product Catalog

Please select a book from our catalog to read more about it and

decide if you like to purchase a copy:

class="test.CatalogBean"

/>

<%--

Generate a list of all products with links to the product page.

--%>

id="cart" scope="session"

class="test.CartBean"

/>

<%-- Show the contents of the shopping cart, if any --%>

Your shopping cart contains the following items:

${fn:escapeXml(https://www.doczj.com/doc/4c8710042.html,)}$

type="currency" />


Total:$

type="currency" />

product.jsp用于显示当前商品的详细信息,并包将该商品加入购物车的链接<%@ taglib prefix="c" uri="" %>

class="test.CatalogBean"

/>

<%-- Get the specified ProductBean from the catalog --%>

id="cart"

scope="session"

class="test.CartBean"

/>

<%-- Add the product to the cart --%>

product.jsp产品信息

<%@ page language="java" contentType="text/html" %>

<%@ taglib prefix="c" uri="" %>

<%@ taglib prefix="fn" uri="" %>

Product Description

class="test.CatalogBean"

/>

<%-- Get the specified ProductBean from the catalog --%>

${fn:escapeXml(https://www.doczj.com/doc/4c8710042.html,)}

${fn:escapeXml(product.descr)}

Add this book to the shopping cart

购物车中相关的JVAV程序

ProductBean.java 用于描述商品信息

package test;

import java.io.*;

/**

* This class represents a product. It holds information about the

* product's name, description and price. All setter methods have

* package scope, since they are only used by the the CatalogBean. *

* @author Hans Bergsten, Gefion software <>

* @version 2.0

*/

public class ProductBean implements Serializable {

private String id;

private String name;

private String descr;

private float price;

/**

* Returns the product id.

*

* @return the product id

*/

public String getId() {

return id;

}

/**

* Returns the product name.

* @return the product name

*/

public String getName() {

return name;

}

/**

* Returns the product description.

*

* @return the product description

*/

public String getDescr() {

return descr;

}

/**

* Returns the product price.

*

* @return the product price

*/

public float getPrice() {

return price;

}

/**

* Sets the product id.

*

* @param id the product id

*/

void setId(String id) {

this.id = id;

}

/**

* Sets the product name.

*

* @param name the product name

*/

void setName(String name) {

https://www.doczj.com/doc/4c8710042.html, = name;

}

/**

* Sets the product description.

*

* @param descr the product description */

void setDescr(String descr) {

this.descr = descr;

}

/**

* Sets the product price.

*

* @param price the product price

*/

void setPrice(float price) {

this.price = price;

}

CatalogBean.java:用于创建和管理商品列表

package test;

import java.io.*;

import java.util.*;

/**

* This class represents a product catalog. It holds a list of

* products available for sale.

*

* This is just a demo so the product list is hardcoded, created

* at instantiation. A real version would get the information from * an external data source.

*

* @author Hans Bergsten, Gefion software <>

* @version 2.0

*/

public class CatalogBean implements Serializable {

private Map catalog = new HashMap();

/**

* Constructor. Creates all ProductBean objects and adds them * to the catalog.

*/

public CatalogBean() {

ProductBean prod = new ProductBean();

prod.setId("1000");

prod.setName("Java");

prod.setDescr(" ");

prod.setPrice(25f);

catalog.put("1000", prod);

prod = new ProductBean();

prod.setId("2000");

prod.setName("cad");

prod.setDescr(" ");

prod.setPrice(32f);

catalog.put("2000", prod);

prod = new ProductBean();

prod.setId("3000");

prod.setName("protral");

prod.setDescr(" ");

prod.setPrice(32f);

catalog.put("3000", prod);

}

/**

* Returns all products as an Iterator, suitable for looping.

*

* @return an Iterator for all ProductBean instances

*/

public ProductBean[] getProductList() {

ProductBean[] products = new ProductBean[catalog.size()]; catalog.values().toArray(products);

return products;

}

* Returns a Map with all ProductBean instances, keyed by ID. *

* @return an Map of all ProductBean instances

*/

public Map getProductsById() {

return catalog;

}

}

CartBean.java:用于管理购物车

package test;

import java.io.*;

import java.util.*;

/**

* This class represents a shopping cart. It holds a list of products. *

* @author Hans Bergsten, Gefion software <>

* @version 2.0

*/

public class CartBean implements Serializable {

private Vector cart = new Vector();

/**

* Adds a product to the cart, if it's not already there.

*

* @param product the ProductBean

*/

public void setProduct(ProductBean product) {

if (product != null && cart.indexOf(product) == -1) {

cart.addElement(product);

}

}

/**

* Returns the product list.

*

* @return an Iterator of ProductBeans

*/

public ProductBean[] getProductList() {

ProductBean[] products = null;

synchronized(cart) {

products = new ProductBean[cart.size()];

cart.toArray(products);

}

return products;

}

/**

* Returns the total price for all products in the cart

*

* @return the total price

*/

public float getTotal() {

float total = 0;

Iterator prods = cart.iterator();

while (prods.hasNext()) {

ProductBean product = (ProductBean) prods.next(); float price = product.getPrice();

total += price;

}

return total;

}

}

◆实验运行结果图INDEX.JSP:

REGISTER.JSP:

登陆longin.jsp

相关主题
文本预览
相关文档 最新文档