springboot中使用servlet通过配置类
在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通过配置类的更多相关文章
- springboot中使用filter用配置类方式
在03-springboot-web的Filter包下,创建HeFilter类 代码示例: package com.bjpowernode.springboot.filter; import java ...
- SpringBoot中使用UEditor基本配置(图文详解)
SpringBoot中使用UEditor基本配置(图文详解) 2018年03月12日 10:52:32 BigPotR 阅读数:4497 最近因工作需要,在自己研究百度的富文本编辑器UEditor ...
- springmvc以及springboot中的拦截器配置
拦截器两种实现 如果不同的controller中都需要拦截器,不能使用相同的拦截器,因为拦截器不能跨controller,这个时候只能为不同的controller配置不同的拦截器,每一个拦截器只能 ...
- SpringBoot中使用Servlet,Filter,Listener
项目最近在替换之前陈旧的框架,改用SpringBoot进行重构,初接触,暂时还没有用到Servlet,Filter,Listener的地方,但在之前回顾Servlet的生命周期时,https://ww ...
- Springboot中SpringMvc拦截器配置与应用(实战)
一.什么是拦截器,及其作用 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对 ...
- SpringBoot中SpringMVC的自动配置以及扩展
一.问题引入 我们在SSM中使用SpringMVC的时候,需要由我们自己写SpringMVC的配置文件,需要用到什么就要自己配什么,配置起来也特别的麻烦.我们使用SpringBoot的时候没有进行配置 ...
- SpringBoot中application.yml基本配置详情
把原有的application.properties删掉.然后 maven -X clean install,或者通过Maven Project双击clean和install(1)端口服务配置 #端口 ...
- SpringBoot中加载XML配置
开篇 在SpringBoot中我们通常都是基于注解来开发的,实话说其实这个功能比较鸡肋,但是,SpringBoot中还是能做到的.所以用不用是一回事,会不会又是另外一回事. 涛锅锅在个人能力能掌握的范 ...
- SpringBoot中自定义properties文件配置参数并带有输入提示
1. 创建配置类 在项目中创建一个参数映射类如下 @ConfigurationProperties(prefix = "user.info") public class MyPro ...
随机推荐
- 在Linux中使用minikube
Minikebe Minikube是一个轻量级Kubernetes实现,它在本地机器上创建一个VM,并部署一个只包含一个节点的简单集群. Minikube使用Docker机器来管理Kubernetes ...
- docker命令大全与资源汇总
容器生命周期管理 run //创建一个新的容器并运行一个命令 start/stop/restart //启动一个或多个已经被停止的容器:停止一个运行中的容器:重启容器 kill //杀掉一个运行 ...
- 导包、时间模块【TIME、CALENDAR DATETIME】、SYS、OS, OS.PATH模块、项目开放周期&规范
导包 1.不考虑包的情况下直接from...直接导入文件夹下的具体模块 2. if __name__ == '__ma__' # 自执行 if __name__ == '模块名' # ...
- Nginx安装出现‘struct crypt_data’没有名为‘current_sal
centos 安装nginx 时出现src/os/unix/ngx_user.c:26:7: 错误:‘struct crypt_data’没有名为‘current_sal 解决办法: 将系统换成版本低 ...
- Flash-aware Page Replacement Algorithm
1.Abstract:(1)字体太乱,单词中有空格(2) FAPRA此名词第一出现时应有“ FAPRA(Flash-aware Page Replacement Algorithm)”说明. 2.in ...
- requests-html快速入门
昨天写了requests库好!最近requests库的作者又发布了一个新库,叫做requests-html,看名字也能猜出来,这是一个解析HTML的库,而且用起来和requests一样爽,下面就来介绍 ...
- 设计模式:模板方法(Template method)
首先我们先来看两个例子:冲咖啡和泡茶.冲咖啡和泡茶的基本流程如下: 所以用代码来创建如下: 咖啡:Caffee.java public class Coffee { void prepareRecip ...
- CF 1133C Balanced Team
题目链接:http://codeforces.com/problemset/problem/1133/C 题目分析 (个人感受:我看错了题目,硬是写了近一个小时!) 这个题目要求一个最长的序列,使得这 ...
- php aes加解密,mcrypt_encrypt 和openssl_encrypt
php7.1以下版本使用 /* * mcrypt_encrypt 加密 * php7.1开始被丢弃 可以使用openssl_encrypt * */ function aes_encrypt($con ...
- 从入门到自闭之Python闭包
闭包 定义:在嵌套函数内,使用(非本层变量)非全局变量就是闭包 闭包必须是内层函数对外层函数的变量(非全局变量)的引用 函数执行完毕后,函数体内的空间自行销毁 def func(): a=1 def ...