用户在线踢人

来源:互联网 发布:淘宝一加3邀请码靠谱吗 编辑:程序博客网 时间:2024/04/28 22:47

index.jsp:

<c:if test="${sessionScopse.user == null}">
  <a href="${app}/loggin.jsp">用户登录</a>
  </c:if>
    <c:if test="${sessionScopse.user != null}">
    欢迎${requestScopse.user.username}  <a href="${app}/servlet/Loggout">注销</a>
    </c:if>


loggin.jsp

<h1>用户登录</h1><hr>
<form action="${app}/servlet/Login" method="post">
用户名:<input type="text" name ="username">
密码:<input type="password" name="password">
<input type="submit" value="登录">
</form>


userList.jsp

<c:if test="${user == null}">
    没有访问权限
    </c:if>
    <c:if test="${user != null}">
    <c:if test="${user.role =='user' }">
    <c:forEach items="${map}" var="entry">
    ${entry.key}<br>
    </c:forEach>
    </c:if>
    <c:if test="${user.role =='admin' }">
    <c:forEach items="${map}" var="entry">
    ${entry.key}<a href="${app}/servlet/Kict?id=${entry.key}">踢人</a>
    </c:forEach>
    </c:if>
    </c:if>


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<String,HttpSession> map =(Map<String, HttpSession>) event.getSession().getServletContext().getAttribute("map");
map.put(username, event.getSession());
}
public void valueUnbound(HttpSessionBindingEvent event) {
Map<String,HttpSession> map =(Map<String, HttpSession>) 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<String, HttpSession>());
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>(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<String,HttpSession> map = (Map<String, HttpSession>) 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);


}


}