1、概述
2、拦截器
3、过滤器的生命周期
4、初始化参数和过滤器链
5、过滤器的使用场合
6、监听器
web应用程序事件模型的一部分,当web应用中的某些状态发生改变时,会产生相应的事件,监听器可以接收这些事件
并且在事件发生时做一些相关的处理。
样例:
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.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>
<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 离开
7、小结
8、初识MVC