实现Filter接口
一般用于完成通用的操作,如:登陆验证、统一编码处理、敏感字符过滤等
/index.jsp
/user/*
*.jsp
/*
web.xml配置
<filter>
<filter-name>demo</filter-name>
<filter-class>包名.类名</filter-class>
</filter>
<filter-mapping>
<filter-name>demo</filter-name>
<!-- 拦截路径 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
注解配置
设置 dispatcherTypes 属性
web.xml配置
设置 <dispatcher></dispatcher>
标签
<filter-mapping>
按照定义顺序执行实现ServletContextListener接口
ServletContextListener
:监听ServletContext对象的创建和销毁
void contextInitialized(ServletContextEvent sce)
:ServletContext 对象创建后会调用该方法void contextDestroyed(ServletContextEvent sce)
:ServletContext 对象被销毁之前会调用该方法配置
web.xml
<listener>
<listener-class>包名.类名</listener-class>
</listener>
指定初始化参数 <context-param>
进行资源文件的加载
注解:
@WebListener
原文:https://www.cnblogs.com/yxmhl/p/10661985.html