用户在线踢人

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);


}


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值