打开javablog的时候发现了一则申请JSP空间的消息,地址是http://www.myjavaserver.com/signup
打开后发现这个申请过程有点意思,要先写一段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 湘江夜游神 阅读(277) 评论(1)  编辑  收藏 所属分类: JAVA生活

FeedBack:
# re: 申请了一个JSP空间
2007-11-09 13:58 | 湘江夜游神
自己占个沙发  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

Locations of visitors to this page