【问题标题】:request.getParameter don't have null values when they are expectedrequest.getParameter 在预期时没有空值
【发布时间】:2015-01-13 00:57:05
【问题描述】:

我发现这个问题非常令人困惑(但也很有趣),因此我想在这里向人们寻求见解。

我一直在自学 JSP 和相关技术。我要做的是从 JSP 中检索参数到 servlet,然后将它们与 If() 一起使用。 这是我的部分代码。

if ((request.getParameter("ID_A") != null || request.getParameter("Password_A") != null) &&
    (request.getParameter("ID_B") != null || request.getParameter("Password_B") != null)) {

        errorMessage = "Information is detected for the both side";
        request.setAttribute("errorMessage", errorMessage);
        request.getRequestDispatcher("4_enter_personal_info.jsp").forward(request, response);
    } // Other conditions...

这里是 JSP 的一部分(上一步)

<form action="PersonalInfor_to_confirmation_servlet" method="POST">
    <h2>For an existing customer</h2>
        <p>Customer ID</p>
        <input type="text" name="ID_A" value="" />
        <p>Password</p>
        <input type="text" name="Password_A" value="" />
        <br>
    <h2>For a new customer</h2>
        <p>Set your customer ID</p>
        <input type="text" name="ID_B" value="" />
        <p>Set your password</p>
        <input type="text" name="Password_B" value="" />
    //There are other lines
</form>

我正在努力确保,当客户端在双方(For an existing customer/For a new customer)输入信息时,JSP 上会出现上述消息"Information is detected for the both side"

但是,即使所有文本框都是空白的,也会出现错误消息。所以上面的所有request.getParameter( ) 方法都不包含空值,即使我将它们设为空。

即使我能想出其他算法,我也想知道这种现象发生的原因。

我们将不胜感激。

【问题讨论】:

标签: jsp servlets http-request-parameters


【解决方案1】:

if 块中的语句之所以运行,是因为该字段存在。如果您不想在文本框为空时运行它,您应该在条件中包含检查参数是否包含空字符串,例如:

request.getParameter("ID_A") != null && !request.getParameter("ID_A").isEmpty()
|| request.getParameter("Password_A") && !request.getParameter("Password_A").isEmpty()
...

所以上面所有的request.getParameter()方法都不包含null 值,即使我将它们设为空。

是的。当getParamater() 返回null 值时,意味着它在表单中找不到具有该名称的字段。使用另一种方法:isEmpty() 来检查字段是否为空。如:

request.getParameter("noSuchField")==null //true
request.getParameter("ID_A")==null //false
request.getParameter("ID_A").isEmpty() //true

【讨论】:

  • 非常感谢。这种方式看起来简洁,可能是最好的一种。所以,在 JSP 的世界里,null 不同于空……对吧?
  • @YMD,是的。 null 和空字符串完全不同。请记住,提交时表单中的每个字段都不是null,默认情况下它们的值为空字符串""。所以记得在你的验证中也包括isEmpty()
  • 谢谢教授...我以为a String variable of value "" 会被识别为null,但它不适用于JSP。非常感谢。
  • @YMD,但是空字符串和null在Java中也不一样。我很高兴我能提供帮助。只需阅读并尝试编码。你很快就会得到它。祝你好运! :)
【解决方案2】:
<input type="text" name="ID_A" value="" />

当您以这种方式提交表单时,您将拥有ID_A 作为键,""(空字符串)作为值。获得 null 的方法是根本不发送 ID_A 任何值。

解决这个问题的一个好方法是显式检查空字符串:

private boolean isNullOrBlank(final String s) {
    return s == null || s.trim().length() == 0;
}

if ((isNullOrBlank(request.getParameter("ID_A"))||
     isNullOrBlank(request.getParameter("Password_A"))
    ) &&
    (isNullOrBlank(request.getParameter("ID_B"))|| 
     isNullOrBlank(request.getParameter("Password_B")))) {
 ...
}

【讨论】:

    猜你喜欢
    • 2012-05-23
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2018-01-31
    • 2019-07-25
    相关资源
    最近更新 更多