下面是jsp代码
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=gb2312" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
        <title>Login</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->  </head>
  
  <body>
   
    <%!  
    //变量声明
    private static String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
private static String dbURL="jdbc:odbc:factory";
   
    private static String user="";
    private static String pwd="";
    private static Connection conn=null;
    private static Statement stmt=null;
    private static ResultSet rs=null;
   
    %>
    <%//加载驱动程序,建立连接
    try{
    Class.forName(driverName);
    conn=DriverManager.getConnection(dbURL,user,pwd);
    }catch(Exception e){
    e.printStackTrace();
    }
   
     
        //获取用户的输入
      String name=request.getParameter("username");
      String password=request.getParameter("userpassword");
      String sql="select * from userInfo where name="+"'"+name+"'"+" and"+" password="+"'"+password+"'";
      try{
      stmt=conn.createStatement();
      rs=stmt.executeQuery(sql);
      }catch(Exception e){
      e.printStackTrace();
      }
      System.out.println(rs.next()+""+rs.getString(1));
   
        if(rs.next())
    { 
      %>
       <jsp:forward page="login_success.jsp" />
      <%
       }
       else
       {
      %>
       <jsp:forward page="login_failure.jsp" />
      <%
       }//end if-else
      
      
        try{
         rs.close();
         stmt.close();
         conn.close();
        }catch(Exception e){
         e.printStackTrace();
        }
     %>
    </body>
</html>
该代码的目的是想在用户名和密码都输入正确的情况下转到login_success.jsp页面,否则就转移到login_failure.jsp页面,但执行以后,无论输入正确账号和密码还是错误的账号和密码,都会转到login_failure.jsp页面.

解决方案 »

  1.   

    可能是:1.private static String user=""; 
      private static String pwd="";conn=DriverManager.getConnection(dbURL,user,pwd); 
    你的数据库用户名user和 pwd都没设置
    2.System.out.println(rs.next()+""+rs.getString(1)); 
    能输出么?
    你检查一下你的rs是否已经获取到值?楼主试试 
      

  2.   

     在这里吧
    System.out.println(rs.next()+""+rs.getString(1)); 
    rs已经被next()
    去掉这句
      

  3.   

    我觉得是:
    其一是这里import="java.sql.*" 还应该导入与之相对应的java程序
    其二是 System.out.println(rs.next()+""+rs.getString(1)); 应为out.println(rs.next()+""+rs.getString(1));否则输不出结果,这也是值得引起注意的
    楼主试试 
      

  4.   

    System.out.println(rs.next()+""+rs.getString(1)); 
    去掉。。
    rs.next()执行了。。被跳转到下个了。。下个没有。。所以当然不行了。