Spring Boot - 使用Servlet,Filter和Listener - 2

上一篇文章介绍了在Spring Boot应用中如何使用@Bean的方式注册Servlet,Filter和Listener。

本文介绍如何使用@WebServlet@WebFilter@WebListener注解类分别注册Servlet,Filter和Listener。

依赖jar包

编辑pom.xml文件,添加spring-boot-starter-web依赖。


  4.0.0
  com.boraji.tutorial.springboot
  spring-boot-servlet-filter-example
  0.0.1-SNAPSHOT
  Spring Boot - Servlet, Filter and  Listener example
  war
  
    1.8
  
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.9.RELEASE
  
  
    
      org.springframework.boot
      spring-boot-starter-web
    
  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  

创建Servlet,Filter和Listener

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("/myServlet")
public class MyServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    System.out.println("MyServlet's doGet() method is invoked.");
    doAction(req, resp);
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    System.out.println("MyServlet's doPost() method is invoked.");
    doAction(req, resp);
  }
  
  private void doAction(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String name = req.getParameter("name");
    resp.setContentType("text/plain");
    resp.getWriter().write("Hello " + name + "!");
  }
  
}

Filter

import java.io.IOException;
import java.util.Enumeration;

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.annotation.WebFilter;

@WebFilter("/myServlet")
public class MyFilter implements Filter {

  @Override
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
      throws IOException, ServletException {
    
    System.out.println("MyFilter doFilter() is invoked.");
    Enumeration params = req.getParameterNames();
    while (params.hasMoreElements()) {
      String param=params.nextElement();
      System.out.println("Parameter:"+param+"\tValue:"+req.getParameter(param));
    }
    chain.doFilter(req, res);
  }

  @Override
  public void init(FilterConfig config) throws ServletException {
    
  }

  @Override
  public void destroy() {

  }

}

Listener

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyServletContextListener implements ServletContextListener {
  @Override
  public void contextInitialized(ServletContextEvent e) {
    System.out.println("MyServletContextListener Context Initialized");
  }

  @Override
  public void contextDestroyed(ServletContextEvent e) {
    System.out.println("MyServletContextListener Context Destroyed");
  }

}

注册Servlet,Filter和Listener

注册servlet组件(如Servlet,Filter和Listener),需要使用@ServletComponentScan注解配置类。@ServletComponentScan这个注解会扫描指定包中的servlet组件。

创建SpringBootApp类,并使用注解@ServletComponentScan@SpringBootApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan(basePackages = { 
    "com.boraji.tutorial.springboot.servlet",
    "com.boraji.tutorial.springboot.filter", 
    "com.boraji.tutorial.springboot.listener"
   })
public class SpringBootApp {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootApp.class, args);
  }

}

运行

运行SpringBootApp.java类。可以通过Run -> Run as -> Java Application或者使用mvn spring-boot:run命令运行spring boot应用。

启动成功后,在浏览器输入http://localhost:8080/myServlet?name=Sunil%20Singh%20Bora

Spring Boot - 使用Servlet,Filter和Listener - 2_第1张图片
浏览器显示

你可能感兴趣的:(Spring Boot - 使用Servlet,Filter和Listener - 2)