向我解释以下JSP代码的一些部分:

时间:2016-08-18 16:21:38

标签: java jsp

我无法在以下JSP代码中获得一段代码(注释为d1,d2,d3,d4):

<%@page import="java.sql.*" errorPage="/MyError.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>LoginProcess</title>
</head>
<body>
    <%
      Connection conn = null;
      String uname = request.getParameter("uname");
      String pass = request.getParameter("pass");
      try
      {
        Class.forName("com.mysql.jdbc.Driver");
      }
      catch(ClassNotFoundException e)
      {
        out.println("Error(class):"+e);
      }
      try
      {
        conn = DriverManager.getConnection("jdbc:mysql://localhost/studentdb","root","mysql");
        PreparedStatement stmt = conn.prepareStatement("select * from studentdb.userdetails where uname=? and pass=?");
        stmt.setString(1, uname);
        stmt.setString(2, pass);
        ResultSet rs = stmt.executeQuery();
        if(!rs.next())
                {
                  out.println("username or password is incorrect");
    %>     <%--d1--%>
      Try Again:<%@include file="Login.html" %> <%--d2--%>
      </body>   <%--d3--%>
      </html>    <%--d4--%>
     <%        
         return;
        } //if
      } //try-sql
      catch(SQLException e)
      {
        out.println("Error(SQL):" + e);
      }
      finally
       {
         conn.close();
       }

    %>
    This is Home Page<br>
    Welcome,<b> <%= uname%></b>
</body>

以下内容出现在if块中,我不知道为什么以及它们如何工作,我知道&lt;%@ include file =&#34; Login.jsp&#34;&gt;的含义和选择性标签,但不是我得到他们如何在这里工作。

   %>     <%--d1--%>                                                          
  Try Again:<%@include file="Login.html" %> <%--d2--%>                      
  </body>   <%--d3--%>                                                       
  </html>    <%--d4--%>                                             
 <%

(正文和html标签没有出现在上面的代码中。)

1 个答案:

答案 0 :(得分:0)

而不是if(!rs.next()),请尝试使用if(rs.isBeforeFirst())。如果rs.isBeforeFirst()false,则表示您没有记录。