在線踢人實例,狂玩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);
}
}
簡單的一個踢人實例就這樣完成了,在完成這個項目前,最好先畫出執行過程圖,然後根據圖形來完成各個功能。