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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘邦m 初级黑马   /  2019-4-25 13:46  /  374 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.lbm.login.web;

import cn.lbm.login.dao.userDao;
import cn.lbm.login.domain.User;
import org.apache.commons.beanutils.BeanUtils;


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.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        /*String username = req.getParameter("username");
        String password = req.getParameter("password");
        User LoginUser = new User();
        LoginUser.setUsername(username);
        LoginUser.setPassword(password);*/
        User loginuser = new User();
        Map<String, String[]> loginMap = req.getParameterMap();

        try {
            BeanUtils.populate(loginuser, loginMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        userDao userDao = new userDao();
        User user = userDao.login(loginuser);

        if(user != null){
            req.setAttribute("user", user);
            req.getRequestDispatcher("/success").forward(req, resp);
        }else{
            req.getRequestDispatcher("/fail").forward(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}

0 个回复

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