2013-01-17 89 views
0
<form> 
. . . 
    <div class="loginDiv"> 
     <%= new java.util.Date() %> 
     <br/><br/> 
     Username : <input type="text" class="resizedTextbox" name="username">&nbsp;&nbsp; 
     Password : <input type="text" class="resizedTextbox" name="password">&nbsp;&nbsp; 
     <input type="submit" name="btnLogin" value="LogIn"> 
      &nbsp;&nbsp;&nbsp; 
      <a href="register.jsp">Register</a> 
    </div> 
</form> 
       <% 
       if(request.getAttribute("btnLogin") != null){ 
        String username = request.getParameter("username"); 
        String password = request.getParameter("password"); 
        if(username != "" && password != ""){ 
         out.print("Invalid Username/Password!"); 
        } 
        else{ 
         out.print("Invalid Username/Password!"); 
        } 
       } 
       %> 

我放置了一個斷點,當我點擊btnLogin時,request.getAttribute(「btnLogin」)..內部的代碼永遠不會被調用。 這是爲什麼呢?按鈕沒有被JSP調用?

回答

1

嘗試這樣做:

if(request.getParameter("username") != null && request.getParameter("password") != null) 

,而不是這樣的:

if(request.getAttribute("btnLogin") != null){ 
0
<form action="Call this Login page here (eg: Login.jsp)" method="post"> 
      <div class="loginDiv"> 
       <%= new java.util.Date()%> 
       <br/><br/> 
       Username : <input type="text" class="resizedTextbox" name="username">&nbsp;&nbsp; 
       Password : <input type="text" class="resizedTextbox" name="password">&nbsp;&nbsp; 
       <input type="submit" name="btnLogin" value="LogIn"> 
       &nbsp;&nbsp;&nbsp; 
       <a href="register.jsp">Register</a> 
      </div> 
     </form> 
     <%    
      String username = request.getParameter("username"); 
      String password = request.getParameter("password"); 
      if (username != "" && password != "") {     
      } else { 
       out.print("Invalid Username/Password!"); 
      } 
     %>