mainpage:<html>
    <head><title>欢迎来到仓库管理主页</title>
        <script language="javascript">  
        </script>
        <style type="text/css">
            <!-- 
            @import URL("mainstyle.css");
            -->
        </style>
    </head>
    <center >
        <caption><h2><bold>欢迎来到仓库管理主页</bold></h2></caption>
        <body id="bg1">
            <h3>管理员登陆</h3>
           
            <table border="3" bgcolor="1E98B2">
                <tr><td>
                    <form action="checklogin.jsp" method="post" name="form1">                    
                        <table>
                            <tr>
                                <td>管理员账号</td>
                                <td><input type="text" name="text1"></td>
                            </tr>
  
                            <tr>
                                <td>管理员密码</td>
                                <td><input type="password" name="text2"></td>
                            </tr>
                
                            <tr>
                                <td><input type="submit" name="submit1" value="登陆"></td>
                                <td><input type="reset" name="reset1" value="重置"></td>
                            </tr>
                        </table>                    
                    </form>
                </td>
                </tr>
            </table>
            <p><br>
            <hr>
            <div id="block1">
                <h3>浏览用户登陆</h3>
            
                <a href="userindex.jsp">浏览用户登陆页面</a><br>
            </div>
            <p>
            <div>
                <img src="images/bottom2.gif" width="50%" height="15%">
            </div>
        </body>
    </center>
</html>
<!--
to add a tail or a picture here later
-->
<%@ page contentType="text/html;charset=gb2312" language="java"%>
<%@ page import="java.sql.*,java.util.*"%><html>
    <head>
        <title>CheckLoginInformation</title>
    </head>
    <body>
          
       
        <%!String URL,SQL,name1,password1; %>
        <%!ResultSet rs=null;%>
        <%!Statement stmt=null;%>
        <%!Connection conn=null;%>
        
        <%
            name1=request.getParameter("text1").trim();
            name1=new String(name1.getBytes("ISO-8859-1"));
            password1=request.getParameter("text2").trim();
            password1=new String(password1.getBytes("ISO-8859-1"));            if((name1.equals(""))||(password1.equals(""))) {
                response.sendRedirect("error.jsp");
            }
            try{
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
                URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DepartmentM";
                conn=DriverManager.getConnection(URL,"admin5","123456");
                stmt=conn.createStatement();
                SQL="select * from Admin where name='"+name1+"'"+"and"+"password='"+password1+"'";
                rs=stmt.executeQuery(SQL);
                if(rs.getRow()>0){
                    response.sendRedirect("userindex.jsp");} else{
                    response.sendRedirect("mainpage.html");
                    }            } catch(Exception e) {
                e.printStackTrace();
                response.sendRedirect("error2.jsp");
            } finally{
                conn.close();
            }
            %>
        
        <center><a href="mainpage.html">返回首页</a></center>
    </body>
</html>
开始用的BEAN,结果不成功,现在换成了jsp还是有问题,运行后就直接调到error2.jsp了,我也检查了很久,就是没有找到错误..

解决方案 »

  1.   

    SQL="select * from Admin where name='"+name1+"'"+"and"+"password='"+password1+"'";
    看出来了吗,上面这句话错了,你上面的and前后都少一个空格,你上面的sql解析出来是下面这个样子的。
    例如你输入的用户名和密码是admin,admin
    select * from Admin where name='admin'andpassword='admin';
    这肯定错了,正确的就是把空格加上
    SQL="select * from Admin where name='"+name1+"'"+" and "+"password='"+password1+"'";
    你再试一下。
    给个建议,你调试这种纯jsp的程序,不知道那里出错了,直接在程序里面打印一些信息,看程序执行到那里了,那里没有执行,一下子就知道原因了,我以前搞ASP的时候就是,有问题,写很多的response.write,问题两下就解决了。。
      

  2.   

    catch(Exception e) {
                    System.out.println(e.getMessage+SQL);
                    response.sendRedirect("error2.jsp");
                } finally{
    把捕捉到的错误看看 就知道什么问题了
      

  3.   

    谢谢tcmis,这里的错误改过来了,但是还有问题,无论我输入正确还是输入错误用户名和密码,都会返回mainpage.html,感觉if else这里出了问题,但是不知道怎么改??
      

  4.   

    if里面不用括号
    if (name1=null || namu.equals("") || ...)  {
    .....
    }
      

  5.   

    sql语句中and左右加两个空格就应该可以了
      

  6.   

    你把 SQL打出来然后到数据库去刷一下 看看有没有记录
      

  7.   

    首先;
    if(rs.getRow()>0)
    检索当前行编号。第一行为 1 号,第二行为 2 号,依此类推。 
    最初你的rs是指向的第一调记录之前,所以你一直都会得到0
    2.程序走到catch里面了,说明执行SQL发生了错误.
    你应该把e.printStackTrace();在控制台的错误消息贴出来,大家好给你分析!
      

  8.   

    你要rs.next()一下,然后rs的指针才真正开始指向第一条记录!
      

  9.   

    我先执行了SQL="select * from Admin where name='"+name1+"'"+" and "+"password='"+password1+"'"
    此时的rs.getRow()是指向第一行之前还是指向符合SQL语句的那一行??
      

  10.   

    谢谢各位了,终于懂了,以前一直一位执行了SQL语句后就直接条到那一行了,结果还要执行一个rs.next()才行.
      

  11.   

    用conn.createStatement(ResultSet.Type_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY)
    试试
      

  12.   

    这是你的SQL语句 SQL="select * from Admin where name='"+name1+"'"+"and"+"password='"+password1+"'";
    拼下来应该是:
    select * from Admin where name='111'andpassword='222'你用下面这句试试看:
    SQL="select * from Admin where name='"+name1+"' and password='"+password1+"'";
      

  13.   

    还有就是你的ResultSet的指针是指在那里的?应该要rs.next一下吧!