示例#1
0
  /**
   * http://example.com:8080/over/there?name=ferret#nose \__/ \______________/\_________/
   * \_________/ \__/ | | | | | scheme authority path query fragment
   *
   * <p>OR
   *
   * <p>[scheme:][//authority][path][?query][#fragment]
   *
   * @param request
   * @param response
   * @return
   * @throws ServletException
   * @throws IOException
   */
  Action dispatch(HttpServletRequest request) throws ServletException, IOException {
    String path = request.getRequestURI();
    String ctxP = request.getContextPath();

    if (ctxP.length() > 0) {
      path = path.substring(ctxP.length());
    }

    // set default character encoding to "utf-8" if encoding is not set:
    if (request.getCharacterEncoding() == null) {
      request.setCharacterEncoding("UTF-8");
    }

    String reqMethod = request.getMethod().toUpperCase();
    ActionConfig actionConfig = null;
    String[] urlArgs = null;

    /* 寻找处理请求的程序(方法) */
    for (UrlMatcher m : this.matchersMap.get(reqMethod)) {
      urlArgs = m.getUrlParameters(path);
      if (urlArgs != null) {
        actionConfig = urlMapMap.get(reqMethod).get(m);
        break;
      }
    }

    if (actionConfig != null) {
      return new Action(actionConfig, urlArgs, path);
    }

    return null;
  }
    private static boolean match(String patt, String s, Request req) {
      HashMap<String, String> m = new HashMap<String, String>();
      if (!UrlMatcher.match(patt, s, m)) return false;

      for (Map.Entry<String, String> e : m.entrySet()) req.set(e.getKey(), e.getValue());

      return true;
    }