public class Navigation { static String[] config= { "/test2", "TestServlet2", "/test", "TestServlet" }; static String requestUri= "/test2/"; public static void main(String[] args) { System.out.println("output" +new Navigation().getHandler(config, requestUri)); } public String getHandler(String[] config, String requestUri) { System.out.println("Request uri : "+requestUri); int maxMatch=0; //store the length of the match that is the longest. String currentMatch=null; //the most accurate match string for (int i=0;imaxMatch){ //compare the length of the //previously matched String, if any maxMatch=config[i].length(); //if so, store it System.out.println("max match... "+config[i+1]); currentMatch=config[i+1]; //Dont forget the String too } } } if (currentMatch!=null){ return currentMatch; //return it. } return "/"; } }