登录处理测试
package net.lmq.shop.servlet;
import net.lmq.shop.bean.User;
import net.lmq.shop.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求对象的编码
request.setCharacterEncoding("utf-8");
//获取会话对象session
HttpSession session = request.getSession();
//得到用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//定义用户服务对象
UserService userService = new UserService();
//执行登录方法,返回结果
User user = userService.login(username,password);
//判断用户是否登录成功
if(user != null){
//设置session属性
session.setMaxInactiveInterval(5*60);
session.setAttribute("username",username);
session.removeAttribute("loginMsg");
if(user.getPopedom() == 0){
System.out.println("用户登录成功,进入后台管理");
response.sendRedirect(request.getContextPath()+"/backend/management.jsp");
}else if(user.getPopedom() == 1){
System.out.println("用户登录成功,进入前台显示类别");
response.sendRedirect(request.getContextPath()+"/showCategory");
}
}else{
System.out.println("用户名或密码错误,请重新登录");
session.setAttribute("loginMsg","用户名或密码错误");
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
普通用户登录在地址栏输入用户名和密码,
跳转后页面显示
控制台输出信息
管理者用户登录:
页面跳转
控制台输出
登录失败:
页面跳转
控制台输出
注销servlet
package net.lmq.shop.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//让session失效
request.getSession().invalidate();
//重定向到登录页面
response.sendRedirect(request.getContextPath()+"/login.jsp");
System.out.println("用户注销成功");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
页面跳转:
控制台输出
注册servlet
package net.lmq.shop.servlet;
import net.lmq.shop.bean.User;
import net.lmq.shop.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求对象的编码
request.setCharacterEncoding("utf-8");
//获取session
HttpSession session = request.getSession();
//获取页面用户信息
String username = request.getParameter("username");
String password = request.getParameter("password");
String telephone = request.getParameter("telephone");
Timestamp registerTime = new Timestamp(System.currentTimeMillis());
//设置为普通用户
int popedom = 1;
//创建用户对象
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setTelephone(telephone);
user.setRegisterTime(registerTime);
user.setPopedom(popedom);
//创建用户服务对象
UserService userService = new UserService();
//调用添加用户方法
int count = userService.addUser(user);
//判断是否注册成功
if(count > 0){
//设置session
session.setAttribute("registerMsg","恭喜注册成功");
//重定向跳转
response.sendRedirect(request.getContextPath()+"/login.jsp");
//控制台输出
System.out.println("恭喜,用户注册成功,跳转到登录页面");
}else {
//设置session
session.setAttribute("registerMsg","抱歉,用户注册失败");
//重定向跳转注册页面
response.sendRedirect(request.getContextPath()+"/frontend/register.jsp");
//控制台输出
System.out.println("抱歉,用户注册失败,跳转到注册页面");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
注册成功
页面信息:
跳转到
控制台输出
查看数据库,看是否添加用户:添加了的
注册失败
页面跳转
控制台输出
按着老师的改了一些DaoImpl
控制台输出:不再显示报报错
再添加一个判断,判断用户是否存在,
如果用户已经存在,控制台显示这个
商品类别显示Servlet
package net.lmq.shop.servlet;
import net.lmq.shop.bean.Category;
import net.lmq.shop.service.CategoryService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/showCategory")
public class ShowCategoryServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建商品类别服务对象
CategoryService categoryService = new CategoryService();
//获取全部商品类别
List<Category> categories = categoryService.findAllCategories();
//获得session
HttpSession session = request.getSession();
//将商品类别放到session里
session.setAttribute("categories",categories);
//重定向跳转到显示商品类别显示页面
response.sendRedirect(request.getContextPath()+"/frontend/showCategory.jsp");
//在控制台输出测试信息
for(Category category:categories){
System.out.println(category);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
测试
控制台输出商品类别信息:
显示商品信息servlet
package net.lmq.shop.servlet;
import net.lmq.shop.bean.Product;
import net.lmq.shop.service.CategoryService;
import net.lmq.shop.service.ProductService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/showProduct")
public class ShowProductServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取商品类别标识
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
//创建商品类别服务对象
CategoryService categoryService = new CategoryService();
//由标识得到商品类别名称
String categoryName = categoryService.findCategoryById(categoryId).getName();
//创建商品服务对象
ProductService productService = new ProductService();
//获取指定商品类别的商品
List<Product> products = productService.findProductsByCategoryId(categoryId);
//获取session
HttpSession session = request.getSession();
//将商品信息放到session
session.setAttribute("products",products);
//重定向到显示商品信息页面
response.sendRedirect(request.getContextPath()+"/frontedn/showProduct.jsp?categoryName="+categoryName);
//在控制台显示商品信息
for(Product product:products){
System.out.println(product);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
页面输入
页面跳转到
控制台输出
添加控制,判断类别里是否有商品
没有类别时控制台显示
添加订单
package net.lmq.shop.servlet;
import net.lmq.shop.bean.Order;
import net.lmq.shop.service.OrderService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/makeOrder")
public class MakeOrderServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求对象的字符编码
request.setCharacterEncoding("utf-8");
// 获取表单提交数据
String username = request.getParameter("username");
String telephone = request.getParameter("telephone");
Double totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
String deliveryAddress = request.getParameter("deliveryAddress");
// 创建订单对象
Order order = new Order();
// 设置订单属性
order.setUsername(username);
order.setTotalPrice(totalPrice);
order.setTelephone(telephone);
order.setDeliveryAddress(deliveryAddress);
order.setOrderTime(new Date());
// 创建订单服务
OrderService orderService = new OrderService();
// 添加订单
orderService.addOrder(order);
// 获取最后一个订单
Order lastOrder = orderService.findLastOrder();
// 保存到session里
request.getSession().setAttribute("lastOrder", lastOrder);
// 重定向到显示订单页面
response.sendRedirect(request.getContextPath() + "/frontend/showOrder.jsp");
System.out.println(lastOrder);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
控制台输出