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

          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"是映射關系,"/nav", "NavigationServlet"同上,那么requestUri: "/nav/test",則是對"/", "/nav"的評判。當滿足條件后選擇較長的字符串作為結果返回。
          咋一看這題目意思挺明確的,不就是輸出長的字符串嗎?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; 
          }

          }

          結果一貼上就提示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.

          關鍵是這一段交代了這個方法的意圖,條件是這樣的一個順序,String數組中充當key值與requestUri的關系是,key作為前綴給requestUri校驗的。只有當校驗通過時,value才能作為結果返回,當多個key值通過校驗后,返回length最大的value。
          于是加了個條件。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}
          編譯通過。
          當然這個是最初級的代碼,如果考慮的更加細致一點。代碼可以更加完善。
          posted on 2007-11-09 13:58 湘江夜游神 閱讀(284) 評論(1)  編輯  收藏 所屬分類: JAVA生活

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

          Locations of visitors to this page

          主站蜘蛛池模板: 长阳| 德化县| 宣化县| 漳浦县| 乌兰察布市| 梅州市| 乌拉特中旗| 岚皋县| 鸡东县| 梧州市| 綦江县| 桐梓县| 曲水县| 葫芦岛市| 华安县| 吕梁市| 双城市| 怀柔区| 兴安县| 惠州市| 盱眙县| 无极县| 廉江市| 天台县| 鄂温| 顺义区| 新乡市| 镇沅| 清新县| 阿瓦提县| 灌阳县| 沙河市| 永城市| 华坪县| 锡林浩特市| 安乡县| 沁源县| 武平县| 溧阳市| 蛟河市| 海淀区|