2006年10月8日星期日

www.myjavaserver.com的注册

免费的jsp空间,www.myjavaserver.com的觉得不错,但注册的时候,有个小测试,难度倒不大,不过是E文的,原文如下:

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, "264wK8R" must be returned. 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".

具体实现如下:

public String getHandler(String[] config, String requestUri) {
       String ret = "Ey1Der6" ;
       boolean found = false ;
       for (int i = 0 ; i < config.length ; i += 2){
      if (requestUri.indexOf(config[i]) == 0){
        if (!found){
       ret = config[i+1] ;
      found = true ;
     } else {
     if (ret.length() < config[i+1].length())
     ret = config[i+1] ;
}
}
}
return ret ;
}

1 条评论:

trustno1 说...

java编程初学者的示例代码
圆形边框上的文字