结果截图:(点击登录之后用户名称和密码都会消失,所以是空的)
①什么都不输入
②输入错误信息
③输入正确信息
源代码:(图片设置的代码没有在这个程序里,代码是找同学问的<新手勿怪>)
①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>
未按时完成的原因:界面代码没有了解清楚,只能看课本一点一点来。
目标:熟练数据库和界面,自己开发一个小型网站。