2014-02-09 31 views
0

我有一個MySQL數據庫,其中有一個用戶ID和一個密碼列。他們每個人都有「mads」的價值。我有一些問題,現在我一直在苦苦掙扎,因爲我沒有長時間編程。我有一個JSP頁面,我有我的表單和一個連接到MySQL數據庫的servlet。當我把用戶名和密碼放入mads時,我總是收到「你無效」的信息。這意味着它按照我的意願給我答案「用戶是有效的」。我不想創建一個用戶,我只想檢查用戶是否存在於數據庫中。我的JSP和servlet代碼在這裏:我希望有人能幫助我,因爲我真的不知道什麼是錯的。如果用戶存在於數據庫中,則返回

問候的Mads

<%@ page language="java" contentType="text/html; charset=US-ASCII" 
    pageEncoding="US-ASCII"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> 
<title>Validation</title> 
<style type="text/css"> 
/* border-radius er rundt hjørner på input..*/ 
input[type=text] {padding:5px; border:2px solid #ccc; webkit-border-radius: 5px; border-radius:5px;} 
input[type=text]:focus{border-color:yellow;} 
input[type=password] {padding:5px; border:2px solid #ccc; webkit-border-radius: 5px; border-radius:5px;} 
input[type=password]:focus{border-color:yellow;} 
input[type=submit] {padding: 5px 15px; background:#ccc; border:0 none; cursor:pointer; webkit-border-radius:5px; border-radius: 5px;} 
</style> 
</head> 
<body> 
    <br><br><br> 
     <center> 
      <h1>Please enter user name and password</h1> 

      <form name="frm" action="LoginValidation" method="post"> 
       <input type="text" name="user"> 
       <input type ="password" name="pass"> 
       <input type="submit" value="Check" class="submit"> 
      </form> 
     </center> 
</body> 

而且我的servlet:

import java.io.*; 
//import java.util.*; 
import java.sql.*; 

import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.*; 
import javax.servlet.*; 

@WebServlet(urlPatterns = {"/LoginValidation"}) 
public class Validation extends HttpServlet { 

    private static final long serialVersionUID = 1L; 
    private ServletConfig config; 

    public void init (ServletConfig config) 
    throws ServletException{ 
     this.config = config; 
    } 

    public void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException,IOException { 


     PrintWriter out = response.getWriter(); 
     String connectionURL = "jdbc:mysql://localhost/dblogin"; 
     Connection connection = null; 
     ResultSet rs; 
     String userid = request.getParameter("userid"); 
     String password =request.getParameter("password"); 
     ////and your select statement 

     try{ 
      String sql = "SELECT * FROM login WHERE userid = ? AND password = ?"; 
      Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(connectionURL, "root", ""); 

      PreparedStatement preparedStatement = connection.prepareStatement(sql); 
      preparedStatement.setString(1, userid); 
      preparedStatement.setString(2, password); 

      Statement s = connection.createStatement(); 
      rs =preparedStatement.executeQuery(); 

     ////if there are next ib rs so you have a user by this id and password 
      if(rs.next()) { 
       out.println("The user is valid"); 
      } 
      else { 
       out.println("You are not valid"); 
      } 

     }catch(Exception e) { 
      System.out.println("The exception is" + e); 
     } 
    } 
} 
+2

您SQL請求是壞,你可能需要用'SELECT * FROM登錄其中userid =? AND password =?' – 2014-02-09 13:00:47

+2

另外,你如何檢查用戶輸入'String userid = new String(「」);'(密碼相同)? – 2014-02-09 13:01:43

+0

你好RC。感謝您的快速回答。嗯,這是一個很好的問題......所以代碼無效:? 'String userid = new String(「」); String password = new String(「」);' 我需要找到一種方法來檢查輸入嗎? – McDuck4

回答

1

可能的解決辦法:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { 
     PrintWriter out = response.getWriter(); 
     String connectionURL = "jdbc:mysql://localhost/dblogin"; 
     Connection connection = null; 
     PreparedStatement preparedStatement = null; 
     ResultSet rs = null; 
     String userid = request.getParameter("user"); 
     String password = request.getParameter("password"); 
     response.setContentType("text/html"); 

     try { 
      // Load the database driver 
      Class.forName("com.mysql.jdbc.Driver"); 
      connection = DriverManager.getConnection(connectionURL, "root", ""); 
      //Add the data into the database 
      String sql = "SELECT * FROM login WHERE userid = ? AND password = ?"; 

      preparedStatement = connection.prepareStatement(sql); 
      preparedStatement.setString(1, userid); 
      preparedStatement.setString(2, password); 

      rs = preparedStatement.getResultSet(); 

      if(rs.next()) { 
       // redirect or print but not both... 
       out.println("The user is valid"); 
       // response.sendRedirect("index_true.jsp"); 
      } else { 
       out.println("You are not valid"); 
      } 
     } catch(Exception e) { 
      System.out.println("Exception is: " + e); 
     } finally { 
      // TODO: check for nullity 
      rs.close(); 
      preparedStatement.close(); 
      connection.close(); 
     } 
    } 

修正:

  • 使用請求參數來從數據庫讀取記錄
  • 執行正確的語句
  • ...

(有可能仍然會有一些問題,這是未測試)

0

您有一個空字符串useridpassword,並通過使用此值進行查詢,這樣就沒有什麼

,所以你需要先採取useridpassword從用戶返回,然後通過使用該值進行查詢,如果有結果,所以你必須有效用戶

String userid = request.getParameter("userid"); 
String password =request.getParameter("password"); 
////and your select statment 
String sql = "SELECT * FROM login WHERE userid = ? AND password = ?"; 
PreparedStatement preparedStatement = connection.prepareStatement(sql); 
preparedStatement.setString(1, userid); 
preparedStatement.setString(2, password); 

Statement s = connection.createStatement(); 
rs =preparedStatement.executeQuery(); 

////if there are next ib rs so you have a user by this id and password 
if(rs.next()) { 
    out.println("The user is valid"); 
} 
else { 
    out.println("You are not valid"); 
} 
+0

好吧,首先嚐試刪除其他評論,因爲它使我困惑,然後嘗試在doPost方法中打印用戶標識和密碼,然後告訴我 –

+0

,並清楚地知道您將得到空值,因爲您在request.getparametar中編寫了「userid」 ,並在jsp中將其命名爲'name':'',因此請使用userid和密碼 –

+0

我現在刪除了註釋:-)如果我運行我的java類代碼現在出現錯誤: HTTP狀態405 - HTTP方法GET不受此URL支持 類型狀態報告 消息HTTP方法GET不受此URL支持 description對於請求的資源不允許指定的HTTP方法。和我的jsp網站: 「你無效」 – McDuck4

0

在形式參數的名稱是用戶,但在servlet您正在使用的getParameter(「用戶ID」)

在這種情況下,調試servlet代碼以檢查收到的參數可能非常有用。

還拿在帳戶以前的評論的正確選擇語法和要傳遞空字符串是查詢參數,你應該使用請求參數和刪除檢查的結果,因爲它是與查詢本身複製..

0

您的查詢不會返回任何結果。您需要做的是將userId和密碼設置爲通過發佈請求發送的值。

userid = request.getParameter("userid"); 
password = request.getParameter("password"); 

當您運行查詢,你會不會需要一個循環,如果你的用戶名是唯一的(應該是)。

0

這是正確的答案:

<%@頁面語言= 「Java」 的的contentType = 「text/html的;字符集= US-ASCII」 的pageEncoding = 「US-ASCII」 %> Validation /* border-radius er rundthjørnerpåinput .. */ input [type = text] {padding:5px; border:2px solid #ccc; webkit-border-radius:5px; border-radius:5px;} input [type = text]:focus {border-color:yellow;} input [type = password] {padding:5px; border:2px solid #ccc; webkit-border-radius:5px; border-radius:5px;} input [type = password]:focus {border-color:yellow;} input [type = submit] {padding:5px 15px;背景:#CCC;邊框:0無;光標:指針; WebKit的邊界半徑:5像素;邊界半徑:5像素;}


請輸入用戶名和密碼

  <form name="frm" action="LoginValidation" method="post"> 
       <input type="text" name="userid"> 
       <input type ="password" name="password"> 
       <input type="submit" value="Check" class="submit"> 
      </form> 
     </center> 
</body> 

package jsp; 

import java.io.*; 
import java.sql.*; 

import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.*; 
import javax.servlet.*; 

@WebServlet(urlPatterns = {"/LoginValidation"}) 
public class Validation extends HttpServlet { 

    private static final long serialVersionUID = 1L; 

    public void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException,IOException { 

     PrintWriter out = response.getWriter(); 
     String connectionURL = "jdbc:mysql://localhost/dblogin"; 
     Connection connection = null; 
     PreparedStatement preparedStatement = null; 
     ResultSet rs = null; 
     String userid = request.getParameter("userid"); 
     String password =request.getParameter("password"); 
     response.setContentType("text/html"); 


     try{ 
      Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(connectionURL, "root", ""); 
      String sql = "SELECT * FROM login WHERE userid = ? AND password = ?"; 

      preparedStatement = connection.prepareStatement(sql); 
      preparedStatement.setString(1, userid); 
      preparedStatement.setString(2, password); 

      //Statement s = connection.createStatement(); 
      rs =preparedStatement.executeQuery(); 

      if(rs.next()) { 
       out.println("The user is valid"); 
      } 
      else { 
       out.println("You are not valid"); 
      } 

     }catch(Exception e) { 
      System.out.println("The exception is" + e); 

     } 

    } 
} 
相關問題