HTTP请求中缺少元素 - 空或空? [英] Missing Elements in HTTP Request - Null or Empty?
问题描述
我有一个HTTP请求 javax.servlet.http.HttpServletRequest
传入一个值,用于使用JAX在Java Web服务中处理的某些代码中RS。 Java中的POST函数正在消耗 application / json
。有两个可能的值要传递给请求,调用一个 X
,另一个 Y
,假设两者都是字符串。该请求要求至少两个可能的值中的一个被视为有效。
I have a HTTP Request javax.servlet.http.HttpServletRequest
that is passing in a value to be used in some code being handled in a Java web service using JAX-RS. The POST function in Java is consuming application/json
. There are two possible values to be passed into the request, call one X
and the other Y
, assume both are Strings. The request requires at least one of the two possible values to be considered 'valid'.
当请求进入时,如果<$ c提供了$ c> X , Y
完全被排除在请求之外,检查<$ c $是否正确的方法是什么c> Y 有吗?你会检查 Y.isEmpty()
或 Y == null
?提供 X
并不保证 Y
存在,反之亦然。
When the request comes in, if X
is provided and Y
is left out of the request entirely, what is the proper way to check to see if Y
is there? Would you check to see if Y.isEmpty()
or Y == null
? Providing X
doesn't guarantee Y
is present, and vice versa.
推荐答案
如果没有像这样指定参数,
If a parameter is not specified at all like so,
http://example.com/context/servlet?x=foo
然后它将返回 null
:
String x = request.getParameter("x"); // "foo"
String y = request.getParameter("y"); // null
如果指定了参数,但没有这样的值,
If a parameter is specified, but does not have a value like so,
http://example.com/context/servlet?x=foo&y
然后它将返回一个空字符串:
then it will return an empty string:
String x = request.getParameter("x"); // "foo"
String y = request.getParameter("y"); // ""
有道理,对吗?
这篇关于HTTP请求中缺少元素 - 空或空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!