springboot中使用servlet

springboot中使用原生servlet有二种方式,
第一种方式
定义一个UserServlet继承HttpServlet,可以自己实现get,post方法

package com.zhihao.miao;

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

@WebServlet("/user/servlet")
public class UserServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("user servlet");
    }

}

定义一个LogFilter实现Filter,监听/user/servlet请求

package com.zhihao.miao;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/user/servlet")
public class LogFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("income log filter " + request.getRemoteHost());
        chain.doFilter(request, response);

    }

    public void destroy() {

    }

}

定义一个监听器实现ServletContextListener

package com.zhihao.miao;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.time.LocalDateTime;

@WebListener
public class MyContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("app startup at " + LocalDateTime.now().toString());
    }

    public void contextDestroyed(ServletContextEvent sce) {
    }

}

启动类启动:

@ServletComponentScan
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

}

启动服务,监听器执行了


springboot中使用servlet_第1张图片

访问http://localhost:8080/user/servlet

springboot中使用servlet_第2张图片

过滤器执行


总结:

  • 编写servlet,然后加上相应的注解@WebServlet("/user/servlet"),@WebFilter("/user/servlet"),@WebListener
  • 需要启用@ServletComponentScan注解
  • 这种方法适合于servlet3.0版本

方法二

定义BookServlet继承HttpServlet,编写自己的doget,dopost方法

package com.zhihao.miao.demo2;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BookServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hi Springboot Servlet!!!!");
    }

}

定义EchoFilter实现Filter接口,过滤相应的请求

package com.zhihao.miao.demo2;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class EchoFilter implements Filter {

    public void init(FilterConfig config) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req =(HttpServletRequest)request;
        System.out.println("spring boot web filter " + req.getRequestURI());
        chain.doFilter(request, response);
    }

    public void destroy() {

    }
}

定义StartupListener实现ServletContextListener

package com.zhihao.miao.demo2;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class StartupListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        System.out.println("=================");
        System.out.println("application is started");
    }

    public void contextDestroyed(ServletContextEvent event) {

    }

}

写一个配置类注册Servlet,Filter,Listener到spring容器中

package com.zhihao.miao.demo2;

import java.util.Arrays;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootConfiguration
public class ServletConfiguration {

    //注册setvlet
    @Bean
    public ServletRegistrationBean createBookServlet(){
        ServletRegistrationBean servlet = new ServletRegistrationBean(new BookServlet(),"/book.do");
        return servlet;
    }

    //过滤哪些url
    @Bean
    public FilterRegistrationBean createFilterRegistrationBean(){
        FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new EchoFilter());
        filter.setUrlPatterns(Arrays.asList("/book.do"));
        return filter;
    }

    //配置监听器
    @Bean
    public ServletListenerRegistrationBean createServletListenerRegistrationBean(){
        ServletListenerRegistrationBean listener =new ServletListenerRegistrationBean<>(new StartupListener());
        return listener;
    }
}

启动类测试:

package com.zhihao.miao.demo2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

监听器执行


访问servlet


springboot中使用servlet_第3张图片

过滤器执行


总结:

  • 编写Servlet,Filter,Listener
  • 装配相应的bean到spring容器中
    servlet->ServletRegistrationBean
    filter ->FilterRegistrationBean
    listener -> ServletListenerRegistrationBean
  • 这种方法适合servlet3.0,2.5及其以下版本

你可能感兴趣的:(springboot中使用servlet)