日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tomcat 的 DefaultServlet

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tomcat 的 DefaultServlet 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題描述:

群里有人測試 Spring MVC,沒有配置任何Controller,只配置了一個view resolver,指定了前綴后綴。

然后,他問的是 當訪問 localhost:8080/test 的時候,為什么會被重定向到 localhost:8080/test/

?

由于是在Spring Boot群里問的,我想當然的認為 /test對應著Controller,于是百思不得其解。后來才知道不是Spring Boot項目,而是自己新建的一個maven項目,/test 對應著項目名稱。

直覺告訴我,這肯定是Tomcat的默認行為,因為項目名稱對應著文件夾,而 當訪問一個文件夾的時候,默認會查找其中的 index.html 等index文件,重定向完全講得通。

?

于是查找 tomcat(7.0.52)下面的配置文件,在 conf/web.xml 的末尾發現了這樣的內容:

<!-- ==================== Default Welcome File List ===================== --><!-- When a request URI refers to a directory, the default servlet looks --><!-- for a "welcome file" within that directory and, if present, to the --><!-- corresponding resource URI for display. --><!-- If no welcome files are present, the default servlet either serves a --><!-- directory listing (see default servlet configuration on how to --><!-- customize) or returns a 404 status, depending on the value of the --><!-- listings setting. --><!-- --><!-- If you define welcome files in your own application's web.xml --><!-- deployment descriptor, that list *replaces* the list configured --><!-- here, so be sure to include any of the default values that you wish --><!-- to use within your application. --><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file></welcome-file-list>

這里說的就是:當一個請求URI對應一個目錄時,default servlet會查找該目錄下的歡迎文件;如果存在,則返回資源;如果不存在,那default servlet 要么返回目錄列表(需要配置,默認不支持),要么返回 404 狀態碼。

?

但這里只是描述結果,并沒有原理性的東西,想要知道原理 只能去查源代碼。

根據上面的內容可以知道Tomcat 有一個default servlet,那我直接使用DefaultServlet查找相應的java文件(在IDEA中新建一個Spring Boot項目,再雙擊 Shift,輸入DefaultServlet即可),果然找到了。

嗯嗯,這里的Tomcat 是8.5.x,Spring Boot 默認的版本,但DefaultServlet的原理一致。

總結

以上是生活随笔為你收集整理的Tomcat 的 DefaultServlet的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。