A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

风过了无痕

初级黑马

  • 黑马币:

  • 帖子:

  • 精华:

© 风过了无痕 初级黑马   /  2019-6-21 13:57  /  666 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.itcast.heimaPro.web.Servlet;

import cn.itcast.heimaPro.domain.Manager;
import cn.itcast.heimaPro.domain.User;
import cn.itcast.heimaPro.service.ManagerService;
import cn.itcast.heimaPro.service.ManagerServiceImpl;

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("/managerLoginServlet")
public class ManagerLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String code = request.getParameter("verifycode");//输入的验证码
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");//生成的验证码
        session.removeAttribute("CHECKCODE_SERVER");//保证验证码的一次性,不会重复
        if (!code.equalsIgnoreCase(checkcode_server)) {
            request.setAttribute("login_msg", "验证码错误");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        }
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Manager manager = new Manager();
        manager.setUsername(username);
        manager.setPassword(password);
        session.setAttribute("manager", manager);
        //5.调用Service查询
        ManagerService service = new ManagerServiceImpl();
        Manager loginManager = service.login(manager);
        if (loginManager == null) {
            request.setAttribute("login_msg", "用户名或密码错误");
            request.getRequestDispatcher(request.getContextPath() + "/login.jsp").forward(request, response);
        } else if (loginManager != null) {
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }
        System.out.println(username);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马