JavaWeb的监听器和过滤器是什么(javaweb,开发技术)

时间:2024-04-30 07:48:50 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

    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.监听器三大作用域

    JavaWeb的监听器和过滤器是什么

    JavaWeb的监听器和过滤器是什么

    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.过滤器

    JavaWeb的监听器和过滤器是什么

    4.1过滤器的使用

    1.编写java 类实现Filter接口2.重写doFilter()方法3.设置拦截的url

    4.2过滤器的拦截路径

    /*:根目录下所有请求都拦截

    /*.do:所有带.do的请求都拦截

    /*.jsp

    4.3过滤器的拦截顺序

    过滤器的拦截顺序,取决于在配置文件web.xml的先后顺序

    4.4过滤器的四种拦截方式

    JavaWeb的监听器和过滤器是什么
    JavaWeb的监听器和过滤器是什么

     </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
    本文:JavaWeb的监听器和过滤器是什么的详细内容,希望对您有所帮助,信息来源于网络。
    上一篇:怎么在HTML中插入空格下一篇:

    9 人围观 / 0 条评论 ↓快速评论↓

    (必须)

    (必须,保密)

    阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18