Parsing URLs in Java

I have a url:

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

Is there any way to get only this part

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

+3
source share
3 answers

Use the java.net.URLURL strings for parsing:

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

Inside ActionClass

String actionName = (String)ActionContext.getContext().get(ActionContext.ACTION_NAME);

will give you a "test".

HttpServletRequest request = ServletActionContext.getRequest();

- your servlet request, from where you can get all the parameters. Anyway, take a look at ActionContext.getContext () . A lot of things you can get from there. Hope this helps.

+1
source

Not URL parsing at all, but this will do your job:

String[] parts = "http://test.com/testapp/test.dotest_id=1&test_name=SS".split("/");

return "/"+parts[parts.length-1]

0
source

All Articles