Java中的URL解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21545912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:18:05  来源:igfitidea点击:

URL parsing in Java

javaurlurl-parsing

提问by user2346047

I've got the URL like this:

我有这样的网址:

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

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

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

回答by sadhu

Use java.net.URLto parse a url string:

使用java.net.URL解析URL字符串:

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

回答by Thrash Bean

Inside your ActionClass

在你的 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.

是您的 servlet 请求,您可以从中获取所有参数。无论如何,看看ActionContext.getContext()。你可以从那里得到很多东西。希望这可以帮助。

回答by Delfic

Not really URL parsing but this would do your job:

不是真正的 URL 解析,但这可以完成您的工作:

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

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

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

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