我有一個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);
}
}
}
您SQL請求是壞,你可能需要用'SELECT * FROM登錄其中userid =? AND password =?' – 2014-02-09 13:00:47
另外,你如何檢查用戶輸入'String userid = new String(「」);'(密碼相同)? – 2014-02-09 13:01:43
你好RC。感謝您的快速回答。嗯,這是一個很好的問題......所以代碼無效:? 'String userid = new String(「」); String password = new String(「」);' 我需要找到一種方法來檢查輸入嗎? – McDuck4