在线踢人实例,狂玩jsp和servlet监听器
项目需求:
1.能看到在线人员
2.看不顺眼的人可以把他踢了
3.踢了后返回到踢人界面
开发流程图如下:
开发过程:
1.做一个登录界面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>My JSP 'login.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td bgcolor="#000099">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td bgcolor="#FFFFFF"> <b>*</b> </td>
<td width="100%"><font color="#CCCCCC"> <font color="#FFFFFF">Title</font></font></td>
</tr>
</table></td>
</tr>
<tr>
<td width="100%" bgcolor="#EAEAEA" colspan="2">
<form action="${pageContext.request.contextPath }/servlet/LoginServlet" method="post"><p>
<label for="textfield">用户名</label>
<br>
<input type="text" name="username" id="user">
</p>
<p>
<label for="textfield2">密码</label>
<br>
<input type="text" name="password" id="password">
</p>
<p>
<input type="submit" name="Submit" value="GO">
</p>
<p> </p>
</form>
</td>
</tr>
</table>
</body>
</html>
2.编写登录控制器LoginServlet,代码:
<%@ 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 'login.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td bgcolor="#000099">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td bgcolor="#FFFFFF"> <b>*</b> </td>
<td width="100%"><font color="#CCCCCC"> <font color="#FFFFFF">Title</font></font></td>
</tr>
</table></td>
</tr>
<tr>
<td width="100%" bgcolor="#EAEAEA" colspan="2">
<form action="${pageContext.request.contextPath }/servlet/LoginServlet" method="post"><p>
<label for="textfield">用户名</label>
<br>
<input type="text" name="username" id="user">
</p>
<p>
<label for="textfield2">密码</label>
<br>
<input type="text" name="password" id="password">
</p>
<p>
<input type="submit" name="Submit" value="GO">
</p>
<p> </p>
</form>
</td>
</tr>
</table>
</body>
</html>
3.编写实现了HttpServletSessionAttributeListener的类MyHttpServletSessionAttributeListener,代码:
package com.itheima.servlet;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import com.itheima.domain.User;
public class MyHttpSessionAttributeListener implements
HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent arg0) {
HttpSession session=arg0.getSession();
Object obj=session.getAttribute("user");
if(obj==null){
return ;
}
if(obj instanceof User){
User u=(User)obj;
//得到ServletContext
ServletContext servletContext=session.getServletContext();
//因为需要将当前登录的多个用户信息存放ServletContext,所以先产生一个Map集合,
//将所有哦用户信息存放到Map集合中,最后将Map集合放入到ServletContext
//Map集合的key值:用户名 value:HttpSession
Map<String,HttpSession>map=(Map<String,HttpSession>)servletContext.getAttribute("map");
if(map==null){
//保证线程同步
map=Collections.synchronizedMap(new HashMap<String,HttpSession>());
servletContext.setAttribute("map", map);
}
map.put(u.getUsername(), session);
}
}
@Override
public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
}
4.做一个主页面index.jsp,代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" 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 '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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<c:if test="${sessionScope.user!=null }">
<c:forEach items="${applicationScope.map }" var="map">
<!-- 为了处理用户的中文名 -->
<c:url value="/servlet/KickServlet" var="myurl">
<c:param name="username" value="${map.key }"></c:param>
</c:url>
${map.key }<a href="${myurl }">踢人</a>
</c:forEach>
</c:if>
<c:if test="${sessionScope.user==null }">
对不起,您可能已经被踢了
</c:if>
</body>
</html>
5.做一个踢人的控制器KickServlet,代码:
package com.itheima.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 {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String username1=request.getParameter("username");
String username=new String(username1.getBytes("iso-8859-1"),"utf-8");
//获取Map集合
Map<String,HttpSession>map=(Map<String, HttpSession>) getServletContext().getAttribute("map");
//找到session
HttpSession session=map.get(username);
//移除session
session.invalidate();
map.remove(username);
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
简单的一个踢人实例就这样完成了,在完成这个项目前,最好先画出执行过程图,然后根据图形来完成各个功能。