Spring Boot Servlet

上一篇我们对如何创建Controller 来响应JSON 以及如何显示数据到页面中,已经有了初步的了解。 
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 注解自动注册,无需其他代码。

通过代码注册Servlet示例代码:

SpringBootSampleApplication.Java

package org.springboot.sample;import org.springboot.sample.servlet.MyServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.DispatcherServlet; @SpringBootApplication public class SpringBootSampleApplication { /** * 使用代码注册Servlet(不需要@ServletComponentScan注解) * * @return * @author SHANHY * @create 2016年1月6日 */ @Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new MyServlet(), "/xs/*");// ServletName默认值为首字母小写,即myServlet } public static void main(String[] args) { SpringApplication.run(SpringBootSampleApplication.class, args); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

MyServlet.java

package org.springboot.sample.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; /** * Servlet * * @author 单红宇(365384722) * @myblog http://blog.csdn.net/catoop/ * @create 2016年1月6日 */ //@WebServlet(urlPatterns="/xs/*", description="Servlet的说明") public class MyServlet extends HttpServlet{ private static final long serialVersionUID = -8685285401859800066L; @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>Hello World</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>大家好,我的名字叫Servlet</h1>"); out.println("</body>"); out.println("</html>"); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

使用注解注册Servlet示例代码

SpringBootSampleApplication.java

package org.springboot.sample;import org.springboot.sample.servlet.MyServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.DispatcherServlet; @SpringBootApplication @ServletComponentScan public class SpringBootSampleApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSampleApplication.class, args); } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

MyServlet2.java

package org.springboot.sample.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; /** * Servlet * * @author 单红宇(365384722) * @myblog http://blog.csdn.net/catoop/ * @create 2016年1月6日 */ @WebServlet(urlPatterns="/xs/myservlet", description="Servlet的说明") // 不指定name的情况下,name默认值为类全路径,即org.springboot.sample.servlet.MyServlet2 public class MyServlet2 extends HttpServlet{ private static final long serialVersionUID = -8685285401859800066L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doGet2()<<<<<<<<<<<"); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doPost2()<<<<<<<<<<<"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hello World</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>大家好,我的名字叫Servlet2</h1>"); out.println("</body>"); out.println("</html>"); } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

使用 @WebServlet 注解,其中可以设置一些属性。

有个问题:DispatcherServlet 默认拦截“/”,MyServlet 拦截“/xs/*”,MyServlet2 拦截“/xs/myservlet”,那么在我们访问 http://localhost:8080/xs/myservlet 的时候系统会怎么处理呢?如果访问 http://localhost:8080/xs/abc 的时候又是什么结果呢?这里就不给大家卖关子了,其结果是“匹配的优先级是从精确到模糊,复合条件的Servlet并不会都执行”

既然系统DispatcherServlet 默认拦截“/”,那么我们是否能做修改呢,答案是肯定的,我们在SpringBootSampleApplication中添加代码:

    /*** 修改DispatcherServlet默认配置** @param dispatcherServlet* @return* @author SHANHY* @create 2016年1月6日 */ @Bean public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) { ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet); registration.getUrlMappings().clear(); registration.addUrlMappings("*.do"); registration.addUrlMappings("*.json"); return registration; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

当然,这里可以对DispatcherServlet做很多修改,并非只是UrlMappings。

http://blog.csdn.net/catoop/article/details/50501686

 

转载于:https://www.cnblogs.com/softidea/p/6065394.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/397416.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于相关性分析系统性能瓶颈

测试的过程中&#xff0c;难免需要会遇到一些性能瓶颈&#xff0c;那么就要求我们能够分析出性能瓶颈&#xff0c;并给出解决方案。性能瓶颈很抽象&#xff0c;我们可以通过数据使其具象。以我工作内容为例&#xff0c;服务器处理数据的能力是有限的&#xff0c;那么其处理的边…

curl网站开发指南

curl网站开发指南 作者&#xff1a; 阮一峰 日期&#xff1a; 2011年9月 4日 我一向以为&#xff0c;curl只是一个编程用的函数库。 最近才发现&#xff0c;这个命令本身&#xff0c;就是一个无比有用的网站开发工具&#xff0c;请看我整理的它的用法。 curl网站开发指南 阮一…

android格式化时间中文版,Android 仿微信聊天时间格式化显示功能

本文给大家分享android仿微信聊天时间格式化显示功能。在同一年的显示规则&#xff1a;如果是当天显示格式为 HH:mm 例&#xff1a;14:45如果是昨天,显示格式为 昨天 HH:mm 例&#xff1a;昨天 13:12如果是在同一周 显示格式为 周一 HH:mm 例&#xff1a;周一14:05如果不是同一…

java分享第十七天-01(封装操作xml类)

做自动化测试的人&#xff0c;都应该对XPATH很熟悉了&#xff0c;但是在用JAVA解析XML时&#xff0c;我们通常是一层层的遍历进去&#xff0c;这样的代码的局限性很大&#xff0c;也不方便&#xff0c;于是我们结合一下XPATH&#xff0c;来解决这个问题。所需要的JAR包&#xf…

Ubuntu12.04 内核树建立

先查看自己使用的内核版本 linlin-virtual-machine:~$ uname -r 3.2.0-23-generic 如果安装系统时&#xff0c;自动安装了源码。在 /usr/src 目录下有对应的使用的版本目录。 linlin-virtual-machine:~$ cd /usr/src linlin-virtual-machine:/usr/src$ ls linux-headers-3.2.0…

【mysql】Innodb三大特性之double write

1、doublewrite buffer&#xff08;mysql官方的介绍&#xff09; InnoDB uses a novel file flush technique called doublewrite. Before writing pages to the data files, InnoDB first writes them to a contiguous area called the doublewrite buffer. Only after the wr…

android crop 大图,com.android.camera.action.CROP 实现图片剪裁

APP 中选取图片之后&#xff0c;有时候需要进行剪裁&#xff0c;比如头像。以下是启动代码。在我的项目中&#xff0c;传的是 filePath&#xff0c;所以我转了一下&#xff0c;但实际上从相册选择图片后&#xff0c;用 data.getData() 就可获得 uri。Uri uri Uri.fromFile(new…

Who Gets the Most Candies? POJ - 2886 (线段树)

按顺时针给出n个小孩&#xff0c;n个小孩每个人都有一个纸&#xff0c;然后每个人都有一个val&#xff0c;这个val等于自己的因子数&#xff0c;如果这个val是正的&#xff0c;那就顺时针的第val个孩子出去&#xff0c;如果是负的话&#xff0c;就逆时针的第val个孩子出去&…

javax.validation.ValidationException: Unable to find a default provider

2019独角兽企业重金招聘Python工程师标准>>> [ERROR] [2016-11-16 13:58:21 602] [main] (FrameworkServlet.java:457) Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name org.springframewo…

第十章练习题----2

package com.Hanqi2;public class xitizhuhanshu {public static void main(String[] args) {// TODO Auto-generated method stubxiti tm new xiti("黑色","15寸");xitizhs tm3 new xitizhs("蓝色","15寸");tm.Call("654"…

关于微信“被返回页”在被返回时自动刷新

网上有很多这些文章&#xff0c;但我觉得没一篇真正解决这个问题&#xff0c;倒是能给人一个解决方案的思路&#xff0c;对&#xff0c;就是posState事件。 要解决这个问题也不难&#xff0c;使用history的replaceState属性替换当前网页链接&#xff08;其实作用是在不增加hist…

android视频播放器api,03.视频播放器Api说明

03.视频播放器Api说明目录介绍01.最简单的播放02.如何切换视频内核03.切换视频模式04.切换视频清晰度05.视频播放监听06.列表中播放处理07.悬浮窗口播放08.其他重要功能Api09.播放多个视频10.VideoPlayer相关Api11.Controller相关Api12.边播放边缓存api13.类似抖音视频预加载14…

使用Python重命名MP3标签

从Window复制MP3文件的到Ubuntu下&#xff0c;MP3标签很多是乱码。于是想自己写个Python程序处理一下。 从酷狗复制过来的音乐文件名都是“作者 - 标题”&#xff0c;所以可以通过解析文件名直接获取作者和标题信息。 需要下载eyeD3模块 $ sudo apt-get install python-eyed3 代…

Taurus.MVC 2.0 开源发布:WebAPI开发教程

背景&#xff1a; 有用户反映&#xff0c;Tausus.MVC 能写WebAPI么&#xff1f; 能&#xff01; 教程呢&#xff1f; 嗯&#xff0c;木有&#xff01; 好吧&#xff0c;刚好2.0出来&#xff0c;就带上WEBAPI教程了&#xff01; 开源地址&#xff1a; https://github.com/cyq116…

android 锁屏 home,android 锁屏界面禁用长按home 和menu(recent apps)

android 5.1 系统中public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {//检查当前是否锁屏&#xff0c; 可以添加getTopApp()判断当前activity 来屏蔽2398 final boolean keyguardOn keyguardOn();添加新的方法&#xff1a;//获…

Chrome浏览器调试踩坑

Chrome浏览器若在响应式状态下&#xff0c;页面缩放比例不是100%&#xff0c;元素会“窜位”&#xff0c;点击元素会点击到元素周围的元素 Chrome页面缩放比例不为100%时&#xff0c;table的单元格就算没有边框&#xff08;CSS去掉了&#xff09;也会显示出边框&#xff08;缝隙…

WordPress 博客文章时间格式the_time()设置

国外设计的WordPress 主题里的文章的时间格式是类似“十一月 21, 2010”这种格式的&#xff0c;而中国人习惯的是年在前&#xff0c;月紧跟其后&#xff0c;日在末尾&#xff0c;所以看国外的就显得很别扭&#xff0c;但是我们可以通过修改WP时间代码来设置成为我们中国人习惯的…

linux yum

更改linux YUM源方法&#xff1a;第一步&#xff1a;进入yum配置文件目录&#xff1a;cd /etc/yum.repos.d/第二步&#xff1a;备份配置文件&#xff1a;mv CentOS-Base.repo CentOS-Base.repo.bak第三步&#xff1a;下载网易的配置&#xff08;或其他源配置文件&#xff09;&a…

chrome瀏覽器去掉緩存的方法

方法一&#xff1a; 1.開發說打開開發者工具 勾選這個訪問可以 方法二: commandshiftR 转载于:https://www.cnblogs.com/kaibindirver/p/9378572.html

Apache Tomcat目录下各个文件夹的作用

1.bin&#xff1a;存放各种不同平台开启与关闭Tomcat的脚本文件。 2.lib&#xff1a;存tomcat与web应用的Jar包。 3.conf&#xff1a;存放tomcat的配置文件。 4.webapps&#xff1a;web应用的发布目录。 5.work&#xff1a;tomcat把由各种jsp生成的servlet文件存放的地方。 6.l…