今天写一个可以用后端验证的登录页面,用到了我前面写的一篇力的代码
先新建一个项目(注意这次是新建一个田Java Enterprise类了,注意还要勾选这个web application,这样子才可以用到web页面
这里提上一嘴,Java Enterprise是一个服务器端 Java应用程序,可以用它来写web
然后新建之后发现多了几个东西,相比之前的
多了一个WEB-INF的文件夹,里面存放的是网络配置文件
然后把昨天那篇博客里面的东西全部弄到src里面,然后进行改写,把四个功能增删改查写道一个类里面,升级为dao,命名为UserDao
public class UserDao {
public static void main(String[] args) {
}
public List<User> findAll() {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<User> userList = new ArrayList<>();
try {
connection = DButil.getConnection();
String sql = "select * from tb_user";
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
userList.add(user);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
DButil.closeAll(resultSet, statement, connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return userList;
}
public User getOneUserByUsername(String username) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
User user = new User();
try {
connection = DButil.getConnection();
String sql = "select * from tb_user where username = ?";
statement = connection.prepareStatement(sql);
statement.setString(1, username);
resultSet = statement.executeQuery();
while (resultSet.next()) {
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
DButil.closeAll(resultSet, statement, connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return user;
}
public void deleteById(int id) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DButil.getConnection();
String sql = "delete from tb_user where id = ?";
statement = connection.prepareStatement(sql);
statement.setInt(1, id);
statement.executeUpdate();
System.out.println("Successful execution");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
DButil.closeAll(null, statement, connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public void insertOne(User user) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DButil.getConnection();
String sql = "INSERT INTO `tb_user` VALUES (?, ?, ?)";
statement = connection.prepareStatement(sql);
statement.setInt(1, user.getId());
statement.setString(2, user.getUsername());
statement.setString(3, user.getPassword());
statement.executeUpdate();
System.out.println("Successful execution");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
DButil.closeAll(null, statement, connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public void UpdateOne(User user) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DButil.getConnection();
String sql = "update tb_user set username = ?,password = ? where id = ?";
statement = connection.prepareStatement(sql);
statement.setString(1, user.getUsername());
statement.setString(2, user.getPassword());
statement.setInt(3, user.getId());
statement.executeUpdate();
System.out.println("Successful execution");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
DButil.closeAll(null, statement, connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
然后发现需要之前的DBunit和User,也导进去
public class User {
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override //重写toString使其一我们需要的形式输出
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
public class DButil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1", "root", "123456");
return connection;
}
public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
最后位置是这样子的
配置一下web.xml和index.jsp
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
//index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login" method="post">
username:<input name = "username" type="text">
username:<input name = "password" type="password">
<input type="submit" value="login">
</form>
</body>
</html>
然后开启TomCat,运行程序
成功