Servlet——利用Session完成的简单购物车
1.创建登录页面
login.html
登录
2.购物页面
buy.html
购买
3.实体类的创建
/day8_shopping_car/src/com/myself/domain/Car.java
package com.myself.domain;
import java.util.ArrayList;
import java.util.List;
/**
* 购物车类,可添加,删除和获取购物车项
* @author Jungor
*
*/
public class Car {
// 用于存放CarItem(购物车项)的list
private List list = new ArrayList();
/**
* 获取购物车中的所有购物车项
* @return 包含所有购物车项的List
*/
public List list() {
return list;
}
/**
* 添加购物车项到购物车
* @param carItem 需要添加的购物车项
*/
public void add(CarItem carItem) {
this.list.add(carItem);
}
/**
* 从购物车中删除购物车项
* @param id 需要删除的购物车项ID
*/
public void remove(String id) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)) {
list.remove(i);
break;
}
}
}
}
/day8_shopping_car/src/com/myself/domain/CarItem.java
package com.myself.domain;
/**
* 购物车项,包含id,货品名,数量
* @author Jungor
*
*/
public class CarItem {
private String id;
private String product;
private Integer number;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
/day8_shopping_car/src/com/myself/domain/User.java
package com.myself.domain;
/**
* 用户类,用于封装用户登录信息
* @author Jungor
*
*/
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [" + username + ", " + password + "]";
}
}
4.创建Servlet
/day8_shopping_car/src/com/myself/servlet/BuyServlet.java
package com.myself.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.myself.domain.Car;
import com.myself.domain.CarItem;
import com.myself.domain.User;
/**
* 购买时需要的Servlet,可以将一个购物车项添加到购物车
* @author Jungor
*
*/
public class BuyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取打印流
PrintWriter out = response.getWriter();
// 获取货品名称
String product = request.getParameter("product");
Integer number = null; // 数量
try {
// 接收到的是一个String,将其转换为Integer,如果转换失败,则向页面输出提示信息
number = Integer.parseInt(request.getParameter("number"));
} catch (NumberFormatException e) {
out.println("数量非法,重新填写
");
return;
//e.printStackTrace();
}
// 通过JSESSIONID,获取session对象,如果没有获取到,则新创建一个session对象
HttpSession session = request.getSession();
// 在session中获取user属性的对象
User user = (User) session.getAttribute("user");
// 在session中获取car属性的对象
Car car = (Car) session.getAttribute("car");
// 只有当用户已登录,且货品和数量不为空时,才允许添加项到购物车
if (user != null && product != null && number != null) {
CarItem carItem = new CarItem(); // 创建购物车项
// UUID.randomUUID().toString(),一个随机且不重复的字符串,方便购物车项的查询
carItem.setId(UUID.randomUUID().toString());
carItem.setProduct(product);
carItem.setNumber(number);
// 如果购物车为空,则创建一个购物车,并添加到Session
if (car == null) {
car = new Car();
session.setAttribute("car", car);
}
car.add(carItem);
out.println("购买成功
");
out.println("继续购买
");
out.println("管理列表
");
} else {
out.println("请登录
");
}
}
}
/day8_shopping_car/src/com/myself/servlet/DeleteServlet.java
package com.myself.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.myself.domain.Car;
import com.myself.domain.User;
/**
* 删除购物加中的购物车项
* @author Jungor
*
*/
public class DeleteServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取输出流
PrintWriter out = response.getWriter();
// 获取session,如果使用JSESSIONID没有找到则创建一个
HttpSession session = request.getSession();
// 获取session中属性名为user的对象
User user = (User) session.getAttribute("user");
// 获取session中属性名为car的对象
Car car = (Car) session.getAttribute("car");
// 接收需要删除的购物车项的id
String id = request.getParameter("id");
// 如果用户已登录,则向下执行,否则向页面输出提示信息
if (user != null) {
// 当car对象存在时,才从此购物车中删除购物车项,且转入的id也不为空
if (car != null && id != null && !"".equals(id)) {
car.remove(id);
out.println("删除成功
");
out.println("继续购买
");
out.println("管理列表
");
} else {
out.println("也还没车车...");
}
} else {
out.println("还没登录,禁止操作。返回登录");
}
}
}
/day8_shopping_car/src/com/myself/servlet/ListServlet.java
package com.myself.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.myself.domain.Car;
import com.myself.domain.CarItem;
import com.myself.domain.User;
/**
* 用于显示购物车里面的内容
* @author Jungor
*
*/
public class ListServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取输出流
PrintWriter out = response.getWriter();
// 获取session,如果使用JSESSIONID没有找到则创建一个
HttpSession session = request.getSession();
// 获取session中属性名为car的对象
Car car = (Car) session.getAttribute("car");
// 获取session中属性名为user的对象
User user = (User) session.getAttribute("user");
// 如果user对象为空,则表示还没登录,要求用户登录后才能进行操作
if (user == null) {
out.println("还没登录。请登录");
return;
}
out.println("" + user.getUsername() + " 的购物车
");
// 如果car对象为空,表示还没创建购物车,如果不为空,就表示有购物车,可以依次输出其内容
if (car != null) {
for (CarItem carItem : car.list()) {
out.println("商品:" + carItem.getProduct() + " 数量:" + carItem.getNumber() + " 删除");
out.println("
");
}
} else {
out.println("还没购物车,所以没内容");
}
}
}
/day8_shopping_car/src/com/myself/servlet/LoginServlet.java
package com.myself.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.myself.domain.User;
/**
* 用户登录,将登录信息封装成一个User对象,并添加到Session
* @author Jungor
*
*/
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取输出流
PrintWriter out = response.getWriter();
// 接收到的参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 获取session,如果使用JSESSIONID没有找到则创建一个
HttpSession session = request.getSession();
// 获取session中属性名为user的对象
User user = (User) session.getAttribute("user");
// 如果user对象为空,用户名和密码不为空,则将登录信息封装为一个User对象,并添加至session中
if (user == null
&& username != null && !"".equals(username)
&& password != null &&!"".equals(password)) {
// 将username和password封装成一个User对象
user = new User();
user.setPassword(password);
user.setUsername(username);
// 将对象添加到session中
session.setAttribute("user", user);
out.println("欢迎回来:" + user.getUsername() + " 注销
");
out.println("购物
");
out.println("管理购物");
} else if (user != null && username == null && password == null) {
//当user对象不为空,但username和password为空,直接读取session中user对象
out.println("欢迎回来:" + user.getUsername() + " 注销
");
out.println("购物
");
out.println("管理购物");
}else {
// 当前面条件不满足时,向页面输出提示信息
out.println("重新登录");
}
}
}
/day8_shopping_car/src/com/myself/servlet/LogoutServlet.java
package com.myself.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.myself.domain.User;
/**
* 用户注销
* @author Jungor
*
*/
public class LogoutServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取session
HttpSession session = request.getSession();
// 注销,让session失效,同时清除user和car在session中的对象
session.invalidate();
// 注销后,重定向到登录页面
response.sendRedirect("/login.html");
}
}
5.web.xml注册
index.html index.htm index.jps LoginServlet com.myself.servlet.LoginServlet LoginServlet /login BuyServlet com.myself.servlet.BuyServlet BuyServlet /buy ListServlet com.myself.servlet.ListServlet ListServlet /list DeleteServlet com.myself.servlet.DeleteServlet DeleteServlet /delete LogoutServlet com.myself.servlet.LogoutServlet LogoutServlet /logout