温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot 中的Servlet简单使用

发布时间:2020-10-16 14:26:05 来源:脚本之家 阅读:160 作者:牛头人 栏目:编程语言

当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。

Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?

在spring boot中添加自己的Servlet有两种方法,代码注册Servlet和注解自动注册(Filter和Listener也是如此)。

一、代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。

也可以通过实现 ServletContextInitializer 接口直接注册。

二、在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。

1.通过代码注册Servlet示例代码

1).SpringBootSimpleApplication.Java类:

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.example.servlet.MyServlet;
@SpringBootApplication
public class SpringBootSimpleApplication {
  /**
   * 使用代码注册Servlet(不需要@ServletComponentScan注解)
   */
  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    return new ServletRegistrationBean(new MyServlet(), "/st/*");// ServletName默认值为首字母小写,即myServlet
  }
  public static void main(String[] args) {
    SpringApplication.run(SpringBootSimpleApplication.class, args);
  }
}

2).MyServlet.java类:

package com.example.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet{
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");
    doPost(req, resp);
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");
    resp.setContentType("text/html"); 
    resp.setCharacterEncoding("utf-8");
    PrintWriter out = resp.getWriter(); 
    out.println("<html>"); 
    out.println("<head>"); 
    out.println("<title>Hello World</title>"); 
    out.println("</head>"); 
    out.println("<body>"); 
    out.println("<h2>大家好,我的名字叫Servlet</h2>"); 
    out.println("</body>"); 
    out.println("</html>"); 
  }
}

2.使用注解注册Servlet示例代码

1).SpringBootSimpleApplication.java类:

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.example.servlet.MyServlet;
@SpringBootApplication
@ServletComponentScan
public class SpringBootSimpleApplication {
  /**
   * 使用代码注册Servlet(不需要@ServletComponentScan注解)
   */
  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    return new ServletRegistrationBean(new MyServlet(), "/st/*");// ServletName默认值为首字母小写,即myServlet
  }
  public static void main(String[] args) {
    SpringApplication.run(SpringBootSimpleApplication.class, args);
  }
}

2).MyServlet2.java类:

package com.example.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// 不指定name的情况下,name默认值为类全路径,即com.example.servlet.MyServlet2
@WebServlet(urlPatterns="/st/myservlet2", description="Servlet的说明")
public class Myservlet2 extends HttpServlet{
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println(">>>>>>>>>>doGet2()<<<<<<<<<<<");
    doPost(req, resp);
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println(">>>>>>>>>>doPost2()<<<<<<<<<<<");
    resp.setContentType("text/html");
    resp.setCharacterEncoding("utf-8");
    PrintWriter out = resp.getWriter(); 
    out.println("<html>"); 
    out.println("<head>"); 
    out.println("<title>Hello World</title>"); 
    out.println("</head>"); 
    out.println("<body>"); 
    out.println("<h2>大家好,我的名字叫Servlet2</h2>"); 
    out.println("</body>"); 
    out.println("</html>"); 
  }
}

使用 @WebServlet 注解,其中可以设置一些属性。

3.访问结果

Spring Boot 中的Servlet简单使用

4.DispatcherServlet默认拦截

DispatcherServlet 默认拦截“/”,MyServlet 拦截“/st/*”,MyServlet2 拦截“/st/myservlet”,那么在我们访问 http://localhost:8080/st/myservlet2 的时候系统会怎么处理呢?如果访问 http://localhost:8080/st/abc的时候又是什么结果呢?其结果是“匹配的优先级是从精确到模糊,复合条件的Servlet并不会都执行”。

既然系统DispatcherServlet 默认拦截“/”,那么我们是否能做修改呢,答案是肯定的,我们在SpringBootSampleApplication中添加代码:

 /**
   * 修改DispatcherServlet默认配置
   */
  @Bean
  public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
    ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
    registration.getUrlMappings().clear();
    registration.addUrlMappings("*.do");
    registration.addUrlMappings("*.json");
    return registration;
  }

可以通过注入DispatcherServlet 然后用ServletRegistrationBean包裹一层 动态的加上一些初始参数。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI