打開javablog的時(shí)候發(fā)現(xiàn)了一則申請JSP空間的消息,地址是http://www.myjavaserver.com/signup
          打開后發(fā)現(xiàn)這個(gè)申請過程有點(diǎn)意思,要先寫一段java代碼完成題目。
          這個(gè)題目的大概意思是:一個(gè)String類型數(shù)組,填充的內(nèi)容是一組映射關(guān)系,一個(gè)String作為校驗(yàn)String數(shù)組的依據(jù)(英文太爛不知道對不對)。
          它給了一個(gè)例子

          Example input:

            String[] config: { "/", "MainServlet", "/nav", "NavigationServlet" }
            String requestUri: "/nav/test"

            Correct result: "NavigationServlet"

          In this example, the configuration contains a mapping of "/" to "MainServlet" and "/nav" to "NavigationServlet". In the case of an incoming URI "/nav/test.nav", "NavigationServlet" is the correct choice because its pattern is longer than that of "MainServlet".
          這段話的意思是"/", "MainServlet"是映射關(guān)系,"/nav", "NavigationServlet"同上,那么requestUri: "/nav/test",則是對"/", "/nav"的評判。當(dāng)滿足條件后選擇較長的字符串作為結(jié)果返回。
          咋一看這題目意思挺明確的,不就是輸出長的字符串嗎?Coding

          public class HandlerFactory
          {
            
          public String getHandler(String[] config, String requestUri)
            
          {

          String str 
          = ""
          String result 
          = "4c1OHY"
          for (int i = 0; i < config.length; i += 2
          if (config[i].length() >= str.length()) 
          str 
          = config[i]; 
          System.out.println(str);
          result 
          = config[i + 1]; 
          }
            
          }

          return result; 
          }

          }

          結(jié)果一貼上就提示error,問題出在了哪里?原來是沒有看完題目。

          As the principal engineer of an HTTP web server, you are responsible for implementing the request processing subsystem of the server.
          An incoming request for a specific resource, identified by an URI, must be dispatched to the appropriate handler according to the server configuration which maps URIs to request handlers. 'HandlerFactory.getHandler' must be implemented:

          public class HandlerFactory
          {
            public String getHandler(String[] config, String requestUri)
            {
            }
          }

          The string array 'config' contains URI patterns and handler names. Two consecutive values form a key-value pair comprised of URI pattern and handler. 'requestUri' represents an incoming request, the URI to match against the configured handlers. 'getHandler' must return the correct handler for a given URI as a string value.

          An URI pattern never contains wildcards and represents the start of an URI string, a prefix. Matching must be implemented accordingly. The handler with the longest matching URI pattern wins if more than one pattern matches. If no handler can be found, "UlFuW0" must be returned.

          關(guān)鍵是這一段交代了這個(gè)方法的意圖,條件是這樣的一個(gè)順序,String數(shù)組中充當(dāng)key值與requestUri的關(guān)系是,key作為前綴給requestUri校驗(yàn)的。只有當(dāng)校驗(yàn)通過時(shí),value才能作為結(jié)果返回,當(dāng)多個(gè)key值通過校驗(yàn)后,返回length最大的value。
          于是加了個(gè)條件。Line:9

           1public class HandlerFactory
           2{
           3  public String getHandler(String[] config, String requestUri)
           4  {
           5
           6String str = ""
           7String result = "4c1OHY"
           8for (int i = 0; i < config.length; i += 2
           9if (requestUri.startsWith(config[i])){
          10if (config[i].length() >= str.length()) 
          11str = config[i]; 
          12System.out.println(str);
          13result = config[i + 1]; 
          14}
           
          15}
           
          16}

          17return result; 
          18}

          19}
          編譯通過。
          當(dāng)然這個(gè)是最初級的代碼,如果考慮的更加細(xì)致一點(diǎn)。代碼可以更加完善。
          posted on 2007-11-09 13:58 湘江夜游神 閱讀(287) 評論(1)  編輯  收藏 所屬分類: JAVA生活

          FeedBack:
          # re: 申請了一個(gè)JSP空間
          2007-11-09 13:58 | 湘江夜游神
          自己占個(gè)沙發(fā)  回復(fù)  更多評論
            

          Locations of visitors to this page

          主站蜘蛛池模板: 定安县| 新密市| 保定市| 仁怀市| 巨野县| 清苑县| 光泽县| 金溪县| 裕民县| 上饶市| 会同县| 苗栗市| 阜阳市| 连城县| 秦皇岛市| 栾城县| 沙雅县| 昔阳县| 任丘市| 治多县| 精河县| 合山市| 公主岭市| 阿荣旗| 尼勒克县| 内乡县| 页游| 隆安县| 顺义区| 安义县| 永平县| 凤台县| 白玉县| 阳高县| 梅河口市| 开封县| 宜阳县| 邵阳市| 西充县| 石泉县| 连南|