(7)Spring Boot web开发 --- servlet容器
配置嵌入式 Servlet 容器
Spirng Boot 默认使用自带的嵌入式 Servlet 容器 ;
修改和定制默认的
Servlet容器的相关配置通过
application.properties/yml文件# 服务器基本配置,都是以 server 开头
server.port=8081 # 具体的 servlet 容器配置,写上具体的 servlet 名字,比如这里是 tomcat
server.tomcat.uri-encoding=utf-8
编写
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>()类这种方式是通过代码来设置:
/**
* 通用的管理
*
* @return
*/
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() { @Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(8089);
}
};
} /**
* 针对具体的 servlet ,这里是 tomcat
*
* @return
*/
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(9000);
factory.setUriEncoding();
return factory;
}关于
EmbeddedServletContainerCustomizer接口,我在我使用的Spring Boot(2.1.3)版本中,没有这个接口,我去官网文档看了下也没有相关的记录了,可能被废弃了;上面的例子也是在官网找到的,其中官网例子还有个东西,就是通过编码设置
session过期时间:

这句代码是通不过的,因此,设置
session过期时间,只能在配置文件里面配置 ;如果编码和配置文件,都配置了相同的属性,编码中的属性优先级高 ,其实应该是后读取的原因;
注册 三大组件
三大组件分别 Servlet、Filter、Listener ,分别使用 ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean 注册;
以 servlet 为例子:
首先写一个 Servlet ,可能有人已经忘记了,怎么写了,这是梦最初的地方啊,从一个简单的 servlet 学起;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("helle 啊啊啊");
}
}
然后注册,在配置类里面写:
/**
* 注册 servlet
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
// 接受两个参数,第一个是要映射的 servlet,第二个参数是变长数组,指定匹配哪些路径
return new ServletRegistrationBean(new MyServlet(), "/my","/sss");
}
两外两个大同小异;
Spring Boot 也自动的帮我们自动的注册了 SpringMvc 的前端控制器 DispatcherServlet ,默认的拦截规则是 :/ 拦截所有请求,包括静态页面,但是不拦截 jsp ,/* 才会拦截 jsp;
我们也可以在配置文件里面,进行更改默认的拦截规则 :server.servletPath ;
使用其他 servlet 容器
Jetty 适合做长连接,视频聊天这样的场景,Tomcat 不适合;而 Undertow 适合高并发,但是不支持 jsp ;
其中内中的 Servlet 容器都不支持 jsp ;
我们可以根据业务需要,选择哪一个容器,反正 Spring Boot 都支持 ;
切换很简单,只需要在 pom 文件中引入即可 ,同时排除 tomcat 的依赖,因为spring-boot-starter-web 场景启动器,默认使用就是 tomcat;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除 tomcat 依赖-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入 jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
排除完 tomcat 以后,要保证项目中没有和 tomcat 有关的东西了,一般应该都没有,除了自己配置服务器的应用,配置了 tomcat 的配置:
/**
* 针对具体的 servlet ,这里是 tomcat
*
* @return
*/
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(9000);
return factory;
}
Spring Boot 默认的 Servlet 容器是 Tomcat,启动可以看到:

切换成 jetty 以后,启动可以看到:

切换 Undertow 一样的操作;
使用外置的 Servlet 容器
内置的 servlet 用起来,确实爽,内置一时爽,一直内置一直爽,但是它也有一个致命的缺点,就是不支持 JSP;
但是项目有时候,确实有 jsp 存在啊,因此,有时候,需要使用外置的 Servlet 容器,在项目创建之初就要选择好,还记得第一篇中讲到的快速构建项目,在第二步的时候,需要改动下:

然后一路继续创建项目;
创建好的项目的pom文件和使用 jar 打包的,有些许不同:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
...
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 打包方式-->
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
...
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- 内置的tomcat 被排除-->
<scope>provided</scope>
</dependency>
...
...
</dependencies>
....
,,,,
</project>
然后项目结构需要添加点东西进来:
首先 IDEA 添加 tomcat ,是个 javaweb 码农都会的操作,略过;
然后按照下面一通瞎造作:

这时候,自动生成的 WEB-INF 目录位置不对,需要自己拖拽下,成品如下:

还多了一个类,这个类,不要去碰,是保正项目正确运行的保障:

然后运行:

OK,完事!
(7)Spring Boot web开发 --- servlet容器的更多相关文章
- Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件
1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor ...
- Springboot 系列(七)Spring Boot web 开发之异常错误处理机制剖析
前言 相信大家在刚开始体验 Springboot 的时候一定会经常碰到这个页面,也就是访问一个不存在的页面的默认返回页面. 如果是其他客户端请求,如接口测试工具,会默认返回JSON数据. { &quo ...
- Springboot 系列(五)Spring Boot web 开发之静态资源和模版引擎
前言 Spring Boot 天生的适合 web 应用开发,它可以快速的嵌入 Tomcat, Jetty 或 Netty 用于包含一个 HTTP 服务器.且开发十分简单,只需要引入 web 开发所需的 ...
- 17. Spring Boot 配置嵌入式Servlet容器
一.如何定制和修改Servlet容器的相关配置 1.配置文件(ServerProperties): 优先级最高 server.port=8081 server.context‐path=/crud s ...
- Spring Boot Web 开发注解篇
本文提纲 1. spring-boot-starter-web 依赖概述 1.1 spring-boot-starter-web 职责 1.2 spring-boot-starter-web 依赖关系 ...
- 四、Spring Boot Web开发
四.Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可 ...
- Spring Boot嵌入式的Servlet容器
一.查看SpringBoot默认的嵌入式Servlet容器(默认使用的是tomcat) 在IDEA的项目的pom文件中按Ctrl + shift + Alt + U可以打开SpringBoot依赖的图 ...
- 4.Spring Boot web开发
1.创建一个web模块 (1).创建SpringBoot应用,选中我们需要的模块: (2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来 (3).自己编 ...
- (5)Spring Boot web开发 --- Restful CRUD
文章目录 `@RestController` vs `@Controller` 默认访问首页 设置项目名 国际化 登陆 & 拦截 Restful 风格 @RestController vs @ ...
随机推荐
- phpMyadmin各个版本漏洞【转载】
原作者:热爱网络安全的小菜狗 原文链接:phpMyadmin各版本漏洞 0x01 PREGREPLACEEVAL漏洞 影响版本:3.5.x < 3.5.8.1 and 4.0.0 < 4. ...
- OpenCV reshape The Matrix is not continuous, thus its number of rows can not be changed
When using OpenCV reshape and gets this error: OpenCV Error: Image step is wrong (The matrix is not ...
- Python自动化测试常用库
基本库: sys 程序和Python解析器的交互 os 启动新进程:操作文件和目录 re 正则表达式,字符串匹配 string 基本字符串操作 inspect 提供自省和反射功能 importlib ...
- cookie 的HttpOnly 和 Secure 属性
设置HttpOnly=true的cookie不能被js获取到,无法用document.cookie打出cookie的内容. Secure属性是说如果一个cookie被设置了Secure=true,那么 ...
- NPAPI插件开发详细记录:用VS2010开发NPAPI插件步骤<转>
原帖地址:https://blog.csdn.net/z6482/article/details/7660748 ------------------------------------------- ...
- longitudinal models | 纵向研究 | mixed model
A longitudinal study refers to an investigation where participant outcomes and possibly treatments o ...
- Robot Framework安装使用
关于robotframework环境搭建安装请参考 另外一篇博文:Robot Framework的环境搭建(就是一些库文件的安装) 项目基本流程: 1.创建项目New Project----选择dir ...
- MiniUI treeGrid 树节点展开和不展开的性能差别很大
参考API: http://miniui.com/docs/api/index.html#ui=datagrid http://miniui.com/docs/api/index.html#ui=tr ...
- Vehicle routing with Optaplanner graph-theory
Vehicle routing with Optaplanner - Stack Overflow https://stackoverflow.com/questions/22285252/vehic ...
- ubuntu 关于curses头文件问题
执行编译gcc -o badterm badterm.c -lcurses后报错情报如下:term.h: 没有那个文件或目录curses.h: 没有那个文件或目录很明显,程序找不到term.h和cur ...