springboot統合ウェブ()-----------サーブレット、フィルタ、リスナー、静的リソースへのアクセス

新springbootプロジェクト

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

1.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");
    }
}

ランチャーを書きます

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);
    }

}

ブラウザアクセス

 

 

 

  • サーブレットのコンポーネント登録を行う方法により、

サーブレットを作成します

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");
    }
}

ランチャーを書きます

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;

    }


}

ブラウザアクセス

 

 

 

 

 

2.filter

  • 登録スキャンを完了するためにノートをフィルタ

Filterクラスを作成します。

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");
    }

}

ランチャーを書きます

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);
    }

}

ブラウザに入力してHTTPを:// localhostを:8080 / H ello

 

 

 

  • 登録完了によるフィルタ部品

Filterクラスを作成します。

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");
    }


}

ランチャーを書きます

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;
    }

}

ブラウザに入力してHTTPを:// localhostを:8080 / H ello

 

 

 

 

3.listener

  • リスナー登録がスキャンノートによって完成されます

リスナークラスを作成します。

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");
    }
}

ランチャーを書きます

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);
    }

}

ファイル名を指定して実行スタータークラス

 

 

 

  • リスナー登録が法によって完成されます

リスナークラスを作成します。

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");
    }
}

ランチャーを書きます

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;
    }

}

ファイル名を指定して実行スタータークラス

 

 

4.静的リソースアクセス

  • SpringBootはクラスパス/静的ディレクトリからアクセス

        

 

  • ルートディレクトリの下のServletContext

       

 

 

 

 

公開された141元の記事 ウォン称賛33 ビュー50000 +

おすすめ

転載: blog.csdn.net/qq_43560721/article/details/104949894