一个登陆页面(login,jsp)一个成功登陆页面(success.jsp)
请问:在JSP里怎么实现登陆验证(连接数据库)?

解决方案 »

  1.   

    什么年代了阿?交给SERVLET办吧,不好么?
      

  2.   

    不好意思,是JSP新手,什么都不懂^-^,还请各位大虾帮帮忙.谢谢
      

  3.   

    写在 success.jsp里面好了  
    不过一般都不是这么写的,起码也要写在一个BEANS里面
      

  4.   

    你获取登入的用户名或密码的值.
    然后若在数据库中搜索数据,若有则跳转到success.jsp中.
    给你个例子吧!
    <form method="POST" name="form9" action="success.jsp">
                  <p class="bod">&gt;&gt;用户名:<br>
                    <input name="username" type="text" class="denglu" value="" size="17" maxlength="17" tabindex="1">
                    <br>
                    &gt;&gt;密码:<br>
                    <input name="password" type="password" class="denglu" value="" size="17" maxlength="17" tabindex="2">
                    <br>
      
     <form>success.jsp<%!String UserName,PassWord,sql;%>
    <%
      UserName=request.getParameter("username");
      PassWord=request.getParameter("password");          sql="Select * From 用户表 Where 用户名='"+UserName+"' And 用户密码='"+PassWord+"'";
              rs=stmt.executeQuery(sql);
              rs.last();
              if (rs.getRow()>0){ 
             //to do 
    }
      

  5.   

    给你一个通过验证的代码:
    我用的是连接池进行的数据库操作哈!如果你用的是JDBC的话,把我注释掉的部分拿来用就可以了哈!当提交表单的时候就提交到这个页面进行登陆的验证:
    tring strUserName = request.getParameter("txtUserName");
    String strPassword = request.getParameter("txtPassword");
    //创建数据库联接
    ConnectionPool connPool = DB.getConnPool();
    Connection con = connPool.getConnection();
    Statement stmt = null;
    ResultSet rs = null;
    //Connection con = null;
    try {
    //Class.forName("com.mysql.jdbc.Driver");
    //DriverManager.registerDriver(new Driver()); 
    //String strUrl = "jdbc:mysql://127.0.0.1:3306/UserLogin?useUnicode=true&characterEncoding=GBK";
    //String strUser = "root";
    //String strPwd = "";
    //System.out.println("1");  
    //con = DriverManager.getConnection(strUrl,strUser,strPwd);
    //System.out.println("2");
    //int nextMessageid = 0;
    //String relative_path = null;    
    String sql = "select * from userinfo where username = '"
    + strUserName + "' and userpwd = '" + strPassword + "'";
    stmt = con.createStatement();
    rs = stmt.executeQuery(sql);
    if (rs.next()) {
    request.getSession(true).setAttribute("UserName",
    strUserName);
    response.sendRedirect("sucess.jsp");
    } else {
    request.getSession(true).setAttribute("UserName",strUserName);
    response.sendRedirect("fail.jsp");
    //out.println("对不起,登陆失败!");
    }
    } catch (SQLException sqlExc) { sqlExc.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (rs != null) {
    rs.close();
    }
    if (stmt != null) {
    stmt.close();
    }
    if (con != null) {
    con.close();
    }
    connPool.freeConnection(con);
    } %>