|
@@ -0,0 +1,83 @@
|
|
|
+package com.lovecoding.study.servlet;
|
|
|
+
|
|
|
+import com.lovecoding.study.domian.User;
|
|
|
+import com.lovecoding.study.mapper.UserMapper;
|
|
|
+import com.lovecoding.study.utils.MybatisUtils;
|
|
|
+import org.apache.ibatis.session.SqlSession;
|
|
|
+import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
+
|
|
|
+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("/user")
|
|
|
+public class UserServlet extends HttpServlet{
|
|
|
+
|
|
|
+ //登陆操作
|
|
|
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
+ String username = req.getParameter("username");
|
|
|
+ String password = req.getParameter("password");
|
|
|
+ SqlSession sqlSession = MybatisUtils.getSession().openSession();
|
|
|
+ if ( username != null && password != null ) {
|
|
|
+ UserMapper mapper = sqlSession.getMapper(UserMapper.class);
|
|
|
+ User user = mapper.findUser(username);
|
|
|
+ if ( user != null ) {
|
|
|
+ if ( user.getPassword().equals(password) ) {
|
|
|
+ //登陆成功
|
|
|
+ HttpSession session = req.getSession(true);
|
|
|
+ session.setAttribute("user", user);
|
|
|
+ session.removeAttribute("error");
|
|
|
+ resp.sendRedirect(req.getContextPath() + "/brand.jsp");
|
|
|
+ } else {
|
|
|
+ //密码不正确
|
|
|
+ req.getSession(true).setAttribute("error", "密码不正确!");
|
|
|
+ resp.sendRedirect(req.getContextPath() + "/login.jsp");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //没查到用户信息
|
|
|
+ req.getSession(true).setAttribute("error", "用户名不正确");
|
|
|
+ resp.sendRedirect( req.getContextPath() + "/login.jsp");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //表单没有数据
|
|
|
+ req.getSession(true).setAttribute("error", "请处输入用户名和密码!");
|
|
|
+ resp.sendRedirect(req.getContextPath() + "/login.jsp");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //注册操作
|
|
|
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
+ req.setCharacterEncoding("UTF-8");
|
|
|
+ String username = req.getParameter("username");
|
|
|
+ String password = req.getParameter("password");
|
|
|
+
|
|
|
+ if ( username != null && !username.equals("")
|
|
|
+ && password != null && !password.equals("") ) {
|
|
|
+ SqlSession sqlSession = MybatisUtils.getSession().openSession();
|
|
|
+ UserMapper mapper = sqlSession.getMapper(UserMapper.class);
|
|
|
+ User user = mapper.findUser(username);
|
|
|
+ if ( user == null ) {
|
|
|
+ //添加用户
|
|
|
+ mapper.addUser( username, password );
|
|
|
+ sqlSession.commit(true);
|
|
|
+ req.getSession(true).removeAttribute("usernameerror");
|
|
|
+ req.getSession(true).removeAttribute("passworderror");
|
|
|
+ resp.sendRedirect(req.getContextPath()+ "/login.jsp");
|
|
|
+ } else {
|
|
|
+ //用户名已存在
|
|
|
+ req.getSession(true).setAttribute("usernameerror", "用户名已存在");
|
|
|
+ resp.sendRedirect(req.getContextPath()+ "/register.jsp");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //用户没天写表单
|
|
|
+ req.getSession(true).setAttribute("passworderror", "表单项不能为空");
|
|
|
+ resp.sendRedirect(req.getContextPath()+ "/register.jsp");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|