六、控制层
在src里创建net.cl.shop.servlet包,在里面创建各种控制处理类。
1、登录处理类LoginServlet
package net.cl.shop.servlet;
import java.io.IOException;
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 net.cl.shop.bean.User;
import net.cl.shop.service.UserService;
import net.cl.shop.bean.User;
import net.cl.shop.service.UserService;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求对象的字符编码
request.setCharacterEncoding("utf-8");
// 获取会话对象
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属性loginMsg
session.setAttribute("loginMsg", "用户名或密码错误!");
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
(1)、测试用户登录
在地址栏里localhost:8080/simonshop/之后输入login?username=admin&password=12345之后敲回车:
报错是因为没有编写management.jsp;我们在去看一下服务器端的控制台,结果如下:
重启服务器,再测试一下:
去看一下服务器端的控制台,结果如下:
重启服务器,再测试一下:
去看一下服务器端的控制台,结果如下:
2、注销处理类LogoutServlet
package net.cl.shop.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 让session失效
request.getSession().invalidate();
// 重定向到登录页面
response.sendRedirect(request.getContextPath() + "/login.jsp");
System.out.println("用户注销成功!");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
下面我们来进行测试。启动服务器,先要登录成功,然后再测试注销功能。
测试用户注销功能,在地址栏输入logout:
在查看客户端控制台:
3、注册处理类RegisterServlet
package net.cl.shop.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Timestamp;
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 net.cl.shop.bean.User;
import net.cl.shop.service.UserService;
import net.cl.shop.bean.User;
import net.cl.shop.service.UserService;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(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 userService = new UserService();
// 调用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 doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
启动服务器,访问http://localhost:8080/simonshop/register?username=萌萌哒&password=55555&telephone=15896961234,敲回车,查看结果:
在去服务器端控制台查看信息:
我们去NaviCat查看用户表,看看是否插入了新的用户记录?
可以看到telephone字段长度为11,那么我们重启服务器再测试,让telephone的值超过11位,结果又会如何呢?
可以很明显的看到,报错了,注册失败,再一次跳转到了注册页面,输入的telephone的值是14位,超过了t_user表的telephone的值,所 以就无法插入新的数据了。
那我们去服务器端控制台查看信息:
确实在控制台输出了“遗憾,注册失败,跳转到注册页面!”信息,但是还抛出了一个异常:com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column ‘telephone’ at row 1,显示方式不是我们喜欢的,问题就出在模型层,可以去修改UserDaoImpl里的insert方法。
将catch字句里的e.printStackTrace();改成System.err.println(“SQL异常:” + e.getMessage());:
重启服务器,再次测试:
再一次去看一看服务器端控制台的提示信息:
4、显示类别处理类ShowCategoryServlet
package net.cl.shop.servlet;
import java.io.IOException;
import java.util.List;
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 net.cl.shop.bean.Category;
import net.cl.shop.service.CategoryService;
@WebServlet("/showCategory")
public class ShowCategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建类别服务对象
CategoryService categoryService = new CategoryService();
// 获取全部商品类别
List categories = categoryService.findAllCategories();
// 获取session对象
HttpSession session = request.getSession();
// 把商品类别列表以属性的方式保存到session里
session.setAttribute("categories", categories);
// 重定向到显示商品类别页面(showCategory.jsp)
response.sendRedirect(request.getContextPath() + "/frontend/showCategory.jsp");
// 在服务器控制台输出测试信息
for (Category category: categories) {
System.out.println(category);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
下面我们来进行测试。
重启服务器,访问http://localhost:8080/simonshop/showCategory:
我们到服务器端控制台查看输出的信息:
5、显示商品处理类ShowProductServlet
package net.cl.shop.servlet;
import java.io.IOException;
import java.util.List;
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 net.cl.shop.bean.Product;
import net.cl.shop.service.CategoryService;
import net.cl.shop.service.ProductService;
@WebServlet("/showProduct")
public class ShowProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(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 products = productService.findProductsByCategoryId(categoryId);
// 获取session对象
HttpSession session = request.getSession();
// 把商品列表对象以属性的方式保存到session里
session.setAttribute("products", products);
// 重定向到显示商品信息页面
response.sendRedirect(request.getContextPath() + "/frontend/showProduct.jsp?categoryName=" + categoryName);
// 在服务器端控制台输出测试信息
for (int i = 0; i < products.size(); i++) {
System.out.println(products.get(i));
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
下面我们来进行测试。
重启服务器,访问http://localhost:8080/simonshop/showProduct?categoryId=1:
回去服务器端控制台查看输出信息: