过滤器和监听器

一、概述html

二、拦截器java

三、过滤器的生命周期web

四、初始化参数和过滤器链session

五、过滤器的使用场合jsp

 六、监听器post

web应用程序事件模型的一部分,当web应用中的某些状态发生改变时,会产生相应的事件,监听器能够接收这些事件ui

而且在事件发生时作一些相关的处理。this

样例:spa

package com.ljb.constants;
public class Constants {
 public static int ONLINE_USER_COUNT = 0;// 在线用户数
}
package com.ljb.listener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import com.ljb.constants.Constants;
/**
 * Application Lifecycle Listener implementation class User
 *
 */
@WebListener
public class User implements HttpSessionBindingListener {
 private String username;
 
 
    public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 /**
     * Default constructor. 
     */
    public User() {
        // TODO Auto-generated constructor stub
    }
 /**
     * 从session中删除时调用
     * @see HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
     */
    public void valueUnbound(HttpSessionBindingEvent arg0) {
        Constants.ONLINE_USER_COUNT--;
    }
 /**
     * User对象存入session时自动调用
     * @see HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
     */
    public void valueBound(HttpSessionBindingEvent arg0) {
       Constants.ONLINE_USER_COUNT++;
    }
 
}

enter.jspcode

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="doEnter.jsp" method="post">
  用户名:<input type="text" name="username"/>
  <input type="submit" value="进入"/>
 </form>
</body>
</html>

doEnter.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.ljb.listener.User" %>
 <%
  String name = request.getParameter("username");
  System.out.println(name);
  if (name == null || name.equals("")) {
   System.out.println("----------------1");
   response.sendRedirect("enter.jsp");
  } else {
   System.out.println("----------------2");
   User user = new User();
   user.setUsername(name);
   session.setAttribute("user", user);// 对象存入session会激发监听器中valueBound方法的运行
   response.sendRedirect("online.jsp");
  }
 %>

online.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.ljb.listener.User,com.ljb.constants.Constants" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <%
  User user = null;
  if (session.getAttribute("user") == null) {
   response.sendRedirect("enter.jsp");
  } else {
   user = (User)session.getAttribute("user");
 %>
 欢迎你:<%=user.getUsername() %>
 此时在线人数为:<%=Constants.ONLINE_USER_COUNT %>
 <a href="doOut.jsp">离开</a>
 <%} %>
</body>
</html>

doOut.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <%
  session.invalidate();
  response.sendRedirect("enter.jsp");
 %>
</body>
</html>

执行结果:

欢迎你:123 此时在线人数为:2 离开

七、小结

 

八、初识MVC

 

相关文章
相关标签/搜索