登陆页面
<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <body> <form action="${pageContext.request.contextPath}/LoginServlet" method="post"> 用户名:<input type="text" name="username"/> <input type="submit" value="登录"/> </form> </body></html>
显示页面
<%@ page language="java" pageEncoding="UTF-8"%><%@ page import="cn.vote.domain.*" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <body> 欢迎${!empty username?username:'游客'}光临 </body></html>
管理员页面
<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <body> <table border="1" align="center"> <caption>后台操作</caption> <tr> <th>用户名</th> <th>操作</th> </tr> <c:forEach var="entry" items="${map}"> <tr> <td>${entry.key}</td> <td> <c:url var="myURL" value="/KickServlet"> <c:param name="username" value="${entry.key}"/> </c:url> <a href="${myURL}" style="text-decoration:none"> 踢人 </a> </td> </tr> </c:forEach> </table> </body></html>
java代码
监听代码
package cn.xijie.listener;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;//监听器public class KickListener implements HttpSessionAttributeListener { public void attributeAdded(HttpSessionBindingEvent se) { String name = se.getName();//name="username" String value = (String) se.getValue();//value="jack" if(name.equals("username")){ //取得该用户对应的HttpSession HttpSession session = se.getSession(); //取得该web应用的SerlvetContext对象 ServletContext context = session.getServletContext(); Map<String,HttpSession> map = (Map<String, HttpSession>) context.getAttribute("map"); if(map==null){ map = new HashMap<String,HttpSession>(); } map.put(value,session); context.setAttribute("map",map); } } public void attributeRemoved(HttpSessionBindingEvent se) { } public void attributeReplaced(HttpSessionBindingEvent se) { }}
登陆代码
package cn.xijie.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 javax.servlet.http.HttpSession;public class LoginServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); if(username != null && username.trim().length() > 0) { HttpSession session = request.getSession(); //将用户名绑定到HttpSession中,事件触发 session.setAttribute("username",username); response.sendRedirect(request.getContextPath()+"/welcome.jsp"); } }}
控制代码
package cn.xijie.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 java.util.Map;import javax.servlet.http.HttpSession;public class KickServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String ip = request.getRemoteAddr(); if ("127.0.0.1".equals(ip)) { // 解码 String username = request.getParameter("username"); byte[] buf = username.getBytes("ISO8859-1"); username = new String(buf, "UTF-8"); // 从ServletContext中取得Map对象 Map<String, HttpSession> map = (Map<String, HttpSession>) this.getServletContext().getAttribute("map"); // 根据用户名查找对应的HttpSession HttpSession session = map.get(username); // 将该用户名从Map中移除 map.remove(username); // 强行销毁该HttpSession session.invalidate(); // 重写向到manager.jsp response.sendRedirect(request.getContextPath() + "/manager.jsp"); } }}
wenb.xml
<servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>cn.xijie.servlet.LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>KickServlet</servlet-name> <servlet-class>cn.xijie.servlet.KickServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>KickServlet</servlet-name> <url-pattern>/KickServlet</url-pattern> </servlet-mapping>
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦