jsp写的登陆,if判断总是出错
下面是我写的用来验证登陆的用户名密码是否正确的代码:
<%@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()把得到的用户名和密码打出来,是正确的,可是为什么执行不出正确结果呢?
[解决办法]
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 ")看看呢
[解决办法]
楼上说的对String不是基本类型需要用equals
[解决办法]
if(uid == "zhx ".toString() && upwd == "1 ")
{
response.sendRedirect( "LogInSuccess.jsp ");
}
进入LogInSucce.jsp if应该是true吧...