JavaWeb静态资源访问流程

1. url-patten 缺省值

想要了解JavaWeb静态资源访问流程,首先要先了解“url-patten 缺省值”的使用

当浏览器发送一个http请求时,tomcat通过请求中URL的路径,从应用中web.xml中查找“url-patten”对应的Servlet,实现对应Servlet的响应。

而有一种特殊的url-patten,即

/

当在web.xml中找不到对应的url-patten时,如果存在上面这样的url-patten 缺省值,则会找到其对应的servlet名字,从而找到对应的Servlet。



  test20201005
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
  	mytest1
  	com.test.servlet.myservelet1
  
  
  
  	mytest1
  	/myservelet1
  

  
  	mytest2
  	com.test.servlet.myservelet2
  
  
  
  	mytest2
  	/
  

如web.xml的内容如上,当tomcat找不到该应用中对应的url-pattern时,则会根据url-pattern的缺省值设置,找到com.test.servlet.myservelet2这个servlet(类)。

2. 静态资源访问流程

了解完上面的url-pattern后。当访问一个静态资源(如http://localhost:8080/test20201003/index.html)时,也会当应用中的web.xml中查找对应的servlet(示例链接对应的url-pattern为/index.html),如果找不到,则会查找url-pattern的缺省值,如果该应用中web.xml没有配置url-pattern的缺省值,则会到全局的web.xml(conf/web.xml)查找有没有该servlet,如果在全局的web.xml中查找不到该servlet,则会在全局的web.xml中查找url-pattern的缺省值,而全局的web.xml中配置了url-pattern的缺省值如下:


    default
    /

对应的servlet为


    default
    org.apache.catalina.servlets.DefaultServlet
    
        debug
        0
    
    
        listings
        false
    
    1

org.apache.catalina.servlets.DefaultServlet这个类会逐行读取静态资源文件(如示例链接中的index.html)并响应给浏览器,浏览器进行显示。

 

你可能感兴趣的:(JavaWeb,JavaWeb,静态资源,访问流程,原理)