1. 程式人生 > >淺談利用Cookie技術實現3天免登陸

淺談利用Cookie技術實現3天免登陸

private void dologin(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username==null || password==null){
Cookie[] cookie = request.getCookies();
for (Cookie c : cookie) {
if(c.getName().equals("username")) {
username = c.getValue();
}else if(c.getName().equals("password")){
password = c.getValue();
}
}
}

if(username==null || password==null) {
request.getRequestDispatcher("login.jsp").forward(request, response);
} else {
for (User user : list) {
if(user.getUsername().equals(username) && user.getPassword().equals(password)) {
Cookie cookie1 = new Cookie("username", username);
Cookie cookie2 = new Cookie("password", password);
cookie1.setMaxAge(15);
cookie2.setMaxAge(15);
response.addCookie(cookie1);
response.addCookie(cookie2);
request.getRequestDispatcher("success.jsp").forward(request, response);
} else{
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}

}