URL parsing in Java

user2346047 picture user2346047 · Feb 4, 2014 · Viewed 38.3k times · Source

I've got the URL like this:

http://test.com/testapp/test.do?test_id=1&test_name=SS

Is there any way we can get only this part

/test.do?test_id=1&test_name=SS

Answer

sadhu picture sadhu · Feb 4, 2014

Use java.net.URL to parse a url string:

URL url = new URL("http://test.com/testapp/test.do?test_id=1&test_name=SS");
System.out.println(url.getPath()+"?"+url.getQuery());