login.jsp
<%@ page language="java" import="java.util.*,bean.SessionUser" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% // //获取IP String ip=request.getRemoteAddr(); SessionUser sessionUser=new SessionUser(); sessionUser.setIp(ip); System.out.println("JSP页面SessionUser= "+sessionUser); session.setAttribute("userinfo", sessionUser); %> <form action="${pageContext.request.contextPath}/LoginServlet" method="post"> 员工信息录入<br/> 用户名:<input type="text" value="" name="username" /> <br/> 密码:<input type="password" name="psw"/> <br/> <input type="submit" value="保存" onclick="save()"/> </form> </body> </html>2. onlinenum.jsp
<%@ page language="java" import="java.util.*,bean.SessionUser" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <br/> 显示游客信息:<br/> <c:forEach items="${applicationScope.onlinemap}" var="sessionUser"> <c:if test="${sessionUser.value.username==null}"> ${sessionUser.value.ip}~~~~${sessionUser.value.username}~~~~ </c:if> </c:forEach> <br/> 显示注册用户信息:<br/> <c:forEach items="${applicationScope.onlinemap}" var="sessionUser"> <c:if test="${sessionUser.value.username!=null}"> ${sessionUser.value.ip}~~~~${sessionUser.value.username}~~~~ <a href="${pageContext.request.contextPath}/RemoveServlet?sessionid=${sessionUser.key}">转为游客</a> </c:if> </c:forEach>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <listener> <listener-class>listener.MyServletContextListener</listener-class> </listener> <listener> <listener-class>listener.MyHttpSessionListener</listener-class> </listener> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>web.LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>RemoveServlet</servlet-name> <servlet-class>web.RemoveServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>RemoveServlet</servlet-name> <url-pattern>/RemoveServlet</url-pattern> </servlet-mapping> </web-app>
bean:
1 User
package bean; public class User { private String username; private String psw; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } }
2.SessionUser:
package bean; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class SessionUser extends User implements HttpSessionBindingListener { private String ip;//登陆用户和游客的IP public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public void valueBound(HttpSessionBindingEvent event) { System.out.println("valueBound~~~~~~~~~~~"); System.out.println("this= "+this); HttpSession session=event.getSession(); String sessionid=session.getId(); System.out.println("sessionid= "+sessionid); ServletContext sc=session.getServletContext(); Map<String, SessionUser> onlinemap=(Map<String, SessionUser>)sc.getAttribute("onlinemap"); onlinemap.put(sessionid, this); } public void valueUnbound(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } }
1.MyServletContextListener:
package listener; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import bean.SessionUser; public class MyServletContextListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent sce) { } public void contextInitialized(ServletContextEvent sce) { Map<String,SessionUser> map=new HashMap<String,SessionUser>();//获取servletContext ServletContext sc=sce.getServletContext(); if (sc!=null) { sc.setAttribute("onlinemap", map); }}}
2. MyHttpSessionListener:package listener; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import bean.SessionUser; public class MyHttpSessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent arg0) { // TODO Auto-generated method stub } public void sessionDestroyed(HttpSessionEvent se) { // 当session过期后或销亡才调用 HttpSession session=se.getSession(); String sessionid=session.getId(); ServletContext sc=session.getServletContext(); Map<String, SessionUser> onlinemap=(Map)sc.getAttribute("onlinemap"); onlinemap.remove(sessionid); session.removeAttribute("userinfo"); } }web1.LoginServlet:
package web; import java.io.IOException; import java.io.PrintWriter; import javax.jms.Session; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import bean.User; public class LoginServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); HttpSession session=request.getSession(false); String path=""; String username=request.getParameter("username"); if (session!=null) { User user=(User)session.getAttribute("userinfo"); user.setUsername(username); path="/title.jsp"; } request.getRequestDispatcher(path).forward(request, response); } }
2. RemoveServletpackage web; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.SessionUser; public class RemoveServlet 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 { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String sessionid=request.getParameter("sessionid"); ServletContext sc=this.getServletContext(); Map<String, SessionUser> onlinemap=(Map)sc.getAttribute("onlinemap"); SessionUser sessionUser=onlinemap.get(sessionid); sessionUser.setUsername(null); } }