15

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

user2346047
  • 213
  • 1
  • 2
  • 16

3 Answers3

30

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());
frogatto
  • 28,539
  • 11
  • 83
  • 129
sadhu
  • 1,429
  • 8
  • 14
1

Inside your ActionClass

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

will give you "test".

HttpServletRequest request = ServletActionContext.getRequest();

is your servlet request where from you can get all the parameters. Anyway, take a look at ActionContext.getContext(). Lot of thing you can get from there. Hope this helps.

Thrash Bean
  • 658
  • 4
  • 7
  • ActionContext is not a generic class. Please, avoid this answers type. – atrujillofalcon May 18 '16 at 08:48
  • @atrujillofalcon, I don't agree. But even so, after the critisism, can you propose something that do the job? Believe me, I know what I'm talking about - I had the same problem, very rare situation btw, in my real job and that was the best way to achieve it. – Thrash Bean May 19 '16 at 15:20
1

Not really URL parsing but this would do your job:

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

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

Delfic
  • 167
  • 2
  • 15