美文网首页
SpringBoot2 - Servlet

SpringBoot2 - Servlet

作者: 深入浅出 | 来源:发表于2018-06-22 09:47 被阅读0次

    传统的Spring项目,配置Servlet需要在web.xml内添加对应的标签以及映射路径规则。

    SpringBoot项目中如何创建Servlet以及自动装载到SpringBoot中?

    构建项目

    访问start.spring.io/
    添加WEB依赖、Servlet依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
    </dependency>
    

    创建Servlet

    package com.gala.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 TestServlet extends HttpServlet {
    
        private static final long serialVersionUID = -2646554587718007415L;
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 设置返回类型为json
            resp.setContentType("application/json");
            // 设置返回字符集
            resp.setCharacterEncoding("UTF-8");
            // 输出对象
            PrintWriter writer = resp.getWriter();
            // 输出error消息
            writer.write("成功执行doGet方法");
            writer.close();
        }
    }
    

    使用Bean注册Servlet(单个Servlet)

    新建ServletConfiguration配置类,并且添加一个@Bean的方法返回值为ServletRegistrationBean

    package com.gala.servlet.conf;
    
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.gala.servlet.TestServlet;
    
    @Configuration
    public class ServletConfiguration {
    
        @Bean
        public ServletRegistrationBean<TestServlet> servletRegistrationBean() {
            return new ServletRegistrationBean<TestServlet>(new TestServlet(), "/test");
        }
    }
    

    启动项目,访问地址:http://127.0.0.1:8080/test

    SpringBoot自动装配Servlet(多个Servlet)

    SpringBoot内部提供了注解@ServletComponentScan,这个注解的作用就是自动扫描我们SpringBoot项目内的有关Servlet配置,自动装配到我们的项目中。

    TestServlet需要与ServletConfiguration同目录或在其子目录中

    1. 修改ServletConfiguration,增加@ServletComponentScan
    package com.gala.servlet.conf;
    
    import org.springframework.boot.web.servlet.ServletComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ServletComponentScan
    public class ServletConfiguration {
    
    }
    
    1. 修改TestServlet,增加@WebServlet(urlPatterns = "/test2")
    package com.gala.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;
    
    @WebServlet(urlPatterns = "/test2")
    public class TestServlet extends HttpServlet {
    
        private static final long serialVersionUID = -2646554587718007415L;
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 设置返回类型为json
            resp.setContentType("application/json");
            // 设置返回字符集
            resp.setCharacterEncoding("UTF-8");
            // 输出对象
            PrintWriter writer = resp.getWriter();
            // 输出error消息
            writer.write("成功执行doGet方法");
            writer.close();
        }
    }
    

    启动项目,访问地址:http://127.0.0.1:8080/test2

    相关文章

      网友评论

          本文标题:SpringBoot2 - Servlet

          本文链接:https://www.haomeiwen.com/subject/afwhyftx.html