java+jsp+mysql实现登入注册识别功能

注册
1. 注册的前端代码 register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>注册模块</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<````````link rel="stylesheet" type="text/css" href="styles.css">
	-->

  <body>
<body background="file:///Users/apple/Desktop/java/DonTai/WebContent/css/images/9f187d8d12874b12dcb614a55749959e.jpg"
style=" background-repeat:no-repeat ;
background-size:100% 100%;
background-attachment: fixed;"
>
    <center>
    <br>
    <br>
	<font face="楷体" size="6" color="#000">注册界面</font>
	<form action = "jq.jsp" method = "post" >
	<br>
	<br>
	<br>
  	<table width="300" height="180" border="0" bordercolor="#A0A0A0">
  	 身    份:
  	<select class="INPUT_text" name="userType">

							<option value="-1" selected="selected">请选择注册的身份</option>
							<option value="0">管理员</option>
							<option value="1">普通成员</option>
							
							
					</select>
  	<tr>
		<th>注册识别码:</th>
		<td><input type="text" name="recognition"  maxlength = "16" ></td>
 	  </tr>
  	  <tr>
		<th>真实姓名:</th>
		<td><input type="text" name="username"  maxlength = "17" ></td>
 	  </tr>
 	 
 	  <tr>
 		<th>输入密码:</th>
 		<td><input type="text" name="password"   maxlength = "20" ></td>
 	  </tr>
 	  <tr>
 		<th>确认密码:</th>
 		<td><input type="text" name="newword"  maxlength = "20"></td>
 	  </tr>
	  <tr>
 		<td colspan="2" align="center">
 		  <input type="submit" value="注  册">    
 		  <input type="reset" value="重  置">
 		  <input type="reset" value="关 闭"onclick ="window.close()">
 		  
 		</td>
	  </tr>
	</table>
    </form>
    </center>
  </body>
</html>

2.封装好JDBC(可运行进行测试是否连接好了数据库)
运行这段代码之前,请加入mysql的JDBC jar包

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBUtils { // Math.PI

	public static final String DRIVER = "com.mysql.cj.jdbc.Driver";

	public static final String URL = "jdbc:mysql://localhost:3306/db01";..**// 最后db01为你的数据库名字**

	public static final String USER = "root"; **//mysql的登入账号(一般默认为root)**

	public static final String PASSWORD = "12345678";**//密码**

	static {

		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

	public static Connection getConnection() throws Exception {
		return DriverManager.getConnection(URL, USER, PASSWORD);

	}

	public static void closeAll(ResultSet rs, PreparedStatement prep, Connection conn) throws Exception {
		if (rs != null) {
			rs.close();
		}

		if (prep != null) {
			prep.close();
		}

		if (conn != null) {
			conn.close();
		}
	}

	public static void main(String[] args) throws Exception {
		Connection conn = getConnection();
		System.out.println(conn);
	}

}

3.数据库层
mysql的代码为下

use db01
create table userr(
UName varchar(20),
x_password varchar(20),
recognition int     //传入0和1来对后面的进行识别身份
);

**4.注册时候按你的识别码进行(普通成员/管理员)注册

普通成员的注册码为 711 管理员为7116**

java系统中管理员和超级管理员怎么设置 java管理员和普通用户登录_网页登入注册


4.注册指向的web后端 jsp代码 checkRegister.jsp

<%@ page language="java" import="java.util.*,java.sql.*"
	pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'Feilong_chechRegister.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>
<body>
	<%
		try {
			String user = new String(request.getParameter("username").getBytes("ISO-8859-1"), "UTF-8");
			String pwd = request.getParameter("password");
			String newwrod = request.getParameter("newword");
			String userType = request.getParameter("userType");//身份
			String recognition = request.getParameter("recognition");//识别码
			Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); 
	        //如果显示没有这个包,需要下载connect for java并添加相应jar包

	        String databaseName = "db01";// 确保在MySQL数据库中创建CREATE好的数据库。  
	        String userName = "root";// MySQL默认的root账户名  
	        String password = "12345678";// 默认的root账户密码为空,或者用户设置的密码  
	        //端口号一般默认为3306,在安装mySQL时候可以自己修改.
	        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + databaseName, userName, password);  
			PreparedStatement pStmt = conn.prepareStatement("select * from  userr where UName = '" + user + "'");
			ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				out.println(
						"<script language='javascript'>alert('该用户已存在,请重新注册!');window.location.href='register.jsp';</script>");
			}
			
			//注册处理
			else if (recognition.equals("711")&&userType.equals("0")) {
				if(!newwrod.equals(pwd)){
					out.println("<script language='javascript'>alert('2次密码不相同!');window.location.href='http://localhost:8080/DonTai/register.jsp';</script>");

							}else{
								PreparedStatement tmt = conn.prepareStatement("Insert into tb_user(UName,x_password,recognition) values('"
										+ user + "','" + pwd + "','" + userType + "')");
								int rst = tmt.executeUpdate();
								if (rst != 0) {
									out.println(
											"<script language='javascript'>alert('管理员注册成功!');window.location.href='http://localhost:8080/DonTai/register.jsp';</script>");
								} else {
									out.println(
											"<script language='javascript'>alert('管理员注册失败!');window.location.href='http://localhost:8080/DonTai/register.jsp';</script>");
								}
							}
							}


			else if (recognition.equals("7116")&&userType.equals("1")) {
				if(!newwrod.equals(pwd)){
					out.println("<script language='javascript'>alert('2次密码不相同!');window.location.href='http://localhost:8080/DonTai/register.jsp';</script>");
				}else{
					
					PreparedStatement tmt = conn.prepareStatement("Insert into tb_user(UName,x_password,recognition) values('"
							+ user + "','" + pwd + "','" + userType + "',)");
					int rst = tmt.executeUpdate();
					if (rst != 0) {
						out.println(
								"<script language='javascript'>alert('普通成员注册成功!');window.location.href='http://localhost:8080/DonTai/register.jsp';</script>");
					} else {
						out.println(
								"<script language='javascript'>alert('普通成员注册失败!');window.location.href='http://localhost:8080/DonTai/register.jsp';</script>");
					}

				}
				
				
				}
				
				
				
			else{	
				out.println("<script language='javascript'>alert('识别码输入有误(或者身份不匹配)');window.location.href='http://localhost:8080/DonTai/register.jsp';</script>");
			}
			
		} catch (Exception e) {
			out.println(
					"<script language='javascript'>alert('未输入用户名或密码');window.location.href='http://localhost:8080/DonTai/register.jsp';</script>");
			
			
		
		}


	%>
</body>

5.登入部分 login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<base href="<%=basePath%>">

<title>My JSP 'Feilong_login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">


</head>

<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	
	-->
	
<body >
<body background="file:///Users/apple/Desktop/java/DonTai/WebContent/css/images/4a270715129080f2de2afddf4da638de.jpg"
style=" background-repeat:no-repeat ;
background-size:100% 100%;
background-attachment: fixed;"
>

	<center>

		<br> <br>

		<td><div align="center"
				style="FONT-WEIGHT: bold; FONT-SIZE: 40pt;">学生宿舍管理系统</div></td> <br>
		<br> <br> <br> <br> <br> <br> <br>        
		<br>
		<%
			String flag = request.getParameter("errNo");
			try {
				if (flag != null)
					out.println("用户名不存在或密码错误(身份未选或选择身份不符合)");
			} catch (Exception e) {
				e.printStackTrace();
			}
		%>
		
		<form action="loginCh.jsp" method="post">
			<table width="300" height="180" border="0" bordercolor="#A0A0A0">
				
				<tr>
					<th>账 户:</th>
					<td><input type="text" name="name" maxlength="16"></td>
				</tr>
				<tr>
					<th>密 码:</th>
					<td><input type="password" name= "pwd" maxlength="20"></td>
					
				</tr>
				<tr>
					<td colspan="2" align="center">
					
					<input type="submit"
						name="submit" value="登       录"> <input type="button"
						value="返       回"bv
						onclick="window.location.href='http://localhost:8080/DonTai/NewFile4.jsp'">
					
					
						<input name="Submit2" type="reset" class="submit1" value="重置">
						<br> <br> 身    份: <select
						class="INPUT_text" name="userType">
							<option value="-1" selected="selected">请选择登陆身份</option>
							<option value="0">管理员</option>
							<option value="1">普通成员</option>
						
					</select>
					
				
					
					
					</td>

				</tr>
				
			</table>
			
			
		</form>
	
	</center>

</body>

</html>

6.登入的 jsp后端文件 login.jsp

根据数据库传过来的 0(管理员)和1(普通成员)来进行一个判断,然后指向相应. 身份的主页

<%@ page language="java" import="java.util.*,java.sql.*,java.net.*"
	pageEncoding="utf-8"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'Feilong_loginCh.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>
<body>
	<%
		//接收用户名和密码  
		String user = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
		String pwd = request.getParameter("pwd");
		String userType =  request.getParameter("userType");
		Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); 
        //如果显示没有这个包,需要下载connect for java并添加相应jar包

        String databaseName = "db01";// 确保在MySQL数据库中创建CREATE好的数据库。  
        String userName = "root";// MySQL默认的root账户名  
        String password = "12345678";// 默认的root账户密码为空,或者用户设置的密码  
        //端口号一般默认为3306,在安装mySQL时候可以自己修改.
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + databaseName, userName, password);  

		PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user
				+ "' and P wd = '" + pwd + "' and userType= '" + userType + "' ");
		ResultSet rs = pStmt.executeQuery(); 
		if (rs.next()) {
			session.setAttribute("name", user);//缓存用户名
			int usetType = rs.getInt("userType");
			if (usetType == 0) {
				response.sendRedirect("frameset.jsp?username=" + URLEncoder.encode(user)); //解决乱码 (管理员主页)	 
			} else if (usetType == 1) {
				response.sendRedirect("frameset.jsp?username=" + URLEncoder.encode(user)); //普通成员	 

			} 
			
		}

		else {
			response.sendRedirect("login.jsp?errNo");//密码不对返回到登陆  
		}
		rs.close();
		pStmt.close();
		conn.close();
	%>
</body>
</html>

本人(初学者)原创代码
如果有不对地方还望见谅
谢谢支持
第一份博客 2019.3.20