Web事件处理与过滤器
实验预习内容
- 在Servlet 2.4规范中共定义了三种对象上的6种事件类型和8个事件监听器接口,他们分别是什么?请用表格形式上表示出来。
- 如何配置Servlet监听器程序?
<listener>
<listener-class>
listener.MyServletContextListener
</listener-class>
</listener>
- 简述过滤器的工作原理。
答:a) 编写过滤器:AuditFilter.java
b) 编译:编译后的类文件存放在WEB-INF\classes目录中
c) 部署:filter
<filter-name>auditFilter</filter-name>
<filter-class>filter.AuditFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>auditFilter</filter-name>
<url-pattern>/login.do</url-pattern>
</filter-mapping>
- 如何配置和访问带参数的过滤器?
filter标签声明过滤器
<!ELEMENT filter(description?,display-name?, icon?, filter-name, filter-class, init-param*)>
filter-mapping设置映射
<!ELEMENT filter-mapping(filter-name, (url-Pattern | servlet-name))>
例如:<filter-mapping>
<filter-name>validatorFilter</filter-name>
<url-pattern> *.doc </url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>validatorFilter</filter-name>
<servlet-name>/login.do</servlet-name>
</filter-mapping>
实验内容与要求
- 创建一个名为exp06的Web项目,编写、部署、测试一个ServletContext事件监听器。
【步骤1】编写监听器类MyServletContextListener.java,Web应用程序启动时创建一个数据源对象,并将其保存在ServletContext作用域中,Web应用销毁时将其清除;在ServletContext对象上添加属性、删除属性和替换属性时,在Tomcat日志中记录有关信息,包括提示信息、属性名和属性值等。
import java.util.Date;
import javax.servlet.*;
public final class MyServletContextListener implements ServletContextListener, ServletContextAttributeListener {
private ServletContext context = null;
public void contextInitialized(ServletContextEvent sce){
context = sce.getServletContext();
context.log("应用程序已启动:"+new Date());
}
public void contextDestroyed(ServletContextEvent sce){
context = sce.getServletContext();
context.log("应用程序已关闭:"+new Date());
}
public void attributeAdded(ServletContextAttributeEvent sce){
context = sce.getServletContext();
context.log("添加一个属性:"+sce.getName()+":"+sce.getValue());
}
public void attributeRemoved(ServletContextAttributeEvent sce){
context = sce.getServletContext();
context.log("删除一个属性:"+sce.getName()+":"+sce.getValue());
}
public void attributeReplaced(ServletContextAttributeEvent sce){
context = sce.getServletContext();
context.log("替换一个属性:"+sce.getName()+":"+sce.getValue());
}
}
【步骤2】在web.xml文件中注册监听器类。
【步骤3】编写监听器测试页面:contextListenerTest.jsp:使用监听器创建的数据源对象连接是一次实验创建的MySQL数据库test,以表格的形式显示其中books数据表的所有内容。
<%--
Created by IntelliJ IDEA.
User: The Last SundayTime
Date: 2021/5/14
Time: 19:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.util.Date" %>
<html>
<head>
<title>contextListenerTest</title>
</head>
<body>
<%
ServletContext context =null;
context = getServletConfig().getServletContext();
response.setContentType("text/html");
out.println("<html><head><title>Event Test</title></head>");
Integer anInteger =new Integer(100);
Date aDate = new Date();
context.setAttribute("obj",anInteger); //添加属性
context.setAttribute("obj",aDate); //替换属性
context.removeAttribute("obj"); //删除属性
out.println("<h3 align=center>"+ "已在ServletContext对象上添加、替换和删除了属性</h3>");
out.println("</body></html>");
out.close();
%>
</body>
</html>
【步骤4】在Tomcat下发布该Web应用,启动Tomcat,访问测试页面。
【步骤5】检查日志文件
打开<CATALINA_HOME>\logs目录中的localhost.yyyy-mm-dd.log日志文件,查看执行事件监听器后写到日志文件中的信息。
2. 编写一个HttpSession事件监听器用来记录当前在线人数。
【步骤1】编写MySessionListener监听器处理类,监视Web应用会话创建事件:每创建一个新的会话对象,就将其保存到会话对象的列表数组中,并将用户会话对象列表保存在ServletContext作用域中的sessionList属性中,同时向日志中写入“创建一个新会话”以及该会话的ID。当一个会话对象被删除时,从用户会话对象列表中删除该会话对象并保存,同时向日志中写入“删除一个会话”以及该会话的ID。
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public final class MySessionListener implements HttpSessionListener{
private int count = 0;
public void sessionCreated(HttpSessionEvent se){
count++;
se.getSession().getServletContext().setAttribute(
"onlineCount",new Integer(count));
}
public void sessionDestroyed(HttpSessionEvent se){
count--;
se.getSession().getServletContext().setAttribute(
"onlineCount",new Integer(count));
}
}
【步骤2】在web.xml文件中注册该事件监听器。
【步骤3】编写一个测试该监听器的页面sessionDisplay.jsp,显示当前应用所有在线的会话对象的id及创建时间。多打开几个浏览器窗口,模拟多用户访问,查看多用户会话统计出的结果。
<%--
Created by IntelliJ IDEA.
User: The Last SundayTime
Date: 2021/5/14
Time: 20:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>sessionDisplay</title>
</head>
<body>
<%
ServletContext context = getServletConfig().getServletContext();
Integer count =(Integer)context.getAttribute("onlineCount");
response.setContentType("text/html;charset=UTF-8");
out.println("<html><head><title>Session Event Test</title></head>");
out.println("<h3 align=center><font color=\"#ff0000\">"+
"当前在线人数:"+count+"</h3>");
out.println("</body></html>");
out.close();
%>
</body>
</html>
3. 编写一个ServletRequestListener监听器,记录某个页面自应用程序启动以来被访问的次数。
【步骤1】编写监听器接口MyRequestListener,在对指定页面onlineCount.jsp发送请求时进行该页面访问次数计数器累加,并将计数器变量保存到应用作用域的属性中。
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public final class MyRequestListener implements ServletRequestListener{
private int count = 0;
public void requestInitialized(ServletRequestEvent re){
HttpServletRequest request= (HttpServletRequest)re.getServletRequest();
if(request.getRequestURI().equals("/exp06/onlineCount.jsp")){
count++;
re.getServletContext().setAttribute("count",new Integer(count));
}
}
public void requestDestroyed(ServletRequestEvent re){
}
}
【步骤2】在web.xml文件中注册该监听器。
【步骤3】编写一个JSP页面onlineCount.jsp。
<%--
Created by IntelliJ IDEA.
User: The Last SundayTime
Date: 2021/5/14
Time: 20:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>onlineCount</title>
</head>
<body>
欢迎您,您的IP地址是<%= request.getRemoteAddr() %>
<p>自应用程序启动以来,该页面被访问了
<span style="color: blue; "><%=application.getAttribute("count")%>
</span>次<br>
</body>
</html>
【步骤4】启动多个浏览器窗口访问该jsp页面,展示并分析程序的运行结果。