大家帮我看看哪不对了,为什么不输出结果呢?
写了一个简单的身份验证,可是我无论输什么,他都输出"123", 大家帮我看看这是怎么回事?<%@ page contentType="text/html; charset=gbk" language="java" errorPage="" %>
<%@page import ="java.sql.Connection" %>
<%@page import ="java.sql.DriverManager" %>
<%@page import ="java.sql.SQLException" %>
<%@page import ="java.sql.Statement" %>
<%@page import ="java.sql.ResultSet" %>
<%!
String usrName,usrPassword;
boolean loginAttempt = false;
%>
<%
if(request.getParameter("name") != null && request.getParameter("passwd")!=null)
{
loginAttempt=true;
}
if(!loginAttempt)
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test","sa","sa");
if(!conn.isClosed())
{
out.println("成功连接");
usrName=request.getParameter("name");
usrPassword=request.getParameter("passwd");
String sql="SELECT * FROM test WHERE name='"+usrName+"'";
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
{
if(usrPassword.equals(rs.getString("passwd")))
out.println("ok");
else
out.println("no");
}
rs.close();
stmt.close();
}
conn.close();
}
else
out.println("123");
%>
<html>
<body>
<form action="#" />
<input type="text" name="name"/>
<input type="text" name="passwd"/>
<input type="submit" value="确定"/>
<input type="reset" value="撤销"/>
</form>
</body>
</html>
搜索更多相关主题的帖子:
结果 输出
----------------解决方案--------------------------------------------------------
支持一下
----------------解决方案--------------------------------------------------------
if(request.getParameter("name") != null && request.getParameter("passwd")!=null)
{
loginAttempt=true;
}
if(!loginAttempt)
你只要输入了东西那不管输什么request.getParameter("name") 和 request.getParameter("passwd")都不等于 null 那么你的 loginAttempt就等于true,然后你下面的判断
!loginAttempt才执行你连接数据库的代码 你的loginAttempt已经等于true了 非真即为假 所以不管你输入什么都执行else输出123
[[italic] 本帖最后由 guoxhvip 于 2007-12-1 15:43 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
我抗不住了,你杀了我吧!求你了!!!
----------------解决方案--------------------------------------------------------
hardes 你是那个??
----------------解决方案--------------------------------------------------------
嗯,有道理,可是如果把它 if ( ! loginAttempt ) 改成 if ( loginAttempt ) ,数据库又连不上了
呵呵,登录验证我做好了!
不过我想知道这个到底为什么不行?那我到底怎么改?
----------------解决方案--------------------------------------------------------
你不是都改好了吗 当你loginAttempt 为true时 !loginAttempt就为false
if(条件为false)肯定就不执行啦
----------------解决方案--------------------------------------------------------
原帖由 [bold][underline]glamey[/underline][/bold] 于 2007-12-1 16:20 发表 [url=http://bbs.bc-cn.net/redirect.php?goto=findpost&pid=1125903&ptid=189727][/url]
hardes 你是那个??
hardes 你是那个??
怎么这么问,呵呵!!
----------------解决方案--------------------------------------------------------
原帖由 [bold][underline]guoxhvip[/underline][/bold] 于 2007-12-1 18:01 发表 [url=http://bbs.bc-cn.net/redirect.php?goto=findpost&pid=1126129&ptid=189727][/url]
你不是都改好了吗 当你loginAttempt 为true时 !loginAttempt就为false
if(条件为false)肯定就不执行啦
你不是都改好了吗 当你loginAttempt 为true时 !loginAttempt就为false
if(条件为false)肯定就不执行啦
嗯,明白了,谢谢你的回复!
另,有谁可以给我说下:怎么把MyEclipse里的WEB-INF下的classes文件显示出来?我很想知道怎么做,3天了,这个问题还没解决!
----------------解决方案--------------------------------------------------------
问题解决了!我会了
----------------解决方案--------------------------------------------------------