[英]If else statements inside jsp
I have a following problem - if else statements do not work in JSP, and to be honest I have no idea why. 我有一个以下问题-如果else语句在JSP中不起作用,说实话我不知道为什么。 Basically I try to change the placeName
depending on what number is stored in a string called place. 基本上,我尝试根据在名为place的字符串中存储的数字来更改placeName
。 After printing the values in the browser I can see the value is not changed. 在浏览器中打印值之后,我可以看到该值未更改。 I am sure it is something simple but... Maybe some one had similar problem before? 我相信这很简单,但是...也许有人以前有过类似的问题?
<%
//requests the strings sent by previous page
String year = request.getParameter("year");
String place = request.getParameter("place");
out.print(year);
out.print(place);
String year2 = request.getParameter("year2");
String place2 = request.getParameter("place2");
//out.print(year2);
//out.print(place2);
if (place == "1")
{
placeName = "Belmullet";
}
else if (place == "2")
{
placeName = "Birr";
}
...more statements here...
else if (place == "15")
{
placeName = "Shannon airport";
};
%>
change the if condition: 更改if条件:
if (place == "1") {
}
by 通过
if ("1".equals(place)) {
}
and the same way for the other if conditions. 其他条件相同。
This SO question may helps you to learn the difference between ==
and equals()
. 这样的问题可以帮助您了解==
和equals()
之间的区别。
It's because you're comparing Strings using ==. 这是因为您正在使用==比较字符串。 Instead, use the .equals()
method. 而是使用.equals()
方法。
The == operator tests to see if two object references refer to the exact same instance of an object. ==运算符进行测试以查看两个对象引用是否引用对象的完全相同的实例。
The .equals() tests to see if the two objects being compared to each other are equivalent. .equals()测试以查看被比较的两个对象是否相等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.