springboot integrated web (a) ----------- servlet, filter, listener, static resource access

New springboot project

https://blog.csdn.net/qq_43560721/article/details/104653470

1.servlet

  • Register by annotating the scan is complete Servlet component

Create a servlet

package com.example.sbservlet.servlet;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="MyServlet",urlPatterns="/hello")
public class MyServlet extends HttpServlet{

    public void doGet(HttpServletRequest req,HttpServletResponse resp)
            throws ServletException, IOException {

        System.out.println("doGet");
    }
}

Write launcher

package com.example.sbservlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan//springBoot启动时会扫描@WebServlet注解,并实例化类
public class SbservletApplication {

    public static void main(String[] args) {
        SpringApplication.run(SbservletApplication.class, args);
    }

}

Browser access

 

 

 

  • By way to do Servlet component registration

Creating the Servlet

package com.example.sbservlet2.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class YouServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws  ServletException, IOException {

        System.out.println("doGet");
    }
}

Write launcher

package com.example.sbservlet2;

import com.example.sbservlet2.servlet.YouServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Sbservlet2Application {

    public static void main(String[] args) {
        SpringApplication.run(Sbservlet2Application.class, args);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })//忽略一些警告
    @Bean
    public ServletRegistrationBean getServletRegistrationBean() {

        ServletRegistrationBean bean = new ServletRegistrationBean(new YouServlet());
        bean.addUrlMappings("/hello");
        return bean;

    }


}

Browser access

 

 

 

 

 

2.filter

  • Filter by notes to complete the registration scanning

Create a Filter class

package com.example.sbfilter.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;


@WebFilter(filterName="MyFileter",urlPatterns="/hello")
public class MyFilter implements Filter {
    //1、通过注解扫描完成Filter注册
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("doFilter before");
        chain.doFilter(request, response);
        System.out.println("doFileter after");
    }

}

Write launcher

package com.example.sbfilter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class SbfilterApplication {

    public static void main(String[] args) {
        SpringApplication.run(SbfilterApplication.class, args);
    }

}

Enter the browser HTTP: // localhost: 8080 / H ello

 

 

 

  • Filter components by means of complete registration

Create a Filter class

package com.example.sbfilter2.filter;
import javax.servlet.*;
import java.io.IOException;

public class YouFilter implements Filter {
    //2、通过方法完成Filter组件注册
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("doFilter before");
        chain.doFilter(request, response);
        System.out.println("doFilter after");
    }


}

Write launcher

package com.example.sbfilter2;

import com.example.sbfilter2.filter.YouFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.Filter;

@SpringBootApplication
public class Sbfilter2Application {

    public static void main(String[] args) {
        SpringApplication.run(Sbfilter2Application.class, args);
    }

    /**
     * 注册Filter
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    public FilterRegistrationBean<Filter> getFilterRegistrationBean(){

        FilterRegistrationBean bean = new FilterRegistrationBean(new YouFilter());
        bean.addUrlPatterns("/hello");
        return bean;
    }

}

Enter the browser HTTP: // localhost: 8080 / H ello

 

 

 

 

3.listener

  • Listener registration is completed by scanning notes

Create a Listener class

package com.example.sblistener.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
//通过注解扫描完成Listener注册
@WebListener
public class MyListener implements ServletContextListener{

    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Listener");
    }
}

Write launcher

package com.example.sblistener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class SblistenerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SblistenerApplication.class, args);
    }

}

Run starter class

 

 

 

  • Listener registration is completed by the method

Create a Listener class

package com.example.sblistener2.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class YouListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {

        System.out.println("Listener init");
    }
}

Write launcher

package com.example.sblistener2;

import com.example.sblistener2.listener.YouListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Sblistener2Application {

    public static void main(String[] args) {
        SpringApplication.run(Sblistener2Application.class, args);
    }
    //通过方法完成Listener注册
    @Bean
    public ServletListenerRegistrationBean<YouListener>
    getServletListenerRegistrationBean(){

        ServletListenerRegistrationBean<YouListener> bean
                = new ServletListenerRegistrationBean<YouListener>(new YouListener());
        return bean;
    }

}

Run starter class

 

 

4. Static Resource Access

  • SpringBoot accessed from the classpath / static directory

        

 

  • ServletContext under the root directory

       

 

 

 

 

Published 141 original articles · won praise 33 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_43560721/article/details/104949894