1.实现ServletContainerInitializer
2.实现ServletContextListener
3.实现Filter
4.servlet启动init
ServletContainerInitializer
package com.xy.core;
import com.xy.util.ReflectionUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.util.WebUtils;
import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.Servlet;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.HandlesTypes;
import javax.servlet.http.HttpServlet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* @ClassName ContainerInitializer
* @Description
* @Author xy
* @Date 2022/5/20
* @Version 1.0
**/
//@HandlesTypes({ HttpServlet.class, Filter.class , Initializer.class})
@HandlesTypes({ Initializer.class})
public class ContainerInitializer implements ServletContainerInitializer, ApplicationContextInitializer<GenericApplicationContext> {
@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
List<Class<Initializer>> initializerClasses = new ArrayList();
for(Class clazz : classes) {
if (Initializer.class.isAssignableFrom(clazz)) {
initializerClasses.add(clazz);
}
}
for (Class<Initializer> initializerClass : initializerClasses) {
try {
((Initializer)initializerClass.newInstance()).init();
} catch (IllegalAccessException | InstantiationException var26) {
//throw ....
}
}
}
@Override
public void initialize(GenericApplicationContext genericApplicationContext) {
}
}
ServletContextListener
package com.xy.core;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* @ClassName MyServletContextListener
* @Description
* @Author xy
* @Date 2022/5/20
* @Version 1.0
**/
@WebListener
public class MyServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
System.out.println("------MyServletContextListener----------");
}
}
Filter
?
package com.xy.core;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* @ClassName MyFilter
* @Description
* @Author xy
* @Date 2022/5/20
* @Version 1.0
**/
@WebFilter
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("-------MyFilter-----");
// Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
HttpServlet
?
package com.xy.servlet;
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;
/**
* @ClassName MyServlet
* @Description
* @Author xy
* @Date 2022/5/19
* @Version 1.0
**/
@WebServlet(
urlPatterns = {"/my/servlet"},
loadOnStartup = 1
)
public class MyServlet extends HttpServlet {
public void init() throws ServletException {
System.out.println("-------------servlet init--------------------");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-------------doGet--------------------");
resp.getWriter().println("this is doGet");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-------------doPost--------------------");
resp.getWriter().println("this is doPost");
}
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-------------doPut--------------------");
resp.getWriter().println("this is doPut");
}
}
控制台打印初始化顺序:
----------ServletContainerInitializer ?init------- ------ServletContextListener init ---------- -------Filter init ----- -------------servlet init--------------------
|