JavaWeb(10)过滤器和监听器

Filter简介

Filter也称之为过滤器,它是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源

:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限

访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。

Filter的创建与配置
(1)编写java类实现Filter接口,并实现其doFilter方法。
例:publicclass filter implements Filter {
	//调用doFilter方法
	publicvoid doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
                //设置编码格式为utf-8
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;chatset=utf-8");
                //放行,将请求的信息和响应的信息编码格式都设置为utf-8后放行
		chain.doFilter(request, response);
	}
}
(2)当我们实现doFilter方法后,我们要在web.xml中进行配置
在web.xml文件中对编写的filter类进行注册,并设置它所能拦截的资源。
web.xml配置各节点介绍:
<filter>指定一个过滤器。
<filter-name>用于为过滤器指定一个名字,该元素的内容不能为空。
<filter-class>元素用于指定过滤器的完整的限定类名。
<filter-mapping>元素用于设置一个 Filter 所负责拦截的资源。
<filter-name>子元素用于设置filter的注册名称。该值必须是在<filter>元素中声明过的过滤器的名字
<url-pattern>设置 filter 所拦截的请求路径(过滤器关联的URL样式)

例: <filter>
		<filter-name>filter</filter-name>
		<filter-class>guolu.filter(限定名)</filter-class>
    </filter>
    <filter-mapping>
		<filter-name>filter</filter-name>
		<url-pattern>/*(所有的请求)</url-pattern>
    </filter-mapping>
注意:<filter>和<filter-mapping>中的<filter-name>的参数值要相同

过滤器的生命周期

当我们在实现Filter接口时,除了doFilter的方法,其中还有两种方法,一种为初始化方法,一种为销毁方法

初始化方法:

publicvoid init(FilterConfig arg0) throws ServletException {

//在服务器启动是执行此方法(init()方法)
}

 

销毁方法:

publicvoid destroy() {

        //在服务器关闭时调用此方法(destroy()方法)

}

生命周期:初始化->执行过滤->销毁

过滤器应用

通过过滤器设置编码方式
例:publicclass filter implements Filter {
	//调用doFilter方法
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException 	{
	//设置编码格式为utf-8
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;chatset=utf-8");
	//放行,将请求的信息和响应的信息编码格式都设置为utf-8后放行
		chain.doFilter(request, response);
	}
}

使用过滤器来过滤IP
例:public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
	//获取请求的ip地址
	String addr = request.getRemoteAddr();
	//获取本机的ip地址
	String local = request.getLocalAddr();
	//当请求的ip地址与本机的ip地址相同时,放行,其他的ip地址无法访问
	if(addr.equals(local)){
		chain.doFilter(request, response);
	}
}
例:publicclass filter implements Filter {
	//调用doFilter方法
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException 	{
	//设置编码格式为utf-8
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;chatset=utf-8");
	//放行,将请求的信息和响应的信息编码格式都设置为utf-8后放行
		chain.doFilter(request, response);
	}
}

使用过滤器来过滤IP
例:public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
	//获取请求的ip地址
	String addr = request.getRemoteAddr();
	//获取本机的ip地址
	String local = request.getLocalAddr();
	//当请求的ip地址与本机的ip地址相同时,放行,其他的ip地址无法访问
	if(addr.equals(local)){
		chain.doFilter(request, response);
	}
}

Listener简介

监听器也叫Listener,是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一

些操作,比如监听在线的用户的数量。

会话监听器
1.实现HttpSessionListener接口,在接口中重写两个方法
publicclass CountLinstener implementsHttpSessionListener
{
		publicvoid sessionCreated(HttpSessionEvent event) {

	}

		publicvoid sessionDestroyed(HttpSessionEvent event) {
			
		}

	}
2.在实现HttpSessionListener接口后要在web.xml中进行配置
<listener>
	<listener-class>com.bwie.linstener.CountLinstener</listener-class>
</listener>
注意:com.bwie.linstener.CountLinstener为限定名

1.实现HttpSessionListener接口,在接口中重写两个方法
publicclass CountLinstener implementsHttpSessionListener
{
		publicvoid sessionCreated(HttpSessionEvent event) {

	}

		publicvoid sessionDestroyed(HttpSessionEvent event) {
			
		}

	}
2.在实现HttpSessionListener接口后要在web.xml中进行配置
<listener>
	<listener-class>com.bwie.linstener.CountLinstener</listener-class>
</listener>
注意:com.bwie.linstener.CountLinstener为限定名
监听器应用
使用会话监听器监测在线人数
1.实现接口
publicclass CountLinstener implementsHttpSessionListener
{
intsum = 0;//初始化访问人数
		publicvoid sessionCreated(HttpSessionEvent event) {
System.out.println("当前访问人数:"+(++sum));
	}


		publicvoid sessionDestroyed(HttpSessionEvent event) {
			System.out.println("当前访问人数:"+(--sum));
		}


	}
2.配置web.xml
<listener>
	<listener-class>com.bwie.linstener.CountLinstener</listener-class>
</listener>

1.实现接口
publicclass CountLinstener implementsHttpSessionListener
{
intsum = 0;//初始化访问人数
		publicvoid sessionCreated(HttpSessionEvent event) {
System.out.println("当前访问人数:"+(++sum));
	}


		publicvoid sessionDestroyed(HttpSessionEvent event) {
			System.out.println("当前访问人数:"+(--sum));
		}


	}
2.配置web.xml
<listener>
	<listener-class>com.bwie.linstener.CountLinstener</listener-class>
</listener>
发布了40 篇原创文章 · 获赞 10 · 访问量 4056

猜你喜欢

转载自blog.csdn.net/qq_41693150/article/details/80334061