JSP实现登录注册连接MySql数据库-初学(JSP+JDBC实现)

     阅读:26

数据库准备

首先用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>