Java 如何获取包含参数的请求字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1578998/
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
How to get the request string including parameters
提问by sproketboy
I have an odd exception in our application and I'd like to log when it occurs and include the complete request string including the parameters.
我的应用程序中有一个奇怪的异常,我想记录它何时发生并包含完整的请求字符串,包括参数。
When I try
当我尝试
log.warn("Weird request " + request.getRequestURL());
I get the request string but not the parameters which were included with ? and &.
我得到了请求字符串,但没有得到包含在 ? 和 &。
example:
例子:
/testRequest.do?param1=1¶m2=2
I only see
我只看到
/testRequest.do
Can I get this whole string somewhere?
我可以在某个地方得到整个字符串吗?
采纳答案by Kevin
See HttpServletRequest#getQueryString()
参见HttpServletRequest#getQueryString()
If you want the whole string, you'll have to append the request url and the query string together as there is no method to get the whole thing.
如果您想要整个字符串,则必须将请求 url 和查询字符串附加在一起,因为没有方法可以获取整个字符串。
System.out.println(request.getRequestURL().append('?').append(request.getQueryString()));