大家帮我看看,怎么解决这个问题
有一个Bean:
public class RBean {
private String password= " ";
private String repassword " ";
public String getPassword(){
return password;
}
public String getRepassword(){
return repassword;
}
public void setPassword(String password){
this.password=password;
}
public void setRepassword(String repassword){
this.repassword=repassword;
}
}
在提交password和repassword参数之后转到下面的jsp输出页面:
<jsp:useBean class= "Voting.RBean " id= "rbean " scope= "request ">
<jsp:setProperty name= "rbean " property= "* " />
</jsp:useBean>
<%
String g1=rbean.getPassword();
String g2=rbean.getRepassword();
if(g1.trim()==g2.trim()){
out.println( "ok ");
}else{
out.println( "error ");
}
%>
当我输入的password的值和repassword的值相等时,为什么jsp输出页面总是显示“error”呢?
------解决方案--------------------==是表示对象的引用,equals()表示比较对象的值,改成equals()就可以了,
------解决方案--------------------用equals()比较吧 建议有时间看下THINKING IN JAVA
------解决方案--------------------楼上的~~~~
------解决方案--------------------== 是比较2个字符串的地址
.equals()是比较2个字符串的值的
------解决方案--------------------if( g1.equal(g2) ){
out.println( "ok ");
}else{
out.println( "error ");
}