JavaWeb的三大组件:servlet ,filter,listener
概念:当访问服务器资源时,过滤器可以将请求拦截下来,完成一些 特殊 的功能
作用:
一般用于完成通用的操作,比如:登录校验,统一编码,敏感字符过滤...
步骤:
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("我被拦截了");
chain.doFilter(req, resp);
System.out.println("我回来了");
}
init:在服务器启动后,会创建Filter对象,调用init方法,只执行一次,用于加载资源
doFilter:每次请求被拦截时,会执行,执行多次
destroy:服务器正常关闭后,即Filter对象被摧毁,只执行一次
拦截路径配置:
拦截方式配置:资源访问方式
注解配置(web.xml配置也行):
逗号隔开:设置dispatcherType
执行顺序:如果有过滤器1和过滤器2
过滤器的执行顺:
注解配置:按照类名的字符比较规则比较,直小的先执行
web.xml配置:
案例:检查是否登录的案例,如果还没登录,就跳转到登录页面进行登录
package com.servlets.serv;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/ServletLogin")
public class ServletLogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setMaxInactiveInterval(60 * 10);
session.setAttribute("login", "true");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
package com.servlets.filters;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("*.jsp")
public class FilterTest1 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//判断是否已经登录了,如果没有登录就需要跳转到登录界面,但是如果访问的资源路径本来
//就是登录页面,那么就不需要拦截了,这里需要进行判断排除
//记得强制转型
HttpServletRequest request = (HttpServletRequest)req ;
//获取session对象,看看对应的属性是否有值
HttpSession session = request.getSession();
Object login = session.getAttribute("login");
//获取url路径,看看里面是否是登录资源相关的
String requestURI = request.getRequestURI();
//在过滤掉登录相关时,一定要排除一些js,css文件
if (login != null || requestURI.contains("/longin.jsp") || requestURI.contains("/css/") ||
requestURI.contains("/js/") || requestURI.contains("/fonts/") ) {
chain.doFilter(req, resp);
} else {
request.getRequestDispatcher("/longin.jsp").forward(request, resp);
}
}
public void init(FilterConfig config) throws ServletException {
}
}
注意点:
案例2:过滤敏感词汇
package com.servlets.filters;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
@WebFilter("/testservlet")
public class TestFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//增强方法
ServletRequest proxyInstance = (ServletRequest)Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("getParameter")) {
//增强的是返回值
//所以先执行原本的方法,拿到返回值
String invoke = (String)method.invoke(req, args);
//修改返回值
if (invoke.contains("笨蛋")) {
invoke = invoke.replace("笨蛋", "***");
}
//再把修改的返回值返回给方法调用者
return invoke;
}
//如果不是getParameter方法,那么就照样执行就ok了
return method.invoke(req, args);
}
});
//记得放行增强后的代理对象
chain.doFilter(proxyInstance, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
package com.servlets.serv;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/testservlet")
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String msg = request.getParameter("msg");
System.out.println(name + ":" + msg);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
事件:一个事情,或者说某个动作,如单击,双击
事件源:发生事件的地方,比如键盘,鼠标
监听器:一个对象(一段代码)
注册监听:把前面的事件,事件源,监听器绑定在一起,在事件源发生的事件触发一个对象的相关代码。
简单介绍一个:ServletContextListener 监听ServletContext的创建和销毁
public void contextInitialized(ServletContextEvent sce) ServletContext对象被创建之前调用的方法
public void contextDestroyed(ServletContextEvent sce) ServletContext对象被销毁之前调用的方法
步骤:
package com.servlets.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebListener;
@WebListener
public class TestListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("对象被创建了");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("对象被销毁了");
}
}
注意:
在web项目中的src文件目录下的文件,我们是通过WEB-INF目录下的classes目录进行查找的。