- 登陆:
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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆页面</title>
</head>
<body>
<form action="LoginServlet" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
<td><input type="button" value="注册" onclick="window.location.href('register.jsp')"/></td>
</tr>
</table>
</form>
</body>
</html>
servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
response.setCharacterEncoding("gb2312");
request.setCharacterEncoding("UTF-8");
// TODO Auto-generated method stub
String user = request.getParameter("user"); //从表单获取用户名
String pass = request.getParameter("password"); //从表单获取密码
System.out.println(user+pass); //测试
//判断
if (connectsql(user, pass) == 1){
response.getWriter().println("登陆成功!"+"Hello " + user + "!");
response.getWriter().println("<br/><a href='http://localhost:8080/project_1/login.jsp'>返回登陆页面");
}
else if (connectsql(user, pass) == 2) {
response.getWriter().println("密码错误!请重新输入");
response.getWriter().println("<br/><a href='http://localhost:8080/project_1/login.jsp'>返回登陆页面");
}
else {
response.getWriter().println("登陆失败!"+"Sorry " + user + "!"+"请先进行注册");
response.getWriter().println("<br/><a href='http://localhost:8080/project_1/login.jsp'>返回登陆页面");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
@SuppressWarnings("resource")
protected int connectsql(String username, String password){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver"); // 加载数据库驱动
// 通过驱动管理类获取数据库链接
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/login?serverTimezone=UTC&characterEncoding=utf-8","root", "PdM46t");
String sql = "select * from record"; // 定义sql语句
// 获取预处理statement
preparedStatement = connection.prepareStatement(sql);
// 向数据库发出sql执行查询,查询出结果集
resultSet = preparedStatement.executeQuery();
// 遍历查询结果集
while (resultSet.next()) {
System.out.println(resultSet.getString("user") + " "
+ resultSet.getString("password"));
if (resultSet.getString("user").equals(username))
if (resultSet.getString("password").equals(password))
return 1; //用户名密码正确,返回1
else
return 2; //用户名正确,密码错误,返回2
}
}
catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//用户名不存在,返回0
return 0;
}
- 注册:
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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="RegisterServlet" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="注册"/></td>
<td><input type="button" value="返回" onclick="window.location.href('login.jsp')"/></td>
</tr>
</table>
</form>
</body>
</html>
servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
response.setCharacterEncoding("gb2312");
request.setCharacterEncoding("UTF-8");
String user = request.getParameter("user"); //从表单获取用户名
String pass = request.getParameter("password"); //从表单获取密码
//response.getWriter().println(user+pass); //测试
connectsql(user,pass);
response.getWriter().println("注册成功");
response.getWriter().println("<br/><a href='http://localhost:8080/project_1/login.jsp'>返回登陆页面");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
protected int connectsql(String username, String password){
Connection con = null;
PreparedStatement pstm = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 通过驱动管理类获取数据库链接
con= DriverManager
.getConnection(
"jdbc:mysql://localhost:3306/login?serverTimezone=UTC&characterEncoding=utf-8",
"root", "PdM46t");
String sql = "insert into record(user,password) value(?,?)";
pstm = con.prepareStatement(sql);
pstm.setString(1, username);
pstm.setString(2, password);
int row = pstm.executeUpdate();
System.out.println("新增数据为:" + row + "条");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
finally{
if(con != null){
try {
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
if(pstm != null){
try {
pstm.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
return 0;
}
数据库: