写在前面的话
本人也是刚接触到JSP,还有很多需要学习的地方。用JSP实现登录验证和注册我很认真的写了详细思路,以下代码有什么不合理或者需要改进的地方,大噶可以给我提宝贵的建议哈
数据库
现有 数据库 【userdemo】中表【information】
登录实现
思路步骤:
(1)表单提交信息跳转到登录验证界面,验证表单提交信息是否注册并且用户名和密码正确
(2)登录验证的思路:动态加载mysql驱动,连接数据库中相对应的表,与表中数据进行比对,用flag标记状态。根据比对结果跳转登录成功或者登录失败页面
(3)特别注意:这里跳转页面要使用 【请求转发】,不要使用 【重定向】。请求转发可以保留表单提交的数据,而重定向不能保留
登录页面
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登陆页面</title>
<style>
a {
text-decoration: none;
color: black;
}
form {
display:inline;
}
</style>
</head>
<body>
<form action='register_check.jsp' method="post">
用户名:<input type="text" name='name'><br>
密码:<input type="password" name='pwd'><br>
<input type="submit" value="登录">
</form>
<button><a href="1.jsp">注册</a></button>
</body>
</html>
登录验证
register_check.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登记验证</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");//动态加载mysql驱动
boolean flag = true;
Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/【数据库名】?serverTimezone=UTC","root","【密码】");
Statement stmt =conn.createStatement();
ResultSet RS_result = stmt.executeQuery("SELECT * FROM 【表名】 ");
String pwd,name;
while(RS_result.next()){
pwd = RS_result.getString("pwd");
name = RS_result.getString("name");
if(request.getParameter("name").equals(name)&&request.getParameter("pwd").equals(pwd)){
flag=false;
request.getRequestDispatcher("check_success.jsp").forward(request,response);
%>
<%
}else{
continue;
}
%>
<%
};
stmt.close();
conn.close();
if(flag)
request.getRequestDispatcher("check_fail.jsp").forward(request,response);
%>
</body>
</html>
登录成功
check_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<h2>登录成功</h2>
<%
//设置编码
request.setCharacterEncoding("UTF-8");
//获取值
String name = request.getParameter("name");
out.print("欢迎"+name);
%>
</body>
</html>
登录失败
check_fail.jsp
扫描二维码关注公众号,回复:
10713772 查看本文章
![](/qrcode.jpg)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录失败</title>
</head>
<body>
<h2>登陆失败</h2>
<h2>输入的用户名或密码有错</h2>
</body>
</html>
注册实现
思路步骤:
注册页面
1.jsp
(1)在登录页面中 点击【注册】按钮(其内是一个a标签) 进入注册页面
(2)注册验证同登录验证相类似。连接数据库–>与数据库中信息比对,是否已经有相同用户名注册–>若没有则注册成功,有则注册失败(用flag做标志)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action='registered.jsp' method="post">
用户名:<input type="text" name='name'><br>
密码:<input type="password" name='pwd'><br>
<input type="submit" value="注册">
</form>
</body>
</html>
注册验证
registered.jsp
<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="javax.naming.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册验证</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");//动态加载mysql驱动
String name1=request.getParameter("name").trim();//去除首尾空格
String pwd1=request.getParameter("pwd").trim();
boolean flag = true;
Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/【数据库名】?serverTimezone=UTC","root","【密码】");
Statement stmt =conn.createStatement();
ResultSet RS_result = stmt.executeQuery("SELECT * FROM 【表名】 ");
String pwd,name;
while(RS_result.next()){
pwd = RS_result.getString("pwd");
name = RS_result.getString("name");
if(request.getParameter("name").equals(name)){
out.print("用户已经存在 "+"请重新<a href=\"1.jsp\">注册</a>");
flag=false;
break;
};
}
%>
<%
if(flag){
stmt.executeUpdate("insert into infomation values('"+name1+"','"+ pwd1 + "')");
%>
注册成功!!!<br>
请点击<a href="text.jsp">这里</a>登录!!!
<%
};
%>
<%
stmt.close();
conn.close();
%>
</body>
</html>
注册成功
数据库中