SpringBoot中如何使用Servlet

发布时间:2022-07-08 10:17:58 作者:iii
来源:亿速云 阅读:213

今天小编给大家分享一下SpringBoot中如何使用Servlet的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

1.方式一(使用注解)

首先,我们写一个Servlet。要求就是简单的打印一句话。

在MyServlet这个类的上方使用 @WebServlet 注解来创建Servlet即可。

package com.songzihao.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;
 
/**
 *
 */
@WebServlet(urlPatterns = "/myservlet")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("My SpringBoot Servlet-1");
        resp.getWriter().flush();
        resp.getWriter().close();
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

之后在SpringBoot项目的入口类上方使用注解 @ServletComponentScan 注解来扫描Servlet中的注解即可。

package com.songzihao.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
 
@SpringBootApplication //开启spring配置
@ServletComponentScan(basePackages = "com.songzihao.springboot.servlet")
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

最后启动测试。

SpringBoot中如何使用Servlet

2.方式二(定义配置类)

仍然是先写一个 Servlet。这次不使用注解。

package com.songzihao.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;
 
/**
 *
 */
public class MyServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("My SpringBoot Servlet-2");
        resp.getWriter().flush();
        resp.getWriter().close();
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

然后再写一个配置类!!!

这个类的上方使用 @Configuration 注解,表名该类是一个配置类,相当于之前的各种xml配置文件。

在类中的方法上方使用 @Bean 注解,ServletRegistrationBean 这相当于是一个Servlet注册类,类似于之前的 <servlet>、<servlet-mapping> 标签的作用。

package com.songzihao.springboot.config;
import com.songzihao.springboot.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 *
 */
@Configuration //该注解将此类定义为一个配置类(相当于一个xml配置文件)
public class ServletConfig {
 
    /**
     * @Bean 是一个方法级别上的注解,主要用在配置类里
     * 相当于一个 <beans>
     *              <bean id="..." class="..." />
     *          </beans>
     * @return
     */
    @Bean
    public ServletRegistrationBean myServletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(
                new MyServlet(),"/myservlet"
        );
        return servletRegistrationBean;
    }
}

最后启动测试。

package com.songzihao.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

SpringBoot中如何使用Servlet

以上就是“SpringBoot中如何使用Servlet”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. 如何使用JavaWeb中Servlet
  2. servlet 与 springboot

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

springboot servlet

上一篇:vue基于websocket如何实现智能聊天及吸附动画效果

下一篇:python中的生成器、迭代器、装饰器怎么使用

相关阅读

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

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