JavaWeb应用案例(Mysql+Javaweb)购物网-02

02
1.编写控制层(servlet)
1.1.登录处理类

import net.bl.xmshop.bean.User;
import net.bl.xmshop.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 {
    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.2.修改主页代码index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>
  </head>
  <body>
  <h3 style="text-align: center">欢迎来到西蒙购物网</h3>
  </body>
</html>

测试index.jsp
在这里插入图片描述
在地址栏输入:http://localhost:8080/web_war_exploded/login?username=admin&password=root


页面404错误,因为对应的JSP没有编写
查看控制台
在这里插入图片描述
登陆成功
尝试普通用户登录管理后台
在这里插入图片描述
1.2.注销处理类 LogoutServlet

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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值