JSP实现登录注册连接MySql数据库-初学(JSP+JDBC实现)
Posted zhy-08
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP实现登录注册连接MySql数据库-初学(JSP+JDBC实现)相关的知识,希望对你有一定的参考价值。
数据库准备
首先用mysql建一个数据库java_test,在这个数据库中建一个表td_user。
导入mysql 驱动包
在项目\\webapp\\WEB-INF\\lib引入mysql 驱动包。
程序实现思路(相关页面阐述)
index.jsp:选择跳转到登录或注册页;
login.jsp:提供用户的登录表单,可以输入用户名和密码;
dologin.jsp:登录检查页,根据表单提交数据验证权限,并跳转到登录成功页;
login_success.jsp:登录成功页,显示欢迎信息;
register.jsp:提供用户的注册表单,可以输入注册新用户的用户名和密码;
doregister.jsp:注册执行页,将注册的新用户信息导入数据库;
register_success.jsp:注册成功页,可以跳转到登录页。
程序实现
1.index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<a href="login.jsp">登录</a><br>
<a href="register.jsp">注册</a><br>
</div>
</body>
</html>
2.login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页</title>
<script type="text/javascript">
function check()
var username=document.getElementById("userName").value;
var password=document.getElementById("userPwd").value;
if(username=='')
alert("用户名不能为空");return false;
else if(password=='')
alert("密码不能为空");return false;
else
return true;
</script>
</head>
<body>
<form action="dologin.jsp" method="post">
用户名:<input type="text" name="userName" id="userName" /><br/>
密码:<input type="password" name="userPwd" id ="userPwd" /><br/>
<input type="submit" value="提交" onclick="return check()">
</form>
</body>
</html>
3.dologin.jsp
<%@ page contentType="text/html" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<html>
<head>
<title>登录验证</title>
</head>
<body>
<% String name=request.getParameter("userName").toString();
String passwd=request.getParameter("userPwd").toString();
try
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/java_test?serverTimezone=UTC&useSSL=false&characterEncoding=UTF-8";
String username="root";
String password="1234";
Connection conn=DriverManager.getConnection(url,username,password);
String sql="select * from tb_user where userName=? ";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, name);
ResultSet rs=ps.executeQuery();
out.print("警告: ");
rs.next();
if((rs.getString("userPwd")).equals(passwd))
out.print("登录成功");
request.setAttribute("userName",name);
request.getRequestDispatcher("login_success.jsp").forward(request, response);
else out.print("密码错误");
ps.close();
conn.close();
catch(Exception e)
out.print("登录失败");
e.printStackTrace();
%>
<br>
<a href="login.jsp">返回</a>
</body>
</html>
4.login_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>登录成功</title>
</head>
<body>
欢迎,<%=request.getAttribute("userName")%>
<a href="login.jsp">返回</a>
</body>
</html>
5.register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>注册页</title>
<script type="text/javascript">
function check1()
var username=document.getElementById("userName").value;
var password=document.getElementById("userPwd").value;
if(username=='')
alert("用户名不能为空");return false;
else if((password==''))
alert("密码不能为空"); return false;
else
return true;
</script>
</head>
<body>
<form action="doregister.jsp" method="post">
用户名:<input type="text" name="userName" id="userName"/><br/>
密码:<input type="password" name="userPwd" id="userPwd"/><br/>
<input type="submit" value="注册" onclick="return check1()"/>
</form>
</body>
</html>
6.doregister.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>
<%
String username=new String(request.getParameter("userName"));
String password=new String(request.getParameter("userPwd"));
// 数据库连接字符串
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/java_test?serverTimezone=UTC&useSSL=false&characterEncoding=UTF-8";
// 数据库用户名
String usename = "root";
// 数据库密码
String psw ="1234";
Connection conn=DriverManager.getConnection(url,usename,psw); //创建连接
String sql="insert into tb_user(userName,userPwd) values('"+username+"','"+password+"')";
Statement stmt=conn.createStatement(); //获取statement对象
stmt.executeUpdate(sql); //执行添加用户的sql语句
request.getRequestDispatcher("register_success.jsp").forward(request, response); //请求转发到register_success.jsp
%>
</body>
</html>
7.register_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>
注册成功!
<a href="login.jsp">返回登录</a>
</body>
</html>
Jsp实现注册登录(连接Mysql)
Jsp实现注册登录(连接mysql)
最近在学jsp连接数据库,存个档吧
- 先在mysql里创建一个数据库
- 创建表
- 写一个用户注册的页面
registered.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<form method="post" action="zc.jsp">
输入用户名:<input type="text" name="username"><br>
输入密码:<input type="text" name="userpwd"><br>
确认密码:<input type="text" name="cuserpwd"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
- 读取用户的信息写入到数据库,完成注册
zc.jsp
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//获取用户输入信息
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String userpwd=request.getParameter("userpwd");
String cuserpwd=request.getParameter("cuserpwd");
//判断两次密码是否相同
if (cuserpwd.equals(userpwd)) {
Connection conn = null;
Statement stmt = null;
try {
//连接驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//建立数据库连接
String url="jdbc:mysql://localhost:3306/bookstore_demo";
//数据库账号
String user="root";
//数据库密码
String upwd="lj20011028";
try {
conn= DriverManager.getConnection(url,user,upwd);
//创建发射器
stmt=conn.createStatement();
//创建sql语句
String sql="insert into yonghu(username,userpwd) value ('"+username+"','"+userpwd+"')";
//发送sql语句并接收结果
int count = stmt.executeUpdate(sql);
if (count>0){
out.print("注册成功");
out.print("<a href='logoin.jsp'>登录</a>");
}
else{
out.print("注册失败");
}
//关闭资源
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
else {
out.print("注册失败");
}
%>
</body>
</html>
- 再写一个用户登录界面
logoin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>logoin</title>
</head>
<body>
<form action="logoin_yz.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="userpwd" ><br>
<input type="submit" value="登录">
</form>
</body>
</html>
- 获取用户输入的信息,在数据库中查询有相同记录则登陆成功
logoin_yz.jsp
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%
//获取用户输入信息
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String userpwd=request.getParameter("userpwd");
Connection conn = null;
Statement stmt = null;
try {
//连接驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//建立数据库连接
String url = "jdbc:mysql://localhost:3306/bookstore_demo";
//数据库账号
String uname="root";
//数据库密码
String upwd="lj20011028";
try {
conn = DriverManager.getConnection(url, uname, upwd);
//创建发射器
stmt = conn.createStatement();
//创建sql语句
String sql = "select*from bookstore_demo.yonghu where username='"+username+"' and userpwd='"+userpwd+"'";
//发送sql语句并接收结果
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
out.print("登陆成功");
} else {
out.print("登陆失败");
}
rs.close();
}
catch (SQLException e) {
e.printStackTrace();
}
%>
</body>
</html>
- 运行
从注册页面开始运行
若两次密码输入不相同则提示注册失败
若两次密码输入相同则提示注册成功
在mysql中可以通过查询查看获取的用户信息
注册成功点击登录跳转到登陆界面
输入注册的用户名和密码即可登陆成功
若输入的用户名与密码不匹配则登录失败
以上是关于JSP实现登录注册连接MySql数据库-初学(JSP+JDBC实现)的主要内容,如果未能解决你的问题,请参考以下文章
如何在Android Studio上利用云数据库Mysql实现在线登录注册?
招新系统(jsp+servlet,实现简略前端网页注册登录+后台增删改查,分学生和管理员,Java语言,mysql数据库连接,tomcat服务器)