web.xml加载顺序
1.先加载<context-param>标签
2.创建servletContext容器
3.把<context-parame>标签中数据转化成键值树交给servletContext容器
4.创建Listener实例
5.加载filter(过滤器)
6.加载Interceptor(拦截器)
7.加载servlet
注:filter加载顺序:根据web.xml中<filter-mapper>来决定 servlet一样如此
1.自定义Listener,我们需要实现ServletContextListener接口
public class MyListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
//加上自己的处理逻辑
}
public void contextDestroyed(ServletContextEvent event) {
//销毁时 处理逻辑
}
}
2.自定义filter,需要实现filter接口
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response =(HttpServletResponse) res;
//拦截业务逻辑
// 将控制权传递到下一个过滤器
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
3.自定义Interceptor,需要实现HandlerInterceptor接口 或者继承 HandlerInterceptorAdapter
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,Exception arg3) throws Exception {
// TODO Auto-generated method stub
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
//拦截逻辑 通过返回 true; 不通过返回false;
}
本文出自 “7619052” 博客,请务必保留此出处http://7629052.blog.51cto.com/7619052/1739892
原文地址:http://7629052.blog.51cto.com/7619052/1739892