小弟刚学了JSP,想做网络聊天室,可是有个郁闷的equals一直没有解决,两天了,郁闷,请大哥们帮我看看.
工程目录:D:/eclipse/workspace/Takeing      Takeing:是工程名
jsp文件在Takeing里面.
Bean在:D:/eclipse/workspace/Takeing/WEB-INF/classes/Bean里面。请看下面的BEAN和.JSP
package Bean;
import java.sql.*;public class Login {
String logname,password,success="false",message;
    Connection con=null;
    Statement stat=null;
    ResultSet rs;
    public Login(){
     try{
     Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
     }catch(ClassNotFoundException e){
     System.out.println("加载数据库驱动程序有问题,请检查!--in Login");
     }
    }
    public void setLogname(String log){
     logname=log;
    }
    public String getLogname(){
     return logname;
    }
    public void setPassword(String ps){
     password=ps;
    }
    public String getPassword(){
     return password;
    }
    public String getSuccess(){
     return success;
    }
    public String getMessage(){
     try{
     con=DriverManager.getConnection("jdbc:microsoft:sqlserver://mazhiyong:1433;DatabaseName=friend","sa","sa");
         stat=con.createStatement();
     String sql="select * from member where logname="+"'"+logname+"'";
         rs=stat.executeQuery(sql);
     int rowcount=0;
     String ps=new String();
         while(rs.next()){
     rowcount++;
     logname=rs.getString("logname");
     ps=rs.getString("password");
     System.out.println(rowcount);
     }
     boolean bo=password.equals(ps);        //下面的输出语句测试出输入的密码和数据库里面的密码
     System.out.println(bo);                //bo的值总是false,但不输入密码就是true
     System.out.println(password.equals(ps));
         System.out.println("password="+password);
     System.out.println("ps="+ps);
     if((rowcount==1)&&(password.equals(ps))){
     System.out.println("option ok!");
     success="ok";
     message="ok";
     }
     else{
     message="输入的用户名或密码不对!";
     success="false";
     }
     con.close();
     return message;
     }catch(SQLException e){
     message="输入的用户名或密码不对!---in catch ";
     success="false";
     return message;
     }
    }
}login.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="Bean.Login"%> <%!
  public String codeString(String s){
     String str=s; 
     try{
     byte b[]=str.getBytes("ISO-8859-1");
     str=new String(b);
     return str;
     }catch(Exception e){
       System.out.println("不能转换!");
       return str;
     }
   } 
 %>
<html>
<body bgcolor=yellow><font size=5>
<%@ include file="head.txt"%>
<p>
请输入你的用户名和密码:
<%String str=response.encodeURL("login.jsp");%>
<FORM action="<%=str%>" Method="post">
<br>登陆名<input type="text" name="logname" value="登陆名">
<br>密码<input type="password" name="password">
<p>
<input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value="重置">
</form>
 <jsp:useBean id="login" scope="session" class="Bean.Login"/>
 <%
   String logname="",password="",message="";
   if(!(session.isNew())){
      
      logname=request.getParameter("logname");//提取用户名,并转为字符
      if(logname==null) logname="";
      logname=codeString(logname);
      out.print("会员名:");//
      out.print(logname+"<br>");
      
      password=request.getParameter("password"); 
      if(password==null) password="";
      password=codeString(password);
       out.print("密码:");
       out.print(password+"<br>");
   }
 %>
 <%
  if(!(logname.equals(""))){
  
 %>
  <jsp:setProperty  name= "login"  property="logname"  value="<%=logname%>" />
  <jsp:setProperty  name= "login"  property="password"  value="<%=password%>" />
 <%
   message=login.getMessage();
   out.println("getmessage");
   if(message==null) {message="";}
   out.print(message);
  }
 %>
 <%
  if(!(session.isNew())){
   out.println("gonging11111111");
   if(message.equals("ok")){
     out.print("登陆成功!转到showMember页面");
     String meb=response.encodeURL("showMember.jsp");
     response.sendRedirect(meb);
   }
   else {
    out.println("message:");
    out.println(message+"null");
    out.println("over message!");
    }
  }
 %>
</font></body>
</html>

解决方案 »

  1.   

    在页面中的空格经常造成一些问题。你多用一下trim()函数
      

  2.   

    String sql="select * from member where logname="+"'"+logname+"'";-->看一下是否有写错;
      boolean bo=password.equals(ps);-->这里的equals换为 == ,试一下看看先!
      

  3.   

    password通过javaBean负值同样可以得到所输入的密码的啊!
      

  4.   

    没记错的话password.equals(ps);是password和ps的内永。而password==ps应该是指引的比较