springboot整合web(一)-----------servlet,filter,listener,静态资源访问

新建springboot项目

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

1.servlet

  • 通过注解扫描完成Servlet组件注册

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

}

浏览器访问

 

 

 

  • 通过方法完成Servlet组件的注册

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

编写启动器

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注册

创建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/hello

 

 

 

  • 通过方法完成Filter组件注册

创建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/hello

 

 

 

 

3.listener

  • 通过注解扫描完成Listener注册

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

}

运行启动器类

 

 

 

  • 通过方法完成Listener注册

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

编写启动器

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从classpath/static目录下访问

        

 

  • ServletContext根目录下

       

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值