下面是我写的用来验证登陆的用户名密码是否正确的代码:<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>验证用户登录信息</title>
    </head>
    <body>    <h1>验证用户登录信息</h1>
    
   <jsp:useBean id="Mybean" scope="session" class="org.me.hello.NameHandler" />
        
        <jsp:setProperty name="Mybean" property="*" />
       <%
           String uid=null,upwd=null;
           
           uid = request.getParameter("username").toString();
           upwd = request.getParameter("userpwd").toString();
        
            out.write(uid);
            out.write(upwd);
   
           if(uid == "zhx".toString() && upwd == "1")
            { 
               response.sendRedirect("LogInSuccess.jsp");
            }
            else 
            {
             
            }    
       %>
       
    </body>
</html>   现在的问题是,当我输入正确的用户名的时候总是进入到LogInSuccess.jsp页面,也就是说if的条件是假,可我的输入是正确的,而且,我用out.write()把得到的用户名和密码打出来,是正确的,可是为什么执行不出正确结果呢?

解决方案 »

  1.   

    uid = request.getParameter("username").toString();
               upwd = request.getParameter("userpwd").toString();
            
                out.write(uid);
                out.write(upwd);
       
               if(uid == "zhx".toString() && upwd == "1")
                { 
                   response.sendRedirect("LogInSuccess.jsp");
                }
                else 
                {
                 
                }    
    你为什么要写.toString啊?
    你把uid == "zhx".toString() 换成uid.equals("zhx")看看呢
      

  2.   

    楼上说的对String不是基本类型需要用equals
      

  3.   

    if(uid == "zhx".toString() && upwd == "1")
                { 
                   response.sendRedirect("LogInSuccess.jsp");
                }进入LogInSucce.jsp if应该是true吧...
      

  4.   

    String的==和equals结果是一样的
      

  5.   

    谢谢两位,换成uid.equals("zhx"),好使了,可是我的是什么问题?我开始没用tostring(),因为总是不好使,所以才加的