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=\""+resp.encodeURL("logout")+"\">Logout</a>");
		out.println("</b></font>");
		
		out.println("</body>");
		out.println("</html>");
		out.flush();
		out.close();
	}

}
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=\"index.html\">home page</a>");
		out.println("</font></body>");
		out.println("</html>");
	}

}
<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="login"/><br/><br/>
	</form>
</body>
</html>
Servlet 监听器实例_第1张图片

Servlet 监听器实例_第2张图片

Servlet 监听器实例_第3张图片

你可能感兴趣的:(Servlet监听器实例)