过滤器和监听器
过滤器基础
Filter简介
Filter也称之为过滤器,它是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。
Filter的创建与配置
1.编写Java类实现Filter接口,并实现其doFilter方法
例:public class filter implements Filter {
//调用doFilter方法
public void doFilter(ServletRequet request ,ServletResponse response,FilterChain chain) throws IOException,ServleteException{
//设置编码格式为utf-8
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
response.setContextType(“text/html;charsete=utf-8”);
//放行,将请求的信息和相应的信息编码格式都设置为utf-8后放行
chain.doFilter(request,response);
}
}
2.当我们实现doFilter方法后,我们要在web.xml中进行配置
在web.xml文件中对编写的filter类进行注册,并设置它所能拦截的资源。
web.xml配置各节点介绍:
指定一个过滤器
用于为过滤器指定一个名字,该元素的内容不能为空
元素用于指定过滤器的完整得到限定类名
元素用于设置一个Filter所负责拦截的资源
子元素用于设置filter的注册名称,该值必须是在元素中生命过的过滤器的名字
设置filter所拦截的请求路径(过滤器关联的url样式)
例:
filter
guolu.filter(限定名)
filter
/*(所有的请求)
注意:和中的参数值要相同
3 . 过滤器的生命周期
当我们在实现Filter接口时,除了doFilter的方法,其中还有两种方法,一种为初始化方法,一种为销毁方法
publicvoid init(FilterConfig arg0) throws ServletException {
//在服务器启动是执行此方法(init()方法)
}
销毁方法:publicvoid destroy() {
//在服务器关闭时调用此方法(destroy()方法)
}
生命周期:初始化->执行过滤->销毁
过滤器应用
1.通过过滤器设置编码方式
例: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.使用过滤器来过滤IP
publicvoid 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的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。 -
会话监听器
//实现HttpSessionListener接口,在接口中重写两个方法
publicclass CountLinstener implementsHttpSessionListener
{
publicvoid sessionCreated(HttpSessionEvent event) {}
publicvoid sessionDestroyed(HttpSessionEvent event) { }
}
//在实现HttpSessionListener接口后要在web.xml中进行配置
com.linstener.CountLinstener
监听器应用
使用会话监听器监测在线人数
publicclass CountLinstener implementsHttpSessionListener
{
intsum = 0;//初始化访问人数
publicvoid sessionCreated(HttpSessionEvent event) {
System.out.println(“当前访问人数:”+(++sum));
}
publicvoid sessionDestroyed(HttpSessionEvent event) { System.out.println("当前访问人数:"+(--sum)); } }
2.配置web.xml
com.linstener.CountLinstener
- javaweb之过滤器监听器
- 过滤器、监听器、拦截器的区别
- 关于过滤器,拦截器,监听器具体应用上的区别
- java 拦截器,监听器,过滤器比较
- AAA过滤器、监听器、拦截器的区别
- java拦截器,过滤器,监听器的区别和执行顺序
- 过滤器、监听器、拦截器的区别
- JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用
- 过滤器和监听器
- web.xml 中的过滤器(拦截器)Filter与监听器Listener的作用和区别?
- Servlet,过滤器,监听器,拦截器的区别
- Listener和Filter监听器和过滤器
- Servlet,过滤器,监听器,拦截器的区别
- 过滤器、监听器
- JavaWeb(过滤器+监听器)
- 过滤器、监听器、拦截器的区别
- 小鸦-拦截器和监听器,过滤器的区别
- Servlet中的过滤器(拦截器)Filter与监听器Listener的作用和区别
- Web中的监听器【Listener】与过滤器【Filter】 实例
- 软件开发工程师(JAVA)中级考试大纲之----五 J2EE WEB高级组件开发(二)Web过滤器组件技术、Web监听器组件技术;