------配置嵌入式servlet容器------

  springboot 默认使用的是嵌入的Servlet(tomcat)容器

  问题?

    1)如何定制修改Servlet容器的相关配置:

      1.修改和server有关的配置(ServerProperties配置文件设置)

// 通用的servlet 容器设置
server.xxxx
server.port=8080
server.servlet.context-path=/crud // tomcat 相关的设置
server.tomcat.xxx
server.tomcat.uri-encoding=UTF-8

      2. 编写一个EnbeddedServletContainerCustomizer:嵌入式servlet 容器的定制器来修改servlet容器默认配置

        在springboot中 会有很多的xxxCustomlzer 帮助我们进行定制配置

    2) 注册Servlet三大组件 【Servlet Filter Lisener

      由于springboot 默认是 以jar包的形式嵌入式的servlet容器启动web应用 没有web.xml 所以注册三大组件用一下方式

      

      1) 注册Servlet ServletRegistrationBean

public class MyServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello myservlet");
}
}
// 注册Servlet
@Bean
public ServletRegistrationBean myServlet() {
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(), "/MyServlet");
return registrationBean;
}

      2) 注册filter FilterRegistrationBean

public class MyFilter implements Filter{

    @Override
public void init(FilterConfig filterConfig) throws ServletException { } @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("MyFilter process");
filterChain.doFilter(servletRequest, servletResponse);
} @Override
public void destroy() { }
}
// 注册filter
@Bean
public FilterRegistrationBean myFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
   // 过滤什么请求
filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello", "/"));
return filterRegistrationBean;
}

      注册lisener ServletListenerRegistrationBean

public class MyLisener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("contextInitialized web 应用启动");
} @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("web 应用习销毁");
}
}
// 注册lisener
@Bean
public ServletListenerRegistrationBean myLisener() {
ServletListenerRegistrationBean<MyLisener> listenerRegistrationBean = new ServletListenerRegistrationBean<MyLisener>(new MyLisener());
return listenerRegistrationBean;
}

      例子:springboot帮助我们配置springmvc时候 自动注册spirngmvc 的前端控制器

      

    3) springboot 支持其他servlet容器

      tomcat

      jetty (长连接 聊天点对点长时间连接)

      Undertow (不支持JSP 并发性能好)

      默认使用tomcat 可以切换成其他容器 修改pom依赖 移除tomcat 模块

      之后启动就为Jetty 启动了

  嵌入式Servlet 容器优缺点:

    优点:简单 便携

    缺点:不支持jsp 优化定制比较复杂 (使用低定制器【ServerProperties, EnveddedServketCibtauberCystinuzer , 自己编写嵌入式Servlet 嵌入式工厂 EmbeddedServletContainerFactory 】)

  使用外置的servlet容器 :外面安装Tomcat- 应用使用War包方式打包

  新建项目 打包方式 选择War包 之前选择是jar包, 并且配置生成Webapp目录

  生成项目结构最后如下:

  配置外置tomcat 服务器

  启动服务器:

  创建 hello.jsp 以及success.jsp, helloController等  

    hello.jsp

<%--
Created by IntelliJ IDEA.
User: UPC
Date: 2018/5/6
Time: 19:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<a href="abc"></a>
</head>
<body> </body>
</html>

    HelloController

package com.lixuchun.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; @Controller
public class HelloController {
@GetMapping("/abc")
public String hello(Model model) {
model.addAttribute("message", "你好");
return "success";
} }

    success.jsp

<%--
Created by IntelliJ IDEA.
User: UPC
Date: 2018/5/6
Time: 19:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>success</title>
<h3>${message}</h3>
</head>
<body> </body>
</html>

    application.properties

spring.mvc.view.prefix=/WEB-INF
spring.mvc.view.suffix=.jsp

    访问效果:

    

    

  总结 如何使用外部容器:

    1) 创建一个war项目

    2)将嵌入式的Tomcat指定为 provided

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

    3)必须编写一个 springbootServletInitiallzer的子类,并且调用 configure方法

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
     // 传入springboot应用的主程序
return application.sources(SpringBoot04WebJspApplication.class);
} }

    4). 启动服务就可以使用了

spring boot 尚桂谷学习笔记07 嵌入式容器 ---Web的更多相关文章

  1. spring boot 尚桂谷学习笔记04 ---Web开始

    ------web开发------ 1.创建spring boot 应用 选中我们需要的模块 2.spring boot 已经默认将这些场景配置好了 @EnableAutoConfiguration ...

  2. spring boot 尚桂谷学习笔记10 数据访问02 mybatis

    数据访问 mybatis 创建一个 springboot 工程,模块选择 sql 中 mysql(数据驱动), jdbc(自动配置数据源), mybatis Web模块中选择 web pom 引入: ...

  3. spring boot 尚桂谷学习笔记11 数据访问03 JPA

    整合JPA SpringData 程序数据交互结构图 (springdata jpa 默认使用 hibernate 进行封装) 使用之后就关注于 SpringData 不用再花多经历关注具体各个交互框 ...

  4. spring boot 尚桂谷学习笔记09 数据访问

    springboot 与数据库访问 jdbc, mybatis, spring data jpa,  1.jdbc原生访问 新建项目 使用 springboot 快速构建工具 选中 web 组件 sq ...

  5. spring boot 尚桂谷学习笔记05 ---Web

    ------web 开发登录功能------ 修改login.html文件:注意加粗部分为 msg 字符串不为空时候 才进行显示 <!DOCTYPE html> <!-- saved ...

  6. spring boot 尚桂谷学习笔记08 Docker ---Web

    ------Docker------ 简介:Docker是一个开元的应用容器引擎,性能非常高 已经安装好的软件打包成一个镜像放到服务器中运行镜像 MySQL容器,Redis容器...... Docke ...

  7. spring boot 尚桂谷学习笔记06 异常处理 ---Web

    ------错误处理机制------ 默认效果 1 返回一个默认的错误页面 浏览器发送请求的请求头:优先接收 text/html 数据 客户端则默认响应json数据 : accept 没有说明返回什么 ...

  8. springboot 尚桂谷学习笔记03

    ------spring boot 与日志------ 日志框架: 市面上的日志框架: jul jcl jboss-logging logback log4j log4j2 ...... 左边一个门面 ...

  9. 初次搭建spring boot 项目(实验楼-学习笔记)

    首先说一下springboot 的优点: 使用Spring Initializr可以在几秒钟就配置好一个Spring Boot应用. 对大量的框架都可以无缝集成,基本不需要配置或者很少的配置就可以运行 ...

随机推荐

  1. MYSQL学习笔记/2019

    以下内容均为转载,只是方便今后学习:如有不妥,请联系删除:特此感谢原博主精心制作!(文章底部贴有原文链接) /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ m ...

  2. Java程序流程控制

    程序流程控制有 选择,循环,以及跳转结构 选择结构中无非就是 If 和 switch语句我两种都做了一些小案例 1. 利用Scanner库来获得控制台用户输入的数字与代码中定义的变量比较 packag ...

  3. P1613跑路

    题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的空间跑路器,每秒钟 ...

  4. c# 获取 Apk ,Aar 文件包名

    最近项目有个需求,需要拿到前端上传的Apk或者Aar文件里面的包名. 在这里贡献出来,方便有需求的小伙伴. 项目是 asp.net core 2.2 需要安装 nuget : AndroidXml S ...

  5. httpclient请求接口,上传文件附加参数(.net core)

    /// <summary> /// 上传文件 - 武汉站点 /// </summary> [HttpPost] public IActionResult UploadWH(Re ...

  6. vue + element 创建教程

    一.环境准备 node安装 node版本:v10.15.3 node下载地址:https://nodejs.org/zh-cn/ vue-cli安装 全局安装vue-cli npm install - ...

  7. 云中沙箱学习笔记1-快速部署并使用MySQL数据库

    1.1 背景知识 业务背景 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于Oracle旗下产品.MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面MyS ...

  8. BJSV-P-002高精度测速一体机

    测速.抓拍.录像于一体,产品处于行业顶尖水平. 1        测速一体机参数 2       接口和资源 3      相机接口 1.       前面板接口 测速一体机镜头接口采用C-Mount ...

  9. Linux修改密码指令

    1.在选择系统菜单界面,按 "e" 进入编辑模式 2.在以字符串“Linux16”开头的行,将光标移动到该行的结尾,然后输入“init=/bin/bash”,按 "Ctr ...

  10. python控制cpu使用率

    以下亲测可行. 使用方法:命令行模式 runing.py -c 2 -t 0.01 -c 指定cpu核数:不指定-c参数默认为所有核数. -t 数值越大,cpu使用率越低. runing.py &qu ...