jsp中 的if else 怎么对啊

<%Stringname=(String)request.getParameter("name");Stringpassword1=(String)request.get... <%
String name = (String) request.getParameter("name");
String password1 = (String) request.getParameter("password1");
String password2 = (String) request.getParameter("password2");

if (password1 == password2) {
Manger manger = new Manger();
User u = new User();
u.setName(name);
u.setPassword(password1);
if (manger.repeat(u)) {
response.sendRedirect("Fail.jsp");
} else {
manger.save(u);
response.sendRedirect("Success.jsp");
}
}

else {

out.print("ddd");
}
%>
理论应该是如果密码不相同才打印“ddd”.为什么 密码相同时仍然打印“ddd”啊
而且 不执行 密码相同的那些语句,
不管什么情况都只执行else里面的语句
展开
 我来答
slaris
2011-06-02 · TA获得超过259个赞
知道答主
回答量:115
采纳率:0%
帮助的人:149万
展开全部
问题出在了这一句:
if (password1 == password2)
原因分析:
1)String password1和password2直接比较是在判断其在内存中的引用(地址位置),所以如果想要两个非基础类型变量相等需要有以下条件:
String pw1 = new String();
String pw2 = pw1; // 现在pw1 == pw2,可以参考 jse docs java.lang.object.equals()
2)如果想进行两个非基础类型变量进行其value的比较,需要实现以下方法:
public boolean Object.equals(Object obj);
jse docs中的描述是(英文,要是嫌烦可以先跳到中文。这里简单描述了该方法具有自反
性、对称性、传递性。其实可以联想数学概念理解其含义):
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:
It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
也就是说,Object间进行values是否相等的判断需要使用equals(Object obj)方法,
String.equals(Object obj)已经为JSE overwrite。所以你在此进行相等判断应该使用如下方法:
if ( pw1.equals ( pw2 ) ) { } ; // 参见 jse docs java.lang.String
3)如果需要实现对象间的比较(大小、相等),除了要重写equals()方法外,还要实现Comparable<T>接口,并实现其中的int compareTo(T obj)方法。具体请参见jse docs中的Comparable.compareTo(T o)描述。比如可以进行比较的Date等都是实现了该接口,如果你设计的Type类型实现进行排序等要求,毋庸置疑也需要实现该接口及该方法。

参考资料: http://download.oracle.com/javase/6/docs/api/

百度网友b9a0abd
2015-11-12 · 超过14用户采纳过TA的回答
知道答主
回答量:78
采纳率:0%
帮助的人:31.4万
展开全部
引入

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

接着这样

<c:choose>
<c:when test="${requestScope.newFlag== '1' || requestScope.newFlag== '2' ||requestScope.newFlag== '3'}">
作品名称: ${star.class_}<br />
作品编号: ${star.raceNum}<br />
</c:when>
<c:otherwise>
班级: ${star.class_}<br />
参赛编号: ${star.raceNum}<br />
</c:otherwise>
</c:choose>

或者

<%!
//中间直接写java代码

%>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
coolrapist
2011-06-02 · TA获得超过590个赞
知道小有建树答主
回答量:196
采纳率:0%
帮助的人:110万
展开全部
比较字符串应该用equals方法,不能用==,==比较的是“是否在同一位置上”,因为拷贝的方式不同,就算是相同的字符串也有可能不在同一位置上,应该用equals方法,这个比较的是字符串的值是否相同,即 password1.equals(password2)如果相同返回true
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zcz3141
2011-06-02 · TA获得超过303个赞
知道小有建树答主
回答量:350
采纳率:0%
帮助的人:126万
展开全部
这种情况应该是 你的password1 和 password2 就从来没相等过.
你把 password1 == password2 改成 password1.equals(password2) 试试
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
MWKO
2011-06-02 · 超过17用户采纳过TA的回答
知道答主
回答量:150
采纳率:0%
帮助的人:75.9万
展开全部
你输出两个密码看看他们相同不相同啊!如果不相等,那就很好理解了;如果确定是相同的,那你就用password1.equals(password2)试试!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式