温馨提示×

温馨提示×

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

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

SpringBoot如何整合Servlet

发布时间:2021-09-29 17:15:48 来源:亿速云 阅读:156 作者:柒染 栏目:大数据

今天就跟大家聊聊有关SpringBoot如何整合Servlet,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

SpringBoot 整合 Servlet

一、准备项目:

使用Spring Initializr创建项目,选择Web组件

二、方式1:通过注解方式

1. 创建Servlet类

需要在Servlet类上增加@WebServlet 注解 ,urlPatterns指定匹配Url

//com.zhl.springbootweb.servlet.FirstServlet

package com.zhl.springbootweb.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;
/*
* 整合Servlet方式一
* */
@WebServlet(name = "FirstServlet",urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("First Servlet");
    }
}

2.修改启动类

在启动类上增加@ServletComponentScan注解

package com.zhl.springbootweb;

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

@SpringBootApplication
/*在SpringBoot启动时会扫描@WebServlet注解,并将该类实例化*/
@ServletComponentScan
public class SpringbootWebApplication {

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

}

三、方式二:通过Bean注入方式

1.创建Servlet

  • 不需要增加@WebServlet注解

//com.zhl.springbootweb.servlet.SecondServlet

package com.zhl.springbootweb.servlet;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/*整合Servlet 方式二*/public class SecondServlet extends HttpServlet {@Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Second Servlet");
    }
}

2.创建配置类 

  • 使用@Configuration标识为配置类

  • 使用@Bean 标识为组件

  • addUrlMappings 方法添加URL匹配

//com.zhl.springbootweb.config.ServletConfig

package com.zhl.springbootweb.config;

import com.zhl.springbootweb.servlet.SecondServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletConfig {

    /*完成Servlet组件注册*/
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new SecondServlet());
        servletRegistrationBean.addUrlMappings("/second");
        return  servletRegistrationBean;
    }
}

看完上述内容,你们对SpringBoot如何整合Servlet有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

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

AI