谁来帮我做一道简单的编程题?(英文)(在线等)

qingyun1136 2006-07-06 01:23:16
Master a simple Java programming challenge (STATUS: NOT PASSED)

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, "cJGHAec" 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 class HandlerFactory
{
public String getHandler(String[] config, String requestUri)
{
//To do
;
}
}
...全文
218 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
seu_cose 2006-07-06
  • 打赏
  • 举报
回复
so long...
wangshenhai 2006-07-06
  • 打赏
  • 举报
回复
public String getHandler(String[] config, String requestUri){
String tmpstr = null;
String ru = requestUri;
int flag = 0;
int lastflag = 0;
int index = 0;
int i = 0;
for(i=0; i<config.length; i+=2){
flag = 0;
tmpstr = config[i];
for(int j=0; j<ru.length();j++){
if(tmpstr.length()>j){
if(tmpstr.substring(0,j+1).equalsIgnoreCase(ru.substring(0,j+1))){
flag++;
}
}
}
if(lastflag<flag){
lastflag = flag;
index = i;
}

}
if(lastflag==0)
return "cJGHAec";
return config[index+1];
}
你是不是就需要这样的效果啊?

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧