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