对用户自动登录实现的代码

login.jsp:


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



 
   
    
    My JSP 'index.jsp' starting page


    



 
  
 
 
   

    用户名:
  

    密  码:
 

    有效期:1小时
    10分钟
    1分钟

   
   

 
 




User.java:


package com.hbsi.csdn.domain;

public class User {
private String username;
private String password;

public User(String username,String password){
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}



}



UserDao.java:


package com.hbsi.csdn.Dao;

import java.util.Arrays;
import java.util.List;

import com.hbsi.csdn.domain.User;

public class UserDao {
List db = Arrays.asList(new User("aa", "11"), new User("bb", "22"),
new User("cc", "33"));//设置几个用户名和密码
public List getAll(){//查找所有用户
return db;
}
public User find(String username){  //根据用户名查找用户
for(User user:db){
if(user.getUsername().equals(username)){
return user;
}
}
return null;
}
public User find(String username,String password){  //核实用户名和密码
for(User user:db){
if(user.getUsername().equals(username)&&user.getPassword().equals(password)){
return user;
}
}
return null;
}
}


LoginServlet.java:

package com.hbsi.csdn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Encoder;

import com.hbsi.csdn.Dao.UserDao;
import com.hbsi.csdn.domain.User;

public class LoginServlet extends HttpServlet {

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

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String username = request.getParameter("username");//获取用户名
String password = request.getParameter("password");
String autoLoginTime1 = request.getParameter("autoLoginTime");

if (autoLoginTime1 != null) {//判断时间,不为空时将时间传到cookie
int autoLoginTime = Integer.parseInt(autoLoginTime1);
UserDao dao = new UserDao();
User user = dao.find(username, password);//在dao中找到用户名和密码
if (user != null) {
request.getSession().setAttribute("user", user);
sendCookie(response, user, autoLoginTime);
}

}

}

private void sendCookie(HttpServletResponse response, User user,
int autoLoginTime) {
long time = System.currentTimeMillis() + autoLoginTime;
String md5value = md5(user.getUsername() + ":" + time + ":"
+ user.getPassword());
String cookievalue=user.getUsername()+":"+time+":"+md5value;
Cookie cookie=new Cookie("autoLogin",cookievalue);//new一个cookie对象
cookie.setMaxAge(autoLoginTime);  //设置cookie的有效时间
cookie.setPath("/FilterAutoLogin");  //设置cookie的有效范围
response.addCookie(cookie);  //发送cookie 
}

private String md5(String input) {//设置md5加密
try {
MessageDigest md = MessageDigest.getInstance("md5");
byte result[] = md.digest(input.getBytes());
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(result);

} catch (Exception e) {
throw new RuntimeException(e);
}

}

}

你可能感兴趣的:(javaWeb编程)