web项目踢出用户登陆操作

@WebServlet("/TickOurSvl")
public class TickOurSvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TickOurSvl() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String uno=request.getParameter("uno");
		String sessionid=LoginSvl.loginedUsers.get(uno);
		if(sessionid!=null) {
			LogTask.logInfo(uno+"没有对应的会话id,请登录");
		}else {
		HttpSession session=	OnlineUserListener.users.get(sessionid);
			if(session!=null) {
				LogTask.logInfo(sessionid+"没有对应的会话");
			}else {
				session.invalidate();
				LogTask.logInfo(uno + "已被踢下线...");
			}
		}
		
	}
}

@WebServlet("/LoginSvl")
public class LoginSvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static Map<String,String> loginedUsers =  new MyHashMap<String, String>();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginSvl() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String uno=request.getParameter("uno");
		String password=request.getParameter("password");
		UserBiz biz=new UserBiz();
		try {
			User user=biz.isExist(uno, password);
			if(user==null) {
				request.setAttribute("msg","用户或密码错误");
				request.getRequestDispatcher("/main/login.jsp").forward(request, response);
			}else {
				//用户登录成功,写入cookie
				String ckValue=user.getUno()+":"+user.getPassword();
				Cookie ck=new Cookie("user", ckValue);
				ck.setMaxAge(7*24*3600);
	    		response.addCookie(ck);
				loginedUsers.put(user.getUno(), request.getSession().getId());
				request.getSession().setAttribute("user", user);
				request.getRequestDispatcher("/MainSvl").forward(request, response);
				}
			}catch (UniqueNotException e) {
			// TODO: handle exception
			request.setAttribute("msg", "该用户名已在其他地方登录");
			request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.logger.error(e.getMessage(),e);
			throw new ServletException(e.getMessage());
		}
	}

}

public class MyHashMap<K,V> extends HashMap<K,V> {
	
		@Override
		public V put(K key, V value) {
			// TODO Auto-generated method stub
			V v=this.get(key);
			if(v!=null) {
				throw new UniqueNotException("用户名已经存在");
			}
			return super.put(key, value);
		}
}
@WebListener
public class OnlineUserListener implements HttpSessionListener {
	public static Map<String, HttpSession> users=new ConcurrentHashMap<>();   //所有在线用户
    /**
     * Default constructor. 
     */
    public OnlineUserListener() {
        LogTask.logInfo("OnlineUserListener构造");
    }

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		String sessionid=se.getSession().getId();
		users.put(sessionid, se.getSession());
		se.getSession().setMaxInactiveInterval(15*60);
		LogTask.logInfo(sessionid+"上线");
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		User user=(User) se.getSession().getAttribute("user");
		LoginSvl.loginedUsers.remove(user.getUno());
		LogTask.logInfo(user.getUno() + "被移除....");
		
		String sessionid=se.getSession().getId();
		users.remove(sessionid);
		LogTask.logInfo(sessionid+"下线....");
	}

 
	
}

实现踢人操作
/利用map唯一性的特性,进行重写封装。
由于已经做过自动登陆操作,所以需要在踢出前清楚掉用户的session。
利用用户名得出sessionid,再通过sessionid得出session进行删除。

public static Map<String,String> loginedUsers =  new MyHashMap<String, String>();  
	loginedUsers.put(user.getUno(), request.getSession().getId());
public static Map<String, HttpSession> users=new ConcurrentHashMap<>();   所有在线用户
users.put(sessionid, se.getSession());

以上两段代码只是项目中关于存储用户名 sessionid 和session的有关代码

至于使用 ConcurrentHashMap<>();的原因是由于他是线程安全,而且采用了分段锁,即安全又快捷的原因

匿名

发表评论

匿名网友