servlet對映路徑匹配解析

2022-08-10 21:02:25

開頭

servlet是javaweb用來處理請求和響應的重要物件,本文將從原始碼的角度分析tomcat內部是如何根據請求路徑匹配得到處理請求的servlet的

假設有一個request請求路徑為/text/servlet/get,並且在web.xml中設定了4個servlet,程式碼如下,那麼該請求呼叫的是哪一個servlet呢?

<servlet>
    <servlet-name>servlet01</servlet-name>
    <servlet-class>com.monian.study.servlet.Servlet01</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servlet01</servlet-name>
    <url-pattern>/test/servlet/get</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>servlet02</servlet-name>
    <servlet-class>com.monian.study.servlet.Servlet02</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servlet02</servlet-name>
    <url-pattern>/test/servlet/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>servlet03</servlet-name>
    <servlet-class>com.monian.study.servlet.Servlet03</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servlet03</servlet-name>
    <url-pattern>/test/*</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>servlet04</servlet-name>
    <servlet-class>com.monian.study.servlet.Servlet04</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servlet04</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
    <servlet>
    <servlet-name>servlet05</servlet-name>
    <servlet-class>com.monian.study.servlet.Servlet05</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servlet05</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

相應各個servlet的程式碼,程式碼很簡單,呼叫哪一個servlet就輸出哪個servlet的名稱:

servlet程式碼
public class Servlet01 extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write("Servlet01");
  }
}


public class Servlet02 extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write("Servlet02");
  }
}

public class Servlet03 extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write("Servlet03");
  }
}

public class Servlet04 extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write("Servlet04");
  }
}

public class Servlet05 extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write("Servlet05");
  }

}

 

原始碼

org.apache.catalina.mapper.Mapper#internalMapWrapper
// 在本例子中 path = '/zxq/test/servlet/get',用offset和end來控制路徑部分長度
// contextPath = '/zxq'
private final void internalMapWrapper(ContextVersion contextVersion,
    CharChunk path,
    MappingData mappingData) throws IOException {

  int pathOffset = path.getOffset();
  int pathEnd = path.getEnd();
  boolean noServletPath = false;
  
  // contextVersion.path = '/zxq'
  int length = contextVersion.path.length();
  if (length == (pathEnd - pathOffset)) {
    noServletPath = true;
  }
  int servletPath = pathOffset + length;
  // path = '/text/servlet/get'
  path.setOffset(servletPath);

  // 規則1:先開始精確匹配
  MappedWrapper[] exactWrappers = contextVersion.exactWrappers;
  internalMapExactWrapper(exactWrappers, path, mappingData);

  // 規則2:字首匹配,也就是路徑匹配
  boolean checkJspWelcomeFiles = false;
  MappedWrapper[] wildcardWrappers = contextVersion.wildcardWrappers;
  if (mappingData.wrapper == null) {
    internalMapWildcardWrapper(wildcardWrappers, contextVersion.nesting,
        path, mappingData);
    if (mappingData.wrapper != null && mappingData.jspWildCard) {
      char[] buf = path.getBuffer();
      if (buf[pathEnd - 1] == '/') {
        /*
         * Path ending in '/' was mapped to JSP servlet based on
         * wildcard match (e.g., as specified in url-pattern of a
         * jsp-property-group.
         * Force the context's welcome files, which are interpreted
         * as JSP files (since they match the url-pattern), to be
         * considered. See Bugzilla 27664.
         */
        mappingData.wrapper = null;
        checkJspWelcomeFiles = true;
      } else {
        // See Bugzilla 27704
        mappingData.wrapperPath.setChars(buf, path.getStart(),
            path.getLength());
        mappingData.pathInfo.recycle();
      }
    }
  }

  if(mappingData.wrapper == null && noServletPath &&
      contextVersion.object.getMapperContextRootRedirectEnabled()) {
    // The path is empty, redirect to "/"
    path.append('/');
    pathEnd = path.getEnd();
    mappingData.redirectPath.setChars
        (path.getBuffer(), pathOffset, pathEnd - pathOffset);
    path.setEnd(pathEnd - 1);
    return;
  }

  // Rule 3 -- Extension Match
  MappedWrapper[] extensionWrappers = contextVersion.extensionWrappers;
  if (mappingData.wrapper == null && !checkJspWelcomeFiles) {
    internalMapExtensionWrapper(extensionWrappers, path, mappingData,
        true);
  }

  // Rule 4 -- Welcome resources processing for servlets
  if (mappingData.wrapper == null) {
    boolean checkWelcomeFiles = checkJspWelcomeFiles;
    if (!checkWelcomeFiles) {
      char[] buf = path.getBuffer();
      checkWelcomeFiles = (buf[pathEnd - 1] == '/');
    }
    if (checkWelcomeFiles) {
      for (int i = 0; (i < contextVersion.welcomeResources.length)
          && (mappingData.wrapper == null); i++) {
        path.setOffset(pathOffset);
        path.setEnd(pathEnd);
        path.append(contextVersion.welcomeResources[i], 0,
            contextVersion.welcomeResources[i].length());
        path.setOffset(servletPath);

        // Rule 4a -- Welcome resources processing for exact macth
        internalMapExactWrapper(exactWrappers, path, mappingData);

        // Rule 4b -- Welcome resources processing for prefix match
        if (mappingData.wrapper == null) {
          internalMapWildcardWrapper
              (wildcardWrappers, contextVersion.nesting,
                  path, mappingData);
        }

        // Rule 4c -- Welcome resources processing
        //            for physical folder
        if (mappingData.wrapper == null
            && contextVersion.resources != null) {
          String pathStr = path.toString();
          WebResource file =
              contextVersion.resources.getResource(pathStr);
          if (file != null && file.isFile()) {
            internalMapExtensionWrapper(extensionWrappers, path,
                mappingData, true);
            if (mappingData.wrapper == null
                && contextVersion.defaultWrapper != null) {
              mappingData.wrapper =
                  contextVersion.defaultWrapper.object;
              mappingData.requestPath.setChars
                  (path.getBuffer(), path.getStart(),
                      path.getLength());
              mappingData.wrapperPath.setChars
                  (path.getBuffer(), path.getStart(),
                      path.getLength());
              mappingData.requestPath.setString(pathStr);
              mappingData.wrapperPath.setString(pathStr);
            }
          }
        }
      }

      path.setOffset(servletPath);
      path.setEnd(pathEnd);
    }

  }

  /* welcome file processing - take 2
   * Now that we have looked for welcome files with a physical
   * backing, now look for an extension mapping listed
   * but may not have a physical backing to it. This is for
   * the case of index.jsf, index.do, etc.
   * A watered down version of rule 4
   */
  if (mappingData.wrapper == null) {
    boolean checkWelcomeFiles = checkJspWelcomeFiles;
    if (!checkWelcomeFiles) {
      char[] buf = path.getBuffer();
      checkWelcomeFiles = (buf[pathEnd - 1] == '/');
    }
    if (checkWelcomeFiles) {
      for (int i = 0; (i < contextVersion.welcomeResources.length)
          && (mappingData.wrapper == null); i++) {
        path.setOffset(pathOffset);
        path.setEnd(pathEnd);
        path.append(contextVersion.welcomeResources[i], 0,
            contextVersion.welcomeResources[i].length());
        path.setOffset(servletPath);
        internalMapExtensionWrapper(extensionWrappers, path,
            mappingData, false);
      }

      path.setOffset(servletPath);
      path.setEnd(pathEnd);
    }
  }


  // Rule 7 -- Default servlet
  if (mappingData.wrapper == null && !checkJspWelcomeFiles) {
    if (contextVersion.defaultWrapper != null) {
      mappingData.wrapper = contextVersion.defaultWrapper.object;
      mappingData.requestPath.setChars
          (path.getBuffer(), path.getStart(), path.getLength());
      mappingData.wrapperPath.setChars
          (path.getBuffer(), path.getStart(), path.getLength());
      mappingData.matchType = MappingMatch.DEFAULT;
    }
    // Redirection to a folder
    char[] buf = path.getBuffer();
    if (contextVersion.resources != null && buf[pathEnd -1 ] != '/') {
      String pathStr = path.toString();
      // Note: Check redirect first to save unnecessary getResource()
      //       call. See BZ 62968.
      if (contextVersion.object.getMapperDirectoryRedirectEnabled()) {
        WebResource file;
        // Handle context root
        if (pathStr.length() == 0) {
          file = contextVersion.resources.getResource("/");
        } else {
          file = contextVersion.resources.getResource(pathStr);
        }
        if (file != null && file.isDirectory()) {
          // Note: this mutates the path: do not do any processing
          // after this (since we set the redirectPath, there
          // shouldn't be any)
          path.setOffset(pathOffset);
          path.append('/');
          mappingData.redirectPath.setChars
              (path.getBuffer(), path.getStart(), path.getLength());
        } else {
          mappingData.requestPath.setString(pathStr);
          mappingData.wrapperPath.setString(pathStr);
        }
      } else {
        mappingData.requestPath.setString(pathStr);
        mappingData.wrapperPath.setString(pathStr);
      }
    }
  }

  path.setOffset(pathOffset);
  path.setEnd(pathEnd);
}

 

匹配路徑程式碼

org.apache.catalina.mapper.Mapper#find(org.apache.catalina.mapper.Mapper.MapElement[], org.apache.tomcat.util.buf.CharChunk, int, int)
// 從map找到一個最與路徑匹配的
private static final <T> int find(MapElement<T>[] map, CharChunk name,
    int start, int end) {

  int a = 0;
  int b = map.length - 1;

  // Special cases: -1 and 0
  if (b == -1) {
    return -1;
  }

  
  // -1表示完全不匹配,直接返回 
  if (compare(name, start, end, map[0].name) < 0 ) {
    return -1;
  }
  // 完全匹配或部分匹配,且只有一個待匹配的servlet直接返回
  if (b == 0) {
    return 0;
  }

  // 類似於二分查詢,找到一個最長路徑匹配 
  int i = 0;
  while (true) {
    i = (b + a) >>> 1;
    int result = compare(name, start, end, map[i].name);
    if (result == 1) {
      a = i;
    } else if (result == 0) {
      return i;
    } else {
      b = i;
    }
    if ((b - a) == 1) {
      int result2 = compare(name, start, end, map[b].name);
      if (result2 < 0) {
        return a;
      } else {
        return b;
      }
    }
  }

}



private static final int compare(CharChunk name, int start, int end,
    String compareTo) {
  int result = 0;
  char[] c = name.getBuffer();
  int len = compareTo.length();
  if ((end - start) < len) {
    len = end - start;
  }
  // 比較url-pattern與 請求路徑path,若有一個字元不相等退出迴圈 
  for (int i = 0; (i < len) && (result == 0); i++) {
    if (c[i + start] > compareTo.charAt(i)) {
      result = 1;
    } else if (c[i + start] < compareTo.charAt(i)) {
      result = -1;
    }
  }
    
  // 都相等的話再比較長度,請求路徑長度比待匹配部分長
  if (result == 0) {
    if (compareTo.length() > (end - start)) {
      result = -1;
    } else if (compareTo.length() < (end - start)) {
      result = 1;
    }
  }
  // result=0代表完全匹配, result=-1代表不匹配,result=1代表開頭部分匹配 
  return result;
}

針對上述的匹配舉個例子,假設有兩個servlet都是萬用字元匹配的,url-pattern為 /test/one/* 和/test/* ,tomcat解析的時候會去掉萬用字元再排序['/test', 'test/one'],之後再去匹配資料中的元素也就是map[i].name,匹配路徑 '/test/one/two'會返回url-parttern=/test/one/* 的這個servlet,這就是最長路徑匹配

 

精確匹配

可以看到符合精確匹配的只有servlet01,且name就是它設定的url-pattern值,然後與requestPath進行匹配 

private final void internalMapExactWrapper
    (MappedWrapper[] wrappers, CharChunk path, MappingData mappingData) {
  // 找到一個與path精確匹配的wrapper
  MappedWrapper wrapper = exactFind(wrappers, path);
  if (wrapper != null) {
    mappingData.requestPath.setString(wrapper.name);
    mappingData.wrapper = wrapper.object;
    if (path.equals("/")) {
      // Special handling for Context Root mapped servlet
      mappingData.pathInfo.setString("/");
      mappingData.wrapperPath.setString("");
      // This seems wrong but it is what the spec says...
      mappingData.contextPath.setString("");
      mappingData.matchType = MappingMatch.CONTEXT_ROOT;
    } else {
      mappingData.wrapperPath.setString(wrapper.name);
      mappingData.matchType = MappingMatch.EXACT;
    }
  }
}


private static final <T, E extends MapElement<T>> E exactFind(E[] map,
    CharChunk name) {
  // find方法會返回部分匹配或完全匹配的map
  int pos = find(map, name);
  if (pos >= 0) {
    E result = map[pos];
    // 完全匹配
    if (name.equals(result.name)) {
      return result;
    }
  }
  return null;
}

顯而易見的開頭那個request與servlet01的url-pattern是精確匹配的

 

萬用字元匹配 (路徑匹配)

接下來web.xml去掉servlet01的設定,只剩下4個servlet,從前面來看,精確匹配肯定是失敗的因為現在去掉servlet01已經沒有符合要求的servlet去精確匹配了,只能進行路徑匹配了,而路徑匹配符合要求的有兩個servlet

/**
 * Wildcard mapping.
 */
private final void internalMapWildcardWrapper
(MappedWrapper[] wrappers, int nesting, CharChunk path,
    MappingData mappingData) {

  int pathEnd = path.getEnd();

  int lastSlash = -1;
  int length = -1;
  // 找一個最匹配path路徑的,根據上面的匹配程式碼可以得到servlet02
  int pos = find(wrappers, path);
  if (pos != -1) {
    boolean found = false;
    while (pos >= 0) {
      if (path.startsWith(wrappers[pos].name)) {
        length = wrappers[pos].name.length();
        if (path.getLength() == length) {
          found = true;
          break;
        // path不以/開頭,則重新找 
        } else if (path.startsWithIgnoreCase("/", length)) {
          found = true;
          break;
        }
      }
      // 獲取path最後一個/ 所在的位置
      if (lastSlash == -1) {
        lastSlash = nthSlash(path, nesting + 1);
      } else {
        lastSlash = lastSlash(path);
      }
      path.setEnd(lastSlash);
      pos = find(wrappers, path);
    }
    path.setEnd(pathEnd);
    if (found) {
      mappingData.wrapperPath.setString(wrappers[pos].name);
      if (path.getLength() > length) {
        mappingData.pathInfo.setChars
            (path.getBuffer(),
                path.getOffset() + length,
                path.getLength() - length);
      }
      mappingData.requestPath.setChars
          (path.getBuffer(), path.getOffset(), path.getLength());
      mappingData.wrapper = wrappers[pos].object;
      mappingData.jspWildCard = wrappers[pos].jspWildCard;
      mappingData.matchType = MappingMatch.PATH;
    }
  }
}

因此servlet02是匹配的,輸出

若再web.xml去掉servlet02,那麼匹配的就是servlet03了

另外我們可以從上面的程式碼得到若請求路徑path = '/test/servlet/get', 則 '/*' 、 '/test/*' 、 '/test/servlet/*' 、 '/test/servlet/get/*' 與之匹配,'/test/serv/*' 這種不匹配 

路徑匹配是能匹配請求路徑以 .jsp 、.html結尾的request的

 

擴充套件名匹配(字尾匹配)

web.xml中註釋servlet02和servlet03後,再次存取.jsp字尾結尾的請求就會直接報404了,可以看後續的匹配邏輯雖然能匹配到處理.jsp的servlet但我們並沒有在相應路徑下設定jsp檔案,那麼自然報404錯誤了 

 

下圖可以看到字尾匹配的servlet有三個,一個我們自定義的字尾為do,另外兩個jsp和jspx是tomcat內建的預設處理jsp的servlet

 

/**
 * Extension mappings.
 *
 * @param wrappers          Set of wrappers to check for matches
 * @param path              Path to map
 * @param mappingData       Mapping data for result
 * @param resourceExpected  Is this mapping expecting to find a resource
 */
private final void internalMapExtensionWrapper(MappedWrapper[] wrappers,
    CharChunk path, MappingData mappingData, boolean resourceExpected) {
  char[] buf = path.getBuffer();
  int pathEnd = path.getEnd();
  int servletPath = path.getOffset();
  int slash = -1;
  for (int i = pathEnd - 1; i >= servletPath; i--) {
    if (buf[i] == '/') {
      slash = i;
      break;
    }
  }
  if (slash >= 0) {
    int period = -1;
    for (int i = pathEnd - 1; i > slash; i--) {
      if (buf[i] == '.') {
        period = i;
        break;
      }
    }
    if (period >= 0) {
      // 擷取到字尾的字元位置 匹配
      path.setOffset(period + 1);
      path.setEnd(pathEnd);
      MappedWrapper wrapper = exactFind(wrappers, path);
      if (wrapper != null
          && (resourceExpected || !wrapper.resourceOnly)) {
        mappingData.wrapperPath.setChars(buf, servletPath, pathEnd
            - servletPath);
        mappingData.requestPath.setChars(buf, servletPath, pathEnd
            - servletPath);
        mappingData.wrapper = wrapper.object;
        mappingData.matchType = MappingMatch.EXTENSION;
      }
      path.setOffset(servletPath);
      path.setEnd(pathEnd);
    }
  }
}

根據find的匹配邏輯可以匹配到我們自定義的servlet05,輸出 

 

首頁welcome資源匹配  

若上述匹配都失敗了則嘗試尋找預設的資原始檔,預設有三個,也可以自定義設定 

假設請求路徑為http://localhost:8082/zxq/ 以'/'結尾,那麼會嘗試將檔名加到path後面,以index.jsp為例,加完後路徑為'/zxq/index.jsp',之後會以此新路徑再去嘗試精確匹配、路徑匹配、物理檔案查詢再進行擴充套件名匹配順序查詢,直到找到能處理此path的servlet 

在webapp目錄下加一個index.jsp檔案之後能成功存取到,執行此請求的就是tomcat預設的jsp servlet

 

預設匹配

<url-pattern>/</url-pattern> '/'就是預設匹配,當上述匹配都失敗的時候,則啟用這個servlet,也就是本文中的servlet04