結果截圖:(點擊登錄之后用戶名稱和密碼都會消失,所以是空的)
①什么都不輸入
②輸入錯誤信息
③輸入正確信息
源代碼:(圖片設置的代碼沒有在這個程序里,代碼是找同學問的<新手勿怪>)
①DBUtil.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String user = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3306/eeeee";
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
}
②login.jsp
<%@page import = "com.test.Util.DBUtil" %>
<%@page import = "java.sql.*" %>
<%@page import = "java.sql.PreparedStatement" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username == null || "".equals(username.trim())){
request.setAttribute("result", "請輸入用戶名!");
%>
<jsp:forward page = "showlogin.jsp"></jsp:forward>
<%
}
if(password == null || "".equals(password.trim())){
request.setAttribute("result", "請輸入密碼!");
%>
<jsp:forward page = "showlogin.jsp"></jsp:forward>
<%
}
Connection connection = DBUtil.getConnection();
boolean flag = false;
String sql = "select * from user where username = ?";
PreparedStatement preparedstatement = null;
ResultSet resultset = null;
preparedstatement = connection.prepareStatement(sql);
preparedstatement.setString(1,username);
resultset = preparedstatement.executeQuery();
while(resultset.next()){
if(resultset.getString("password").equals(password)){
flag = true;
request.setAttribute("result", "登錄成功!");
%>
<%=request.getAttribute("result")%>
<%
}
else{
request.setAttribute("result", "密碼錯誤!請重新登錄!");
%>
<jsp:forward page = "showlogin.jsp"></jsp:forward>
<%
}
}
if(!flag){
request.setAttribute("result", "該用戶不存在!登錄失敗!");
%>
<jsp:forward page = "showlogin.jsp"></jsp:forward>
<%
}
%>
</html>
③
Showlogin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用戶登錄頁面</title>
</head>
<body>
<%=request.getAttribute("result") %>
<form action="login.jsp" method = "get">
<table align = "center" border = "1" width = "500">
<tr>
<td>用戶名稱:</td>
<td>
<input type = "text" name = "username"/>
</td>
</tr>
<tr>
<td>用戶密碼:</td>
<td>
<input type = "password" name = "password"/>
</td>
</tr>
<tr align = "center">
<td colspan = "2">
<input type = "submit" value = "登錄"/>
<input type = "reset" value = "重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
未按時完成的原因:界面代碼沒有了解清楚,只能看課本一點一點來。
目標:熟練數據庫和界面,自己開發一個小型網站。