SpringBoot整合Servlet的两种方式 - 极悦

SpringBoot教程

SpringBoot整合Servlet的两种方式

方式一:通过注解扫描方式实现

项目名称:030-springboot-servlet-01

1.通过注解方式创建一个Servlet

在com.abc.springboot.servlet包下创建MyServlet

package com.abc.springboot.servlet;

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;

/**
 * ClassName:MyServlet
 * Package:com.abc.springboot.servlet
 * Description:

 */
@WebServlet(urlPatterns = "/myServlet")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("My SpringBoot Servlet");
        response.getWriter().flush();
        response.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}

2.在主应用程序Application类上添加@ServletComponentScan("com.abc.springboot.servlet")

package com.abc.springboot;

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

@SpringBootApplication
@ServletComponentScan(basePackages = "com.abc.springboot.servlet")
public class Application {

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

}

3.启动应用SpringBoot,浏览器访问测试

方式二: 通过SpringBoot的配置类实现(组件注册)

项目名称:031-springboot-servlet-02

1.创建一个普通的Servlet

在com.abc.springboot.servlet包下创建HeServlet类

package com.abc.springboot.servlet;

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

/**
 * ClassName:HeServlet
 * Package:com.abc.springboot.servlet
 * Description:

 */
public class HeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("He SpringBoot Servlet");
        response.getWriter().flush();
        response.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}

2.编写一个Spring Boot的配置类,在该类中注册Servlet

在com.abc.springboot.config包下,创建ServletConfig配置类

package com.abc.springboot.config;

import com.abc.springboot.servlet.HeServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName:ServletConfig
 * Package:com.abc.springboot.config
 * Description:

 */
@Configuration //添加@Configuration 将此类变为配置变
public class ServletConfig {

    /**
     * @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名
     * 如下代码相当于
     * 
     *     
     * 
     * @return
     */
    @Bean
    public ServletRegistrationBean heServletRegistrationBean() {

        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new HeServlet(),"/servlet/heServlet");

        return servletRegistrationBean;
    }
}

3.启动应用SpringBoot,浏览器访问测试

技术文档推荐

更多>>

视频教程推荐

更多>>