简体   繁体   English

JSP request.getParameter字符串

[英]JSP request.getParameter string

I have the following page in JSP: 我在JSP中有以下页面:

<%
    String req = request.getParameter("req");

    if (req == "Start")
        // Do something here
%>

<form>
    <input type="submit" name="req" value="Start" />
</form>

But when I press the Start button it seems that req value is not equal to Start , and the code does not enters the if statement. 但是,当我按下开始按钮时,看来req值不等于Start ,并且代码没有输入if语句。

What can be wrong? 有什么事吗

You should write 你应该写

 if (req != null && req.equals("Start"))

instead of 代替

 if (req == "Start")

Use if("Start".equals(request.getParameter("req"))) 使用if("Start".equals(request.getParameter("req")))

Even if request.getParameter("req") is null, you won't receive any errors. 即使request.getParameter(“ req”)为null,您也不会收到任何错误。

if (req == "Start") comparing the references of both object thats why you are not entering in if block. if (req == "Start")比较两个对象的引用,这就是为什么您不输入if块的原因。 Use String class equals() method which compare the values instead of reference. 使用String类equals()方法比较值而不是引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM