JavaWeb的监听器和过滤器你了解吗
1.监听器---->Context,Session
what is listener?
监听器是一个接口内容由我们实现,会在特定时间被调用,监听器用于监听web应用中三大域对象(request,session,application),信息的创建,销毁,增加,修改,删除等动作的发生,然后做出相应的响应处理。当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。常用于统计在线人数和在线用户,系统加载时进行信息初始化,统计网站的访问量等。
ContextListener
通过实现ServletContextListener
来进行全局监听
ContextListener可以通过记录用户访问网站的次数思路:用户通过访问index.jsp,来获取存放在监听器中的hashmap< String,Integer>
,然后在index.jsp中进行判断。
ContextListener的代码思路如下:
public class ContextListener1 implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("init"); //创建map集合 Map<String, Integer> map = new HashMap<String, Integer>(); // 获取全局对象 ServletContext context = servletContextEvent.getServletContext(); context.setAttribute("map", map); System.out.println(map.isEmpty()); System.out.println(map); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("destory"); } }
index.jsp的代码思路如下:
<% //获取用户ip地址 String ServerName = request.getServerName(); //获取全局对象 Map<String, Integer> map = (Map<String, Integer>) application.getAttribute("map"); if (map.containsKey(ServerName)) { map.put(ServerName, map.get(ServerName) + 1); } else { map.put(ServerName, 1); } int count = map.get(ServerName); int size = map.size(); %> <h4>ip地址是:<%=ServerName%>,您是第<%=count%>位访问的用户,当前服务器共被<%=size%>个用户访问过</h4>
2.监听器三大作用域
3.属性监听器
属性监听器主要监听属性值的变化,例如request.setAttribute()
等这些数据的变化。
package listener; import javax.servlet.*; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; /** * @author wjs * @create 2022-02-27 15:09 */ public class AttrListener implements ServletContextAttributeListener, ServletRequestAttributeListener, HttpSessionAttributeListener { @Override public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) { // 向全局作用域中添加值的监听器 } @Override public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) { // 向全局作用域删除值的监听器 } @Override public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) { // 向全局域对象修改值的监听器 } @Override public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) { // 向request域中添加值的监听器 } @Override public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) { // 向request域中删除值的监听器 } @Override public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) { // 向request域中修改值的监听器 } @Override public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) { // 向session域中添加值的监听器 } @Override public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) { // 向session域中删除值的监听器 } @Override public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) { // 向session域中修改值的监听器 } }
4.过滤器
4.1过滤器的使用
1.编写java 类实现Filter接口 2.重写doFilter()方法 3.设置拦截的url
4.2过滤器的拦截路径
/*:根目录下所有请求都拦截
/*.do:所有带.do的请求都拦截
/*.jsp
4.3过滤器的拦截顺序
过滤器的拦截顺序,取决于在配置文件web.xml的先后顺序
4.4过滤器的四种拦截方式
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
相关文章
java web学习_浅谈request对象中get和post的差异
下面小编就为大家带来一篇java web学习_浅谈request对象中get和post的差异。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-05-05SpringBoot实现前后端、json数据交互以及Controller接收参数的几种常用方式
这篇文章主要给大家介绍了关于SpringBoot实现前后端、json数据交互以及Controller接收参数的几种常用方式,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2022-03-03通过Java来测试JSON和Protocol Buffer的传输文件大小
这篇文章主要介绍了通过Java来测试JSON和Protocol Buffer的传输文件大小,Protocol Buffer(文中简称Protobuffer)是谷歌开发的新的文件传输格式,需要的朋友可以参考下2015-12-12
最新评论