springboot web integrado (a) ----------- servlet, filtro, oyente, acceso a los recursos estáticos

Nuevo proyecto springboot

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

1.servlet

  • Register anotando la exploración es componente completo Servlet

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

lanzador de escritura

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

}

el acceso del navegador

 

 

 

  • A modo de hacer el registro de componentes Servlet

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

lanzador de escritura

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;

    }


}

el acceso del navegador

 

 

 

 

 

2.filter

  • Filtrar por notas para completar el escaneo de registro

Crear una clase de filtro

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

}

lanzador de escritura

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

}

Introduzca el navegador http: // localhost: 8080 / H Ello

 

 

 

  • componentes filtro por medio de inscripción completa

Crear una clase de filtro

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


}

lanzador de escritura

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

}

Introduzca el navegador http: // localhost: 8080 / H Ello

 

 

 

 

3.listener

  • registro oyente se completa con notas de escaneo

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

lanzador de escritura

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

}

Inicio de Clases Run

 

 

 

  • registro oyente se completa con el método

Crear una clase Listener

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

lanzador de escritura

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

}

Inicio de Clases Run

 

 

4. estática de acceso de recursos

  • SpringBoot accede desde el directorio de ruta de clases / estática

        

 

  • ServletContext en el directorio raíz

       

 

 

 

 

Publicados 141 artículos originales · ganado elogios 33 · Vistas a 50000 +

Supongo que te gusta

Origin blog.csdn.net/qq_43560721/article/details/104949894
Recomendado
Clasificación