一、web结构图
二、jsp(V)
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/12/6 0006
Time: 18:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<center>
<h1 style="color:red">登录</h1>
<form id="indexform" name="indexForm" action="/web002/login" method="post">
<table border="0">
<tr>
<td>账号:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password">
</td>
</tr>
</table>
<br>
<input type="submit" value="登录" style="color:#BC8F8F">
</form>
</center>
</body>
</html>
三、servlet(C)
package com.login;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import com.db.*;
@WebServlet(name = "Servlet")
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
String html = null;
Mysqldb db = new Mysqldb();
if (db.isLogin(username, password))
html = "<div style='color:green'>登入成功</div>";
else
html = "<div style='color:red'>登入失败</div>";
PrintWriter pw = response.getWriter();
pw.println(html);
db.close();
}
四、数据库(M)
package com.db;
import java.sql.*;
public class Mysqldb {
// JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://10.0.0.101:3306/admin?useSSL=false";
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "root";
private Connection conn = null;
private PreparedStatement stmt = null;
private ResultSet resultset = null;
public Mysqldb() {
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("数据连接成功!");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("数据连接失败!");
}
}
public boolean isLogin(String username, String password) {
try {
String sql = "select * from login where username=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
resultset = stmt.executeQuery();
if (resultset != null) {
while (resultset.next()) {
String pwd = resultset.getString("password");
System.out.println("pwd:"+pwd);
if (pwd.equals(password)) {
return true;
} else {
return false;
}
}
} else {
return false;
}
} catch (Exception ex) {
System.out.println("ִ执行查询错误!");
}
return false;
}
public void close() {
try {
stmt.close();
conn.close();
} catch (Exception e) {
}
}
}
五、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.login.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>