首页 > 代码库 > Servlet 监听器实例
Servlet 监听器实例
1、简单介绍一下监听器
所谓监听器就是监听事件,然后针对相应的事件进行处理
监听器举例:ServletcontextListener、HttpSessionListener、HttpSessionAttributeListener
2、设计一个统计在线人数的实例
import java.util.LinkedList; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class OnlineListener implements HttpSessionAttributeListener, ServletContextListener, HttpSessionListener { private ServletContext application=null; @Override public void sessionCreated(HttpSessionEvent arg0) { // TODO Auto-generated method stub } @Override public void sessionDestroyed(HttpSessionEvent arg0) { // TODO Auto-generated method stub HttpSession hs=arg0.getSession(); String user=(String) hs.getAttribute("username"); LinkedList<String> online = (LinkedList<String>) application.getAttribute("online"); online.remove(user); application.setAttribute("online", online); } @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub application=arg0.getServletContext(); application.setAttribute("online", new LinkedList<String>()); } @Override public void attributeAdded(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub HttpSession hs=arg0.getSession(); LinkedList<String> online = (LinkedList<String>) application.getAttribute("online"); String name=arg0.getName(); if(name.equals("username")){ online.add((String)arg0.getValue()); } application.setAttribute("online", online); } @Override public void attributeRemoved(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } @Override public void attributeReplaced(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } }
import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.doGet(req, resp); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.doPost(req, resp); PrintWriter out=resp.getWriter(); String user=(String) req.getParameter("username"); if(user!=null && !user.equals("")){ req.getSession().setAttribute("username", user); } LinkedList<String> online= (LinkedList<String>) getServletContext().getAttribute("online"); resp.setContentType("text/html;charset=utf-8"); out.println("<html>"); out.println("<head><title>Login</title></head>"); out.println("<body>"); out.println("<b><font size=\"+4\">"); out.println("<font color=\"#FF0000\">The curent user's list is:<br/>"); for(int i=0;i<online.size();i++){ out.println(i+1+"."+online.get(i)+"<br/>"); } out.println("<hr/>"); out.println("<a href=http://www.mamicode.com/""+resp.encodeURL("logout")+"/">Logout");>import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LogoutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.doGet(req, resp); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.doPost(req, resp); req.getSession().invalidate(); LinkedList<String> online=(LinkedList<String>) getServletContext().getAttribute("online"); resp.setContentType("text/html;charset=utf-8"); PrintWriter out=resp.getWriter(); out.println("<html>"); out.println("<head><title>Log out</title></head>"); out.println("<body>"); out.println("<b><font size=\"+4\">"); out.println("<font color=\"#FF0000\">The curent user's list is:<br/>"); for(int i=0;i<online.size();i++){ out.println(1+i+"."+online.get(i)+"<br/>"); } out.println("<hr/><a href=http://www.mamicode.com/"index.html/">home page");><listener> <listener-class>OnlineListener</listener-class> </listener> <servlet> <servlet-name>login</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet> <servlet-name>logout</servlet-name> <servlet-class>LogoutServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>logout</servlet-name> <url-pattern>/logout</url-pattern> </servlet-mapping><html> <head><title>index</title></head> <body> <form action="login" method="post"> username:<input type="text" name="username"/> <input type="submit" value=http://www.mamicode.com/"login"/>
>
Servlet 监听器实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。