[十二]SpringBoot 之 servlet
Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。
当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。
Spring boot 的主 Servlet 为DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?
在spring boot中添加自己的Servlet有两种方法,代码注册Servlet和注解自动注册(Filter和Listener也是如此)。
一、代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和ServletListenerRegistrationBean 获得控制。
也可以通过实现 ServletContextInitializer 接口直接注册。
二、在SpringBootApplication 上使用@ServletComponentScan注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。
1.通过代码注册Servlet示例代码:
package me.shijunjie.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class MyServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<"); doPost(req, resp); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>HelloWorld</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>这是:MyServlet1</h1>"); out.println("</body>"); out.println("</html>"); }
}
package me.shijunjie.testspringboot2; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; import me.shijunjie.servlet.MyServlet1; @SpringBootApplication
public class App
{ /**
* 注册Servlet.不需要添加注解:@ServletComponentScan
* @return
*/
@Bean
public ServletRegistrationBean myServlet1(){ return new ServletRegistrationBean(new MyServlet1(),"/myServlet/*");
} public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
测试
2.使用注解注册Servlet示例代码 添加@WebServlet注解
package me.shijunjie.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns="/myServlet2/*",description="Servlet的说明")
public class MyServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");
doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>HelloWorld</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>这是:MyServlet2</h1>"); out.println("</body>"); out.println("</html>"); }
}
添加@ServletComponentScan注解
package me.shijunjie.testspringboot2; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; /**
* Hello world!
*
*/
@SpringBootApplication
@ServletComponentScan("me.shijunjie.servlet")
public class App
{ /* *//**
* 注册Servlet.不需要添加注解:@ServletComponentScan
* @return
*//*
@Bean
public ServletRegistrationBean myServlet1(){ return new ServletRegistrationBean(new MyServlet1(),"/myServlet/*");
}*/ public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
测试
[十二]SpringBoot 之 servlet的更多相关文章
- 十二.spring-boot使用spring-boot-freemarker
①.在springMVC中:它代表着view层组件 ②.为什么使用freemarker:简单容易学.逻辑分明 ③.freemarker优点:它不依赖servlet.网络或者web环境 一.创建一个ma ...
- springboot(十二) SpringBoot 性能优化
代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo springboot优化主要有三类优化:1.包扫描优化 2. ...
- java痛苦学习之路[十二]JSON+ajax+Servlet JSON数据转换和传递
1.首先client须要引入 jquery-1.11.1.js 2.其次javawebproject里面须要引入jar包 [commons-beanutils-1.8.0.jar.commons-c ...
- springboot(二十二)spring-boot使用AOP
https://blog.csdn.net/w05980598/article/details/79053209
- SpringBoot第十二集:度量指标监控与异步调用(2020最新最易懂)
SpringBoot第十二集:度量指标监控与异步调用(2020最新最易懂) Spring Boot Actuator是spring boot项目一个监控模块,提供了很多原生的端点,包含了对应用系统的自 ...
- 【使用篇二】SpringBoot整合Servlet(1)
两种方式: 通过注解扫描完成 Servlet组件的注册 通过方法完成 Servlet组件的注册 一.通过注解扫描完成 Servlet 组件的注册 1. 编写Servlet类 /** * SpringB ...
- (二十二)SpringBoot之使用Druid连接池以及SQL监控和spring监控
一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...
- SpringBoot系列(十二)过滤器配置详解
SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...
- springboot源码解析-管中窥豹系列之BeanPostProcessor(十二)
一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...
随机推荐
- 北京Uber优步司机奖励政策(4月12日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- [SDOI2011]染色 树链剖分
LG传送门 我写这道题的题解主要是因为洛谷上的题解要么讲的不清要么代码丑滑稽,导致初学时的我调了很久,所以想发个题解方便后来人. 由于要维护的信息还是具有区间可加性,只需要记录一下每个区间的左右端点颜 ...
- 你不需要jQuery You Don't Need jQuery
转载:https://github.com/oneuijs/You-Dont-Need-jQuery/blob/master/README.zh-CN.md You Don't Need jQuery ...
- Spring中的TransactionProxyFactoryBean作用及配置(转)
问: 原文链接 http://blog.csdn.net/cpp_lzth/article/details/6551703 看AOP的时候发现spring中有个org.springframework. ...
- TeamViewer卡在正在初始化显示参数
在windows的mstsc远程桌面中打开teamviewer,远程桌面开着的时候可以连接teamviewer,但是当我断开mstsc之后,再用teamviewer连就连接不上了,一直都是正在初始化显 ...
- xshell连接虚拟机linux系统失败问题
问题:在xshell新建对话弹出的对话框中输入ip地址后,确定并没有弹出输入用户名和密码对话框 直接显示连接失败 Could not connect to ): Connection failed. ...
- python3 selenium实现自动登陆网页
一. 安装python3与pycharm python安装参考链接:https://www.cnblogs.com/hepeilinnow/p/9727922.html pycharm最好安装专业版 ...
- OAI搭建总结
我是参考网上的方法:oai搭建之eNB的文章, 接下来就根据自身所遇到的问题再这里总结一下步骤: 一.再官网上下载oai的文件openairinterface5g-master.zip 二.编译的过程 ...
- WebGL树形结构的模型渲染流程
今天和大家分享的是webgl渲染树形结构的流程.用过threejs,babylonjs的同学都知道,一个大模型都是由n个子模型拼装而成的,那么如何依次渲染子模型,以及渲染每个子模型在原生webgl中的 ...
- spark重点知识
1 scala 2 spark rdd 3 sprak core 4 性能优化 5 spark sql 6 spark streaming 掌握程度-精通的理解: