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

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

这篇文章主要为大家展示了“JavaWeb的监听器和过滤器是什么”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JavaWeb的监听器和过滤器是什么”这篇文章吧。

    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过滤器的四种拦截方式


    以上是“JavaWeb的监听器和过滤器是什么”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道!

    发布于 2022-03-03 21:30:05
    收藏
    分享
    海报
    0 条评论
    36
    上一篇:Matlab空心散点检测的示例分析 下一篇:怎么在HTML中插入空格
    目录

      0 条评论

      本站已关闭游客评论,请登录或者注册后再评论吧~

      忘记密码?

      图形验证码