Missing Elements in HTTP Request - Null or Empty?

前端 未结 2 2045
北恋
北恋 2020-12-19 07:38

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.

相关标签:
2条回答
  • 2020-12-19 08:03

    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?

    0 讨论(0)
  • 2020-12-19 08:10

    Tests if a parameter is present in the request

    httpServletRequest.getParameter( "Y" ) == null
    

    The following code tests the value of the parameter if it is present

    if ( httpServletRequest.getParameter( "Y" ) != null )
    {
        String value = httpServletRequest.getParameter( "Y" );
    
        // Put your test code here.  Include a empty value check
    }
    
    0 讨论(0)
提交回复
热议问题