在线踢人的操作
分析:
用户:登陆页面login.jsp——>点提交时,提交给一个LoginServlet.java中,(封装在User对象中——>session 。。。在这设置一个监听器,监听session的属性添加的事件,在事件指定的方法中,将user加入到一个集合对象里面去){集合是Map类型的,map(key,value),key=username,value=session},跳转到首页上index.jsp(首页上显示欢迎:xxx)
管理员:可以看到所有的用户列表(listUser.jsp,将所有的用户列表显示出来 )每一个用户名后面有一个超链接<a>踢人</a>----à要把这个请求交给一个KickUserServlet.java完成踢人的操作。
用户列表:
源代码:
1、首先创建com.hbsi.domain包:在包下创建实体类User.java
将username、password两个属性封装在里面。,并且实现set和get方法。
package com.hbsi.domain;
public class User {
private String username;
private String 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;
}
}
2、创建登录界面login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>在线登录界面 </title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="${pageContext.request.contextPath}/servlet/LoginServlet" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
3、点击提交,提交给一个LoginServlet.java。
在com.hbsi.web.servlet中创建LoginServlet.java
package com.hbsi.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hbsi.domain.User;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
User user=new User();
user.setUsername(username);
user.setPassword(password);
request.getSession().setAttribute("user",user);
//实现跳转
//使用请求的重定向
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}
4、在这设置一个监听器,监听session的属性添加的事件,在事件指定的方法中,将user加入到一个集合对象里面去。
package com.hbsi.web.listener;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import com.hbsi.domain.User;
public class SessionAttributeListener implements HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent arg0) {
//在session对象里添加了一个属性——得到属性
Object obj=arg0.getValue();
if(obj instanceof User){
//将该用户的用户名以及这个用户所对应的session对象加入到一个Map类型的集合中去
HttpSession session=arg0.getSession();
//得到servletContext对象
Map map=(Map)session.getServletContext().getAttribute("map");
if(map==null){
//如果为空,就创建一个HaspMap
map=new HashMap();
session.getServletContext().setAttribute("map",map);
}
//在map中添加一个元素
User user=(User) obj;//强制造型
map.put(user.getUsername(),session);
}
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
}
5、编写listUser.jsp界面,显示所有用户列表
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'listUser.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
用户列表:<br/>
<c:forEach items="${map}" var="me">
${me.key} <a href="${pageContext.request.contextPath}/servlet/KickServlet?username=${me.key}">踢人</a>
</c:forEach>
</body>
</html>
6、在KickServlet.java中完成踢人的操作
package com.hbsi.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class KickServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
Map map=(Map) this.getServletContext().getAttribute("map");
HttpSession session=(HttpSession) map.get(username);
if(session!=null){
session.invalidate();//失效
map.remove(username);
}
request.getRequestDispatcher("/listUser.jsp").forward(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
7、在index.jsp页面中得到在线的用户。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h3>欢迎:${user.username}</h3>
</body>
</html>