监听器(网站在线统计实验)与过滤器(request解码response编码实验)使用

来源:互联网 发布:如何修改网站数据库 编辑:程序博客网 时间:2024/05/18 13:26

过滤器

Java web开发中的过滤器(filter)是从Servlet2.3规范开始增加的功能,并在2.4中得到增强。对Web应用来说,过滤器是一个驻留在服务器端的Web组件,它可以截取客户端和服务器之间的请求与响应信息,并对这些信息进行过滤。当Web容器接受到一个资源的请求时,它将判断是否有过滤器与这个资源相关联。如果有,那么容器将把这个请求交给过滤器进行处理。在过滤器中你可以改变请求的内容。或者重新设置请求的报头信息,然后再将请求发送给目标资源。当目标资源对请求做出响应的时候容器同样将会响应先转发给过滤器,在过滤器中你可以对响应的内容进行转换,然后再将响应发送到客户端。
常见的过滤器用途主要包括:对用户请求进行统一认证,对用户的访问请求进行记录和审核,对用户发送的数据进行过滤或者替换,转换图像格式、对响应内容进行压缩以减少传输量,对请求或响应进行加解密处理,触发资源访问时间,对XML的输出应用XSlT等。
和过滤器相关的接口有:Filter,FilterConfig和FilterChain
这里在servlet3中进行对编码的过滤

实验

/** * Created by admin on 2017/8/29. */@WebFilter(urlPatterns = {"*"}, initParams = {@WebInitParam(name = "encoding", value = "utf-8")})public class CodingFilter implements Filter {    private String defaultEnceding = "utf-8";    public void init(FilterConfig config) throws ServletException {        String encoding = config.getInitParameter("encoding");        if (encoding != null) {            defaultEnceding = encoding;        }    }    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {        req.setCharacterEncoding(defaultEnceding);        resp.setCharacterEncoding(defaultEnceding);        chain.doFilter(req, resp);    }    public void destroy() {    }}

监听器

Java Web开发中的监听器(listener)就是application(ServletContext),session(HttpSession),request(ServletRequest)三个对象创建,销毁或者往其中添加修改删除属性时自动执行代码的功能组件,如下所示
1 ServletContextListener:对servlet上下文的创建和销毁进行监听。
2 ServletContextAttributeListener:监听Servlet上下文的属性的添加、删除和替换
3 HttpSessionListener:对Session的创建和销毁进行监听。(销毁有两种情况,1session超时web.xml/中配置2调用session的invalidate方法使session失效)
4 HttpSessionAttributeListener:对Session对象中属性的添加、删除和替换进行监听。
5 ServletRequestListener:对请求对象的初始化和销毁进行监听
6 ServletRequestAttributeListener:对请求对象属性的添加,删除和替换进行监听。

举例统计网站最多在线人数的监听

这里需要借助两个就可以实现,一个创建application域的监听(初始化),另外一个是对正会话的监听(修改在线人数)

import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;/** * 这里的域使用的是application,作用域是整个服务器环境,服务器重启后清零 * Created by admin on 2017/8/29. */@WebListenerpublic class InitListener implements ServletContextListener {    @Override    public void contextInitialized(ServletContextEvent evt) {        evt.getServletContext().setAttribute("onLineCount", 0);        evt.getServletContext().setAttribute("maxOnLineCount", 0);    }    @Override    public void contextDestroyed(ServletContextEvent evt) {    }}
import javax.servlet.ServletContext;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;/** * 统计在线人数,可以通过用户会话建立和销毁来进行监听,而对于用户的监听对应的是session域,每一个用户上线 * 下线都会出现对应的session的创建和销毁 * Created by admin on 2017/8/29. */@WebListenerpublic class MaxCountListener implements HttpSessionListener {    @Override    public void sessionCreated(HttpSessionEvent event) {        ServletContext application = event.getSession().getServletContext();        int count = Integer.parseInt(application.getAttribute("onLineCount").toString());        count++;        int maxOnlineCount = Integer.parseInt(application.getAttribute("maxOnlineCount").toString());        if (count > maxOnlineCount) {            application.setAttribute("maxOnlineCount", count);            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");            application.setAttribute("date", df.format(new Date()));        }    }    @Override    public void sessionDestroyed(HttpSessionEvent event) {        ServletContext application = event.getSession().getServletContext();        int count = Integer.parseInt(application.getAttribute("onLineCount").toString());        application.setAttribute("onLineCount", --count);    }}
阅读全文
0 0
原创粉丝点击