Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

java - Missing Elements in HTTP Request - Null or Empty?

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'.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If a parameter is not specified at all like so,

http://example.com/context/servlet?x=foo

then it will return 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"); // ""

Makes sense, right?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...