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.pswp.cn/news/397416.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397416.shtml
英文地址,請注明出處:http://en.pswp.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…