[英]Login Authentication using JDBC and JSP : How to check if the username and Password entered by the user exists in the Database?
[英]How to check if username exists in database or not without submitting the form using jsp?
我試圖在不提交表單的情況下檢查用戶名是否存在於數據庫中。 但我沒有成功。 下面是我的代碼。
控制器1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ page import="java.util.regex.*"%>
<%-- <%@ page import="java.net.URLEncoder" session="false" %> --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Controller</title>
</head>
<body>
<div class="container">
<%
String Username = request.getParameter("Username");
session.setAttribute( "theName", Username );
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc?autoReconnect=true&useSSL=false", "root", "");
try{
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from signupform where Username='" + Username + "'");
if(rs.next()){
String usermessage = "Username already exists";
request.getSession().setAttribute("usermessage", usermessage);
response.sendRedirect("Signupform.jsp");
}
else{
PreparedStatement ps = con. prepareStatement("insert into signupform(Username) values(?)");
ps.setString(1, Username);
if(Username.isEmpty())
{
String message = "Please fill all the fields";
request.getSession().setAttribute("message", message);
/* response.sendRedirect("Signupform.jsp?message="+ URLEncoder.encode(message, "UTF-8")); */
response.sendRedirect("Signupform.jsp");
}
else{
ps.executeUpdate();
response.sendRedirect("Welcome.jsp");
}
}
}
catch (Exception e) {
out.println(e);
}
%>
</div>
</body>
</html>
用於提供輸入的表單顯示在代碼 Signupform.jsp 中。 當用戶填寫相應字段時,它應該立即在同一頁面報告用戶是否存在。
報名表.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</title>
<link Rel=stylesheet type="text/css" href="main.css">
</head>
<body>
<div class ="container">
<div>
<form action="controller1.jsp" method = "POST">
Username <input type = "text" name = "Username" value="">
<br>
<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
<br>
<p>${usermessage}</p>
<c:remove var="usermessage" scope="session"/>
</form>
</div>
</div>
</body>
</html>
對於初學者來說,這是一個巨大的安全漏洞。 您不能像這樣進行數據庫查詢; 使用准備好的語句。 或者,填寫用戶名:
joe'; DROP TABLE signupform CASCADE; --
並且您的數據庫將消失。
您似乎知道 preparedstatements 是如何工作的(您將它用於插入)。 你必須在任何地方都使用它,你所有的 SQL 幾乎都必須是一個字符串常量。 如果您曾經鍵入"SELECT...." +...
停止。 不要那樣做。
想要的東西不容易做到。 這不是 web 的工作原理。 客戶端是客戶端,服務器是服務器,除非您明確表示,否則它們不會相互聯系。
客戶端必須使用 javascript(在客戶端上運行的代碼)在您輸入時詢問服務器,觸發用戶名字段更改,這本身就很棘手(有很多方法可以更改瀏覽器中的文本字段。例如,您可以右鍵單擊並粘貼 select,因此注冊鍵盤處理程序是不好的)。
Easy-ish 解決方案:使用setInterval
每 ±200 毫秒左右檢查一次。 如果自上次檢查以來該字段未被修改,請不要執行任何操作,並且不要檢查 ajax 呼叫是否正在進行中。 否則,向服務器發送一個 ajax 調用來詢問,並操縱 DOM 來反映這一點。
你將不得不學習 javascript,這不是你可以在一小時內完成的事情。 這真的可以歸結為:“我如何編寫 webapps”。 這個問題有答案,但 SO 不是一個好地方。 對於書籍和非常長的教程來說,這更像是一個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.