整合Servlet两种方式
方式一:写一个类继承HttpServlet 使用@WebServlet注解
@WebServlet(name = "FirstServlet",urlPatterns = "/first")
public class FristServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get servlet...");
PrintWriter out= resp.getWriter();
out.write("success");
out.flush();
out.close();
}
}
测试类 @ServletComponentScan这个是关键 需要写上
@SpringBootApplication
@ServletComponentScan
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
访问localhost:8080/first
方式二:写一个类继承HttpServlet
public class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get SecondServlet...");
PrintWriter out= resp.getWriter();
out.write("SecondServlet success");
out.flush();
out.close();
}
}
测试,因为@SpringBootApplication 包含一个@Configuration 本质还是一个配置类,所以用@Configuration+@Bean的方式将servlet注入到容器 一样可以使用。
@SpringBootApplication
@ServletComponentScan
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
//servlert的第二种实现方式(@ServletComponentScan很关键)
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
}
整合Filter两种方式
方式一:使用@WebFilter注解
@WebFilter(urlPatterns="/first")
public class FirstFilter implements Filter {
//拦截器的第一种实现方式
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("-----FirstFilter init----------");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("-----前----------");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("-----后----------");
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
测试
@SpringBootApplication
@ServletComponentScan
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
方式二:因为@SpringBootApplication 包含一个@Configuration 本质还是一个配置类,所以用@Configuration+@Bean的方式将fileter注入到容器 一样可以使用。
public class SecondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("-----SecondFilter init----------");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("-----SecondFilter 前----------");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("-----SecondFilter 后----------");
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
测试类
@SpringBootApplication
@ServletComponentScan
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
//拦截器的第二种实现方式(@ServletComponentScan很关键)
@Bean
public FilterRegistrationBean getRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
bean.addUrlPatterns("/second");
return bean;
}
}
整合Listener两种方式
方式一:使用注解@WebListener
@WebListener
public class FirstListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("--------------FirstListener Initialized---------------");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("--------------FirstListener estroyed---------------");
}
}
测试类
@SpringBootApplication
@ServletComponentScan
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
方式二:
public class SecondListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("--------------SecondListener Initialized---------------");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("--------------SecondListener estroyed---------------");
}
}
测试类
@SpringBootApplication
@ServletComponentScan
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
//监听器的第二种实现方式(@ServletComponentScan很关键)
@Bean
public ServletListenerRegistrationBean getListenerRegistrationBean(){
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener());
return bean;
}
}