基于javaweb+JSP+Servlet简单购物车
开发工具:eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package entity;
public class Cart {
private String goodsname;
public void setData(String sql) {
Statement stm = null;
try {
stm = con.createStatement();
stm.executeUpdate(sql);
System.out.println(" setData!!!");
} catch (SQLException e) {
System.out.println("SQLException!!!");
e.printStackTrace();
} finally {
if (stm != null) {
try {
<a href="register.jsp">注册</a><%} %>
<hr>
<table border="1">
<tr>
<th>序号</th>
<th>商品</th>
<th>价格</th>
<th>操作</th>
</tr>
<%
for(int i=1;i<=goods.size();i++){
Goods g=goods.get(i-1);
%>
<a href="<%=request.getContextPath()%>/generate.order">生成订单</a> <a href="<%=request.getContextPath()%>/show.cart");">返回</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String msg=(String)request.getAttribute("msg"); %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户注册</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>用户注册</h1>
<hr/>
<form action="<%=request.getContextPath()%>/register.user" method="post">
<table>
<tr>
<td>用户名:</td>
<td>
<input type="text" name="username">
User u=(User)req.getSession().getAttribute("user");
if(u==null) {
resp.sendRedirect(req.getContextPath()+"/login.jsp");
return;
}
String tag = req.getParameter("tag");
if("AddCart".equals(tag)) {
String index=req.getParameter("index");
resp.sendRedirect(req.getContextPath()+"/add.cart?index="+index);
}
else{
resp.sendRedirect(req.getContextPath()+"/show.cart");
}
}
}
package mysql;
import java.sql.*;
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
if("/confirm.order".equals(path)){
confirm(req,resp);
}
if("/generate.order".equals(path)){
generate(req,resp);
}
if("/showOrders.order".equals(path)){
showOrders(req,resp);
}
if("/detail.order".equals(path)){
detail(req,resp);
}
//总计
Double sum=0.0;
for(Order o:order){
sum+=o.getPrice();
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>我的订单</h1>
<hr>
<%=order.get(0).getUsername() %>用户<%=order.get(0).getId() %>订单
<hr>
<table border="1">
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User() {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
db.close();
}
protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User u=(User)req.getSession().getAttribute("user");
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path = req.getServletPath();
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
if("/logout.user".equals(path)){
logout(req,resp);
}
if("/check.user".equals(path)){
check(req,resp);
}
}
protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
String phone=req.getParameter("phone");
String address=req.getParameter("address");
DataBase db=new DataBase();
ResultSet rs = db.getData("SELECT * FROM t_user where un='"+username+"'");
try {
try {
stm = con.createStatement();
ResultSet result = stm.executeQuery(sql);
System.out.println("getData!!!");
return result;
} catch (SQLException e) {
System.out.println("SQLException!!!");
e.printStackTrace();
return null;
}
}
public void setData(String sql) {
Statement stm = null;
try {
stm = con.createStatement();
stm.executeUpdate(sql);
System.out.println(" setData!!!");
} catch (SQLException e) {
System.out.println("SQLException!!!");
e.printStackTrace();
} finally {
if (stm != null) {
try {
运行环境
Java≥8、Tomcat≥8.0、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
技术框架
JSP Servlet MySQL JDBC Tomcat
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
注册、登录、注销
商品加入购物车,查看购物车,删除购物车中的商品和清空购物车
生成订单,查看历史订单及订单详情等