首先我们要知道 severlet是用来处理客户端请求的服务端年组件
首先我们需要设计一个简易的前端页面,让用户来输入用户名和密码以及实现提交功能
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=request.getContextPath()%>/"/>
<title>用户登录</title>
<link type="text/css" rel="stylesheet" href="css/style.css;">
</head>
<body bgcolor="#E7ECEF">
<center>
<form action="/toLogin" method="post">
<table border="0" cellspacing="0" cellpadding="0" style="margin-top:130px">
<tr><td><img src="images/logon_top.gif"></td></tr>
<tr height="180">
<td background="images/logon_middle.gif" align="center" valign="top">
<table border="0" width="90%" cellspacing="0" cellpadding="0">
<tr height="50"><td colspan="2"></td></tr>
<tr height="30">
<td align="right" width="40%">用户名: </td>
<td style="text-indent:5px">
<input type="text" name="username" size="30px" value="" id="log_Login_action_user_userName"/>
<span>${error}</span>
</td>
</tr>
<tr height="30">
<td align="right">密 码: </td>
<td style="text-indent:5px"><input type="password" name="password" size="30px" id="log_Login_action_user_userPassword"/></td>
</tr>
<tr height="60">
<td></td>
<td>
<input type="submit" id="" value="登录"/>
<input type="reset" value="重置"/>
<a id="log_Login_action_" href="view/indextemp.jsp">[返回首页]</a>
</td>
</tr>
</table>
</td>
</tr>
<tr><td><img src="images/logon_end.gif"></td></tr>
</table>
</form>
</center>
</body>
</html>
然后再设计一个报错页面以及简易的登录成功页面
index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body >
<img src="images/1.png">
</body>
</html>
接下来开始定义数据库连接Dao类
首先将数据库账号密码以及url地址都存入在配置文件里面,易于修改并且安全性高
db.properpies
db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/cityfile2021?characterEncoding=utf8
db.driver=com.mysql.jdbc.Driver
然后定义连接数据库的工具类,在前面的文章里面对于jdbc连接数据库有详细的解释,这里不再赘述
package until;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/*
连接数据的类
*/
public class DBUtil {
private static String username;
private static String password;
private static String url;
private static String driver;
static {
try {
InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
// FileInputStream in=new FileInputStream("db.properties");
System.out.println("in:"+in);
Properties p=new Properties();
p.load(in);
username = p.getProperty("db.username");
password=p.getProperty("db.password");
url=p.getProperty("db.url");
driver=p.getProperty("db.driver");
System.out.println(username);
System.out.println(password);
System.out.println(url);
System.out.println(driver);
} catch (IOException e) {
e.printStackTrace();
}
}
//封装一个连接方法
public static Connection getConn(){
Connection conn=null;
try {
//加载驱动
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
System.out.println("...........................");
System.out.println(conn);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
}
最后建立一个查询类,查询结果返回role的值,从而实现根据账户的权限不同决定登录成功后访问的页面不同
package dao;
import until.DBUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UsersCrudDao {
private String username;
private String password;
public UsersCrudDao(String username, String password) {
this.username = username;
this.password = password;
}
public Integer selectStudent() throws SQLException {
//获得连接
Connection connection=DBUtil.getConn();
//编辑数据库语句
String sql="select role from tb_admin_user where name=\""+username+"\"and pwd=\""+password+"\"";
//建立statement对象和resuleset结果对象
Statement statement=null;
ResultSet resultSet=null;
try {
int role=-1;
int count=0;
statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
//用ResultSet的next方法判断是否有下一个
while (resultSet.next()){
//将相关的属性值引出来 并且存入到student对象中
role=resultSet.getInt("role");
count++;
}
if (count!=1){
return -1;
}
return role;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
resultSet.close();
statement.close();
connection.close();
}
return -1;
}
}