用户在线踢人

index.jsp:


  用户登录
 

   
    欢迎${requestScopse.user.username}  注销
   


loggin.jsp

用户登录




用户名:
密码:



userList.jsp


    没有访问权限
   

   
   
   
    ${entry.key}

   

   

   
   
    ${entry.key}踢人
   

   

   


user:

public class Users implements Serializable,HttpSessionBindingListener {
private int id;
private String username;
private String password;
private String nickName;
private String role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public void valueBound(HttpSessionBindingEvent event) {
Map map =(Map) event.getSession().getServletContext().getAttribute("map");
map.put(username, event.getSession());
}
public void valueUnbound(HttpSessionBindingEvent event) {
Map map =(Map) event.getSession().getServletContext().getAttribute("map");
map.remove(username);
}

}


CreateMap :

public class CreateMap implements ServletContextListener {


public void contextDestroyed(ServletContextEvent arg0) {

}


public void contextInitialized(ServletContextEvent event) {
event.getServletContext().setAttribute("map", new HashMap());
event.getServletContext().setAttribute("app",event.getServletContext().getContextPath());
}


}

Login :

public class Login extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("html/text;charset=utf-8");
//1.获取参数
String username = request.getParameter("username");
String password = request.getParameter("password");
//到数据库去查
Users users = null;
QueryRunner run = new QueryRunner(DaoUtils.getSource());
try {
users = run.query("select * from users where username=? and password=?", new BeanHandler(Users.class),username,password);
HttpSession session = request.getSession();
if(users==null){
response.getWriter().write("用户不存在");
response.sendRedirect(request.getContextPath()+"/loggin.jsp");
}else{
session.setAttribute("user", users);
response.sendRedirect(request.getContextPath()+"/userList.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}
}


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


}


}


Kict:

public class Kict extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("id");
//2.获取usermap,查找此名字对应的记录,找找到此人的session杀死
HttpSession session = request.getSession();
Map map = (Map) this.getServletContext().getAttribute("map");
if(map.containsKey(username)){
map.get(username).invalidate();
}
response.sendRedirect(request.getContextPath()+"/userList.jsp");
}


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


}


}

你可能感兴趣的:(踢人)