봄 부팅 통합 서블릿, 필터, 리스너, 액세스 정적 자원

봄 부팅 통합 서블릿 (두 가지)

  1. 새로운 Maven 프로젝트 생성
    그림 삽입 설명 여기
    완료 후 구조 다이어그램을 생성하기를 :
    그림 삽입 설명 여기
  2. 의 도입에 따라 pom.xml 파일
	<!--引入父项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>
    <dependencies>
        <!--SpringBoot web启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

첫 번째 방법 (스캔 모드를 주석으로 등록 서블릿 구성 요소를 완료하기 위해) :

  1. 스캔에 의해 주석을 완료하는 서블릿 구성 요소가 등록
    1.1. 서블릿 만들기

그림 삽입 설명 여기
1.2 서블릿 코드를 작성 :

@WebServlet(name = "firstServlet",urlPatterns = "/firstServlet") //urlPatterns:访问路径
public class firstServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进来了firstServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }

1.3 클래스를 쓰기 시작
springboot 시작 클래스를 생성하는
그림 삽입 설명 여기
코드 :

@SpringBootApplication
//在spring boot启动时会扫描@WebServlet注解,并创建该类的实例
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

참고 : 시작 클래스의 필요성은 @ServletComponentScan 코멘트 수단을 추가 할 수 있습니다 : 당신이 인스턴스 @WebServlet 노트를 스캔 시작할 때, 서블릿 작성

1.4 브라우저에서 시작 클래스를 실행하고 localhost를 입력 : 8080 firstServlet를 /
그림 삽입 설명 여기
콘솔 출력
그림 삽입 설명 여기

(전체 등록에 서블릿 구성에 의해) 번째 방법

  1. 서블릿 만들기
    그림 삽입 설명 여기
  2. 만들기 springboot 클래스는 시작
    그림 삽입 설명 여기
    등록 서블릿 방법의 주요 구성 요소에 새로운 방법을
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    //添加一个方法,方法名无要求,必须返回ServletRegistrationBean。注册Servlet对象
    @Bean     //主键等价于<bean>标签
    public ServletRegistrationBean<SecondServlet> getServletRegistrationBean(){
        ServletRegistrationBean<SecondServlet> bean=
                new ServletRegistrationBean<SecondServlet>(new SecondServlet(),"/SecondServlet");
        return bean;
    }
}
  1. 브라우저 localhost를 입력에서 실행 시작 클래스 : 8080 / SecondServlet
    그림 삽입 설명 여기
  2. 콘솔 정보를 인쇄
    그림 삽입 설명 여기

Springboot 통합 필터 (서블릿과 통합 된 방식과 유사)

제 1 실시 예 (주사하여 등록 Fliter 주석 성분을 완료)

  1. 필터 클래스 만들기
    그림 삽입 설명 여기
  2. 인터페이스 필터 상속은 부모 클래스가 구현하는
    그림 삽입 설명 여기
    다음과 같은 코드가 있습니다 :
@WebFilter(filterName = "firstFilter", urlPatterns = "/firstFilter")
public class firstFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("----进入FirstFilter-----");
        chain.doFilter(request, response);//放行
        System.out.println("----离开FirstFilter-----");
    }
}
  1. 시작 클래스 만들기

    다음 코드는 :
@SpringBootApplication
//在spring boot启动时会扫描@WebServlet @WebFilter @WebListener注解,并创建该类的实例
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

브라우저에서 시작 클래스를 실행하고 localhost를 입력 : 8080 / firstFilter
그림 삽입 설명 여기
여기에 신문이 출시 된 이후 쓰기 경로 (404)는하지 않았기 때문에;

콘솔 정보를 출력한다 :
그림 삽입 설명 여기

두 번째 방법 (수단과 방법에 의해 등록 필터 구성 요소를 완료)

  1. 메모를 작성하지 않고 필터 클래스를 작성 @WebFilter
    그림 삽입 설명 여기
  2. 수업 시작
    그림 삽입 설명 여기
    다음과 같이 코드입니다 :
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    //添加一个方法
    @Bean
    public FilterRegistrationBean<SecondFilter> getFilterRegistrationBean(){
        FilterRegistrationBean<SecondFilter> bean=
                new FilterRegistrationBean<SecondFilter>(new SecondFilter());
        bean.addUrlPatterns("*.do","*.jsp","/SecondFilter"); //以.do , .jsp , SecondFilter结尾路径的都会进到过滤器
        return bean;
    }
}

  1. 8080 / SecondFilter의 : 브라우저에 localhost를 입력에서 실행 시작 클래스
    그림 삽입 설명 여기
    콘솔 인쇄 정보 :
    그림 삽입 설명 여기

Springboot 통합 수신기 (같은 책)

등록 노트를 스캔하여 전체 조립 Fliter

  1. 클래스 리스너 만들기
    그림 삽입 설명 여기
    그림 삽입 설명 여기
    리스너 코드 :
@WebListener()
public class firstListener implements ServletContextListener{
    //监听application对象的创建
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("-----------application对象创建-----------------");
    }
}

  1. 시작 클래스 생성
    그림 삽입 설명 여기
    코드 :
@SpringBootApplication
@ServletComponentScan  //在spring boot启动时会扫描@WebServlet @WebFilter @WebListener注解,并创建该类的实例
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
  1. 실행 시작 클래스는 콘솔 인쇄 정보를 볼 수 있습니다
    그림 삽입 설명 여기

두 번째 방법 (방법으로는 수신기 구성 요소 등록 완료)

AS는 코드 코드에 직접 코드를 생략한다

  1. 리스너 클래스 만들기
  2. 시작 클래스
    그림 삽입 설명 여기
    코드 :
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    @Bean
    public ServletListenerRegistrationBean<firstListener> getServletListenerRegistrationBean(){
        ServletListenerRegistrationBean<firstListener> bean=
                new ServletListenerRegistrationBean<firstListener>(new firstListener());
        return bean;
    }
}
  1. 실행 시작 클래스는 콘솔 인쇄 정보를 볼 수 있습니다
    그림 삽입 설명 여기

Springboot 액세스 정적 자원 (두 가지)

첫 번째 방법 (ServletContext 내의 루트에보고하여 정적 리소스)

웹 애플리케이션의 SRC / 메인 디렉토리에 생성 1. (디렉토리 이름하는 것은 웹 애플리케이션이어야 함)
과 같은 다른 정적 리소스를 저장하는 웹 애플리케이션에서 다른 디렉토리를 생성합니다 : 이미지의 사진을 넣어.
그림 삽입 설명 여기
실행 액세스 경로와 같은 리소스에 직접 액세스를 시작하는 2
그림 삽입 설명 여기
그림 삽입 설명 여기

두 번째 방법 (디렉토리 클래스 경로에서 정적 자원에 대한보기 / 정적의)

SRC / 메인 / 자원에서 정적 디렉토리 (디렉토리 이름은 정적이어야합니다) 만들기
등 :. 이미지 사진을 넣어, 정적 다른 정적 자원에 저장된 다른 디렉토리를 생성
그림 삽입 설명 여기
2. 실행에 직접 액세스 리소스에 액세스 경로에 클래스 브라우저를 시작합니다
그림 삽입 설명 여기
그림 삽입 설명 여기

이들은이 튜토리얼의 내용에 관련된, 감사보고를 들어, 소스를 표시하시기 바랍니다

추천

출처www.cnblogs.com/joker-dj/p/12657528.html