package com.servlet;
import com.valueBean.UserBean;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet{
private List<UserBean> userList = new ArrayList<>();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
public void init() throws ServletException {
super.init();
userList.add(new UserBean("user1","1111",1));
userList.add(new UserBean("user2","2222",2));
userList.add(new UserBean("user3","3333",3));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
HttpSession session = req.getSession();
if(username == null || password == null || username.equals("") || password.equals("")){
resp.sendRedirect("login.jsp");
return;
}
for (UserBean userBean : userList) {
if (userBean.getUsername().equals(username) && userBean.getPassword().equals(password)) {
session.setAttribute("userBaen",userBean);
resp.sendRedirect("show.jsp");
}
}
}
}