JavaWeb的监听器和过滤器是什么
导读:本文共2707字符,通常情况下阅读需要9分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 1.监听器---->Context,Sessionwhat is listener?监听器是一个接口内容由我们实现,会在特定时间被调用,监听器用于监听web应用中三大域对象(request,session,application),信息的创建,销毁,增加,修改,删除等动作的发生,然后做出相应的响应处理。当范围对象的状态发生变化的时候,服务器自动调用监听器对... ...
目录
(为您整理了一些要点),点击可以直达。1.监听器---->Context,Session
what is listener?
监听器是一个接口内容由我们实现,会在特定时间被调用,监听器用于监听web应用中三大域对象(request,session,application),信息的创建,销毁,增加,修改,删除等动作的发生,然后做出相应的响应处理。当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。常用于统计在线人数和在线用户,系统加载时进行信息初始化,统计网站的访问量等。
ContextListener
通过实现ServletContextListener
来进行全局监听
ContextListener可以通过记录用户访问网站的次数思路:用户通过访问index.jsp,来获取存放在监听器中的hashmap< String,Integer>
,然后在index.jsp中进行判断。
ContextListener的代码思路如下:
publicclassContextListener1implementsServletContextListener{@OverridepublicvoidcontextInitialized(ServletContextEventservletContextEvent){System.out.println("init");//创建map集合Map<String,Integer>map=newHashMap<String,Integer>();//获取全局对象ServletContextcontext=servletContextEvent.getServletContext();context.setAttribute("map",map);System.out.println(map.isEmpty());System.out.println(map);}@OverridepublicvoidcontextDestroyed(ServletContextEventservletContextEvent){System.out.println("destory");}}
index.jsp的代码思路如下:
<%//获取用户ip地址StringServerName=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);}intcount=map.get(ServerName);intsize=map.size();%><h5>ip地址是:<%=ServerName%>,您是第<%=count%>位访问的用户,当前服务器共被<%=size%>个用户访问过</h5>
2.监听器三大作用域
3.属性监听器
属性监听器主要监听属性值的变化,例如request.setAttribute()
等这些数据的变化。
packagelistener;importjavax.servlet.*;importjavax.servlet.http.HttpSessionAttributeListener;importjavax.servlet.http.HttpSessionBindingEvent;/***@authorwjs*@create2022-02-2715:09*/publicclassAttrListenerimplementsServletContextAttributeListener,ServletRequestAttributeListener,HttpSessionAttributeListener{@OverridepublicvoidattributeAdded(ServletContextAttributeEventservletContextAttributeEvent){//向全局作用域中添加值的监听器}@OverridepublicvoidattributeRemoved(ServletContextAttributeEventservletContextAttributeEvent){//向全局作用域删除值的监听器}@OverridepublicvoidattributeReplaced(ServletContextAttributeEventservletContextAttributeEvent){//向全局域对象修改值的监听器}@OverridepublicvoidattributeAdded(ServletRequestAttributeEventservletRequestAttributeEvent){//向request域中添加值的监听器}@OverridepublicvoidattributeRemoved(ServletRequestAttributeEventservletRequestAttributeEvent){//向request域中删除值的监听器}@OverridepublicvoidattributeReplaced(ServletRequestAttributeEventservletRequestAttributeEvent){//向request域中修改值的监听器}@OverridepublicvoidattributeAdded(HttpSessionBindingEventhttpSessionBindingEvent){//向session域中添加值的监听器}@OverridepublicvoidattributeRemoved(HttpSessionBindingEventhttpSessionBindingEvent){//向session域中删除值的监听器}@OverridepublicvoidattributeReplaced(HttpSessionBindingEventhttpSessionBindingEvent){//向session域中修改值的监听器}}
4.过滤器
4.1过滤器的使用
1.编写java 类实现Filter接口2.重写doFilter()方法3.设置拦截的url
4.2过滤器的拦截路径
/*:根目录下所有请求都拦截
/*.do:所有带.do的请求都拦截
/*.jsp
4.3过滤器的拦截顺序
过滤器的拦截顺序,取决于在配置文件web.xml的先后顺序
4.4过滤器的四种拦截方式
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
JavaWeb的监听器和过滤器是什么的详细内容,希望对您有所帮助,信息来源于网络。