在servlet目录下创建个servlet类,示例代码如下:

package com.bjpowernode.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; public class HeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("he springboot servlet");
resp.getWriter().flush();
resp.getWriter().close(); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}

编写配置类:

package com.bjpowernode.springboot.config;

import com.bjpowernode.springboot.servlet.HeServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.servlet.ServletRegistration; @Configuration
public class ServletConfig { @Bean
public ServletRegistrationBean heServletRegisterBen() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new HeServlet(), "/servlet/heservlet"
);
return servletRegistrationBean;
}
}

注意需要加的两个注解:@Configuration  还有@Bean

然后就可以启动springboot项目访问地址:http://localhost:8080/servlet/heservlet

进行测试了。

springboot中使用servlet通过配置类的更多相关文章

  1. springboot中使用filter用配置类方式

    在03-springboot-web的Filter包下,创建HeFilter类 代码示例: package com.bjpowernode.springboot.filter; import java ...

  2. SpringBoot中使用UEditor基本配置(图文详解)

    SpringBoot中使用UEditor基本配置(图文详解) 2018年03月12日 10:52:32 BigPotR 阅读数:4497   最近因工作需要,在自己研究百度的富文本编辑器UEditor ...

  3. springmvc以及springboot中的拦截器配置

    拦截器两种实现   如果不同的controller中都需要拦截器,不能使用相同的拦截器,因为拦截器不能跨controller,这个时候只能为不同的controller配置不同的拦截器,每一个拦截器只能 ...

  4. SpringBoot中使用Servlet,Filter,Listener

    项目最近在替换之前陈旧的框架,改用SpringBoot进行重构,初接触,暂时还没有用到Servlet,Filter,Listener的地方,但在之前回顾Servlet的生命周期时,https://ww ...

  5. Springboot中SpringMvc拦截器配置与应用(实战)

    一.什么是拦截器,及其作用 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对 ...

  6. SpringBoot中SpringMVC的自动配置以及扩展

    一.问题引入 我们在SSM中使用SpringMVC的时候,需要由我们自己写SpringMVC的配置文件,需要用到什么就要自己配什么,配置起来也特别的麻烦.我们使用SpringBoot的时候没有进行配置 ...

  7. SpringBoot中application.yml基本配置详情

    把原有的application.properties删掉.然后 maven -X clean install,或者通过Maven Project双击clean和install(1)端口服务配置 #端口 ...

  8. SpringBoot中加载XML配置

    开篇 在SpringBoot中我们通常都是基于注解来开发的,实话说其实这个功能比较鸡肋,但是,SpringBoot中还是能做到的.所以用不用是一回事,会不会又是另外一回事. 涛锅锅在个人能力能掌握的范 ...

  9. SpringBoot中自定义properties文件配置参数并带有输入提示

    1. 创建配置类 在项目中创建一个参数映射类如下 @ConfigurationProperties(prefix = "user.info") public class MyPro ...

随机推荐

  1. Jenkins笔录

    1.Linux下安装jdk8的方法 ,只需要一条命令就可以安装jdk: yum install java-1.8.0-openjdk* -y 执行过这条命令无需配置,直接可以使用. 2.JDK12版本 ...

  2. linux 基础 yum 安装

    ls /dev/cdrom mkdir /mnt/cdrom mount -r /dev/cdrom /mnt/cdrom

  3. react中component存在性能问题

    Component存在的问题? 1). 父组件重新render(), 当前组件也会重新执行render(), 即使没有任何变化 2). 当前组件setState(), 重新执行render(), 即使 ...

  4. nginx reload的原理

    nginx启动时,会启动两个进程: 一个是Master进程和worker进程.改变配置后nginx做的事1)改变了nginx配置之后,HUP signal的信号需要发送给主进程.2)主进程首先会检测新 ...

  5. 云计算共享组件--Memcache缓存系统(4)

    一.缓存系统 一.静态web页面: 1.在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告 ...

  6. C++学习笔记-const

    const在C++中有着大量的运用,深刻理解const有助于进一步理解C++. const基础知识 int main() { const int a;//C++中必须初始化 int const b;/ ...

  7. NModbus4的使用

    步骤1:打开串口 SerialPort port = new SerialPort("COM7") port.BaudRate = ; port.BaudRate = ; port ...

  8. 在C语言中函数及其调用过程

    目录 函数 C语言中的变参函数 函数的本质是什么 内存区域的区分技巧 函数的调用过程 栈帧的概念 调用过程细节 按照约定传参 函数 如果一个函数有声明没实现,那么就会出现链接错误: 以上代码会出现链接 ...

  9. 在.Net中使用RedLock实现分布式锁

    ⒈简介 RedLock 分布式锁算法由 Redis 的作者提出,大部分语言都有对应的实现,查看,RedLock.net 是 RedLock 分布式锁算法的 .NET 版实现,用来解决分布式下的并发问题 ...

  10. 2019.07.05 纪中_B

    今日膜拜:czj大佬orz%%% 2019.07.05[NOIP提高组]模拟 B 组 今天做题的时候大概能判断出题人的考点,可是就是没学过...特别痛苦 T0:栈的定义,模拟就好了T1:感觉像是找规律 ...