奇怪的java 奇怪的字符问题
代码如下
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = new User();
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String sName = request.getParameter("username");
String sPassword = request.getParameter("password");
String sRepassword = request.getParameter("repassword");
String sSex = "1";
String snickName = request.getParameter("nickname");
if(sName==null){
//姓名不能为空
out.println("<h3>姓名不能为空</h3>");
return;
}
if(sPassword==null){
//密码不能为空
out.println("<h3>密码不能为空</h3>");
return;
}
if(sRepassword==null){
//确认密码不能空
out.println("<h3>确认密码不能空</h3>");
return;
}
/*if(sSex==null){
//性别不能为空
return;
}*/
if(sPassword!=sRepassword){
//两次输入密码不一致
out.println("<h3>两次输入密码不一致</h3>");
return;
}
if(user.checkUser(sName))
{
//用户名已经被占用
out.println("<h3>用户名已经被占用</h3>");
return;
}
if(user.doUserRegister(sName, sPassword, sSex, snickName)){
//注册成功!
out.println("<h3>注册成功!</h3>");
}
else{
//注册失败!
out.println("<h3>注册失败!</h3>");
}
}
静态页面就不用写了传递的参数也都传递过来了 ,现在是我传的是两个密码
一个是Password=1&rpassword=1
可是在判断的时候sPassword!=sRepassword返回值是true 好不明白为什么呢? java
[解决办法]
判断是否相等的时候用
!sPassword.equal(sRepassword)
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
public class StringTests {
public static void main(String[] args) {
String a = "abc";
String b = new String("abc");
String c = new String("abc").intern();
System.out.println(a == b);
System.out.println(a == c);
}
}