URL parsing in Java

别来无恙 提交于 2019-12-18 06:18:05

问题


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


回答1:


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());



回答2:


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.




回答3:


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]



来源:https://stackoverflow.com/questions/21545912/url-parsing-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!