验证分两种情况 ,成功,进入管理员页,可以看信息和删记录
失败,直接给出登录失败,然后重新登录
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//登录
//将号和密码包为user对象
//查user对象是否存在
//如果再切换到管理页
//如果不再继续返回login.jsp页
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="dologin.jsp" method="post">
用户名:<input type="text" name="uname" required="required" size="20" value="">
<br>
密 码:<input type="password" name="upwd" required="required" size="20">
<br>
<input type="submit" />
<input type="reset" />
</form>
</body>
</html>
dologin.jsp
<%@page import="org.apache.tomcat.dbcp.dbcp2.PStmtKey"%>
<%@page import="java.sql.PreparedStatement"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="
java.util.Date,
java.io.IOException,
java.io.PrintWriter,
java.sql.*
" %>
<%@ page import="src.User"
%>
<%
//将传过来的信息 包在值类里
User you=new User();//登录的人的信息包装成对象
you.setName(request.getParameter("uname"));
you.setPwd(request.getParameter("upwd"));
%>
<%
//1.导入jar包
//2.注册驱动
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
//3.获取连接
Connection con = DriverManager.getConnection("jdbc:mysql://mysql.sqlpub.com:3306/huangjin","laocooon","fc12f7a5215e8e0a");
//4.获取执行者对象
String sql = "SELECT * FROM adminuser WHERE uname = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, you.getName());//给第一个参数为用户名
//5.执行sql语句,并且接收结果
ResultSet rs = pstmt.executeQuery();
User fUser= new User();
//6.遍历记录集
while(rs.next()){
fUser.setId(rs.getInt(1));
fUser.setName(rs.getString(2));
fUser.setPwd(rs.getString(3));
}
// 7 各种关闭
rs.close();
pstmt.close();
con.close();
//找到用户名,现在分析密码对不对
if(fUser.getPwd().equals(you.getPwd())){
//由于 此信息需要整体网站需要,所以使用session处理
session.setAttribute("adminuser",fUser);
//如果没有找到记录,说明 fUser的id是0
if(fUser.getId()==0)
response.sendRedirect("./error.html");
else
response.sendRedirect("./adminuser.jsp");
}
else
response.sendRedirect("./error.html");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>验证登录是否成功</title>
</head>
<body>
</body>
</html>
error.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录失败 </title>
<!--
<style type="text/css">
p{
text-align: center;
font-size: 24px;
color:red;
}
</style>
-->
<link href="./css/myCss.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p>登录失败</p>
<p><a href="./login.jsp">重新登录</a></p>
</body>
</html>