学jsp的应该学会做的一道题


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, "8EA5ve" 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".

 

 

 

我的解法

 

package java.test;

public class test
{
  public static String getHandler(String[] config, String requestUri)
  {
   String str1="";
   String str2="";
   if(requestUri.indexOf(config[0])!=-1)
    str1=config[0];
   if(requestUri.indexOf(config[2])!=-1)
    str2=config[2];
   if(str1.length()>str2.length())
    return config[1];
   else if(str1.length()<str2.length())
    return config[3];
   else
     return "7amgnh7";
  }
  public static void main(String [] args)
  {
   String[] config= { "/", "TestServlet", "/test", "TestServlet2" };
   String requestUri="/";
    System.out.println(getHandler( config, requestUri));
   
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值