当前位置:网站首页>JSP connects with Oracle to realize login and registration (simple)

JSP connects with Oracle to realize login and registration (simple)

2022-06-28 05:28:00 Yuanxiaobai

requirement
tomcat、oracle The database service starts
Need one Oracle The driver of , Put it in WEB-INFO Of lib. Version number: :
Eclipse Create directory
Database files
stay Oracle In the database scott The connection of , Create a name called t_user Table of , The fields of the table are username,password.

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Sign in </title>
<style>
input[type="submit"] {
    
	width: 100px;
	margin: 0 5px;
}
input[type="button"] {
    
	width: 100px;
	margin: 0 5px;
}
</style>
</head>
<body>
	<center>
	<font face=" Regular script " size="6" color="#000"> Login screen </font>
	<form action="index.jsp" method="post">
	<table width="300" height="180" border="5" bordercolor="#A0A0A0">
		<tbody>
			<tr>
				<td><label> user name :</label></td>
				<td><input type="text" name="name" maxlength = "12" placeholder=" Please enter a user name !" /></td>
			</tr>
			<tr>
				<td><label> The secret &nbsp;&nbsp;&nbsp;  code :</label></td>
				<td><input type="password" name="pwd" maxlength = "16" placeholder=" Please input a password !" /></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<input type="submit" value=" land " />
				<input type="button" value=" register " onclick = "window.open('reg.jsp')" /></td>
			</tr>
		</tbody>
	</table>
	</form>
	</center>
</body>
</html>

The renderings are as follows :
 Insert picture description here

index.jsp

Check the connection between login interface and database
Realize database connection , Determine whether the user name and password are correct when logging in .

<%@ page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
	<%
		String uName = new String(request.getParameter("name"));
	String uPwd = request.getParameter("pwd");

	Connection conn = null;
	PreparedStatement pstm = null;
	ResultSet rs = null;
	String driverClass = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	String user = "scott";
	String password = "tiger";
	String sql = "select username,password from t_user";
	try {
    
		Class.forName(driverClass);
		conn = DriverManager.getConnection(url, user, password);
		pstm = conn.prepareStatement(sql);
		rs = pstm.executeQuery();
		while (rs.next()) {
    
			if (rs.getString("username").equals(uName) && rs.getString("password").equals(uPwd)) {
    
				RequestDispatcher dis = request.getRequestDispatcher("success.jsp");
				dis.forward(request, response);
				return;
			} 
		}
		response.sendRedirect("login.jsp");
	} catch (ClassNotFoundException e) {
    
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
    
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
    
		try {
    
			if (rs != null) {
    
				rs.close();
				rs = null;
			}
			if (pstm != null) {
    
				pstm.close();
				pstm = null;
			}
			if (conn != null) {
    
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	%>
</body>
</html>

reg.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> The registration screen </title>
<style>
input[type="submit"] {
    
	width: 100px;
	margin: 0 5px;
}

input[type="reset"] {
    
	width: 100px;
	margin: 0 5px;
}
</style>
<script>
	function addCheck() {
    
		var username = document.getElementById("username").value;
		var password = document.getElementById("password").value;
		var newword = document.getElementById("newword").value;
		if (username == "") {
    
			alert(" The username cannot be empty !");
			document.getElementById("username").focus();
			return false;
		}
		if (password == "") {
    
			alert(" The password cannot be empty !");
			document.getElementById("password").focus();
			return false;
		}
		if (password != newword) {
    
			alert(" The two passwords are different !");
			document.getElementById("newword").focus();
			return false;
		}
	}
	function validate() {
    
		var flag = addCheck();
		if (flag == false)
			return false;
		return true;
	}
</script>
</head>
<body>
	<center>
		<font face=" Regular script " size="6" color="#000"> The registration screen </font>
		<form action="index2.jsp" method="post"
			onsubmit="return validate()">
			<table width="300" height="180" border="5" bordercolor="#A0A0A0">
				<tr>
					<th> user name :</th>
					<td><input type="text" name="name" id="username"
						placeholder=" Input 16 Within characters " maxlength="16"
						onfocus="if(this.value == ' Input 16 Within characters ') this.value =''"></td>
				</tr>
				<tr>
					<th> Input password :</th>
					<td><input type="password" name="pwd" id="password"
						placeholder=" Input 20 Within characters " maxlength="20"
						onfocus="if(this.value == ' Input 20 Within characters ') this.value =''"></td>
				</tr>
				<tr>
					<th> Confirm the password :</th>
					<td><input type="password" name="nwd" id="newword"
						placeholder=" Reenter the password " maxlength="20"
						onfocus="if(this.value == ' Reenter the password ') this.value =''"></td>
				</tr>
				<tr>
					<td colspan="2" align="center"><input type="submit"
						value=" notes   book "> <input type="reset" value=" heavy   Set up "></td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>

The renderings are as follows :
 Insert picture description here

index2.jsp

Check the connection between the registration interface and the database
Realize database connection , Add the registered user name and password to the database .

<%@ page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
	<%
		String uName = request.getParameter("name");
		String uPwd = request.getParameter("pwd");
		
		Connection conn =null;
		PreparedStatement pstm = null;
		ResultSet rs =  null;
		String driverClass = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String user = "scott";
        String password = "tiger";
        String sql="insert into t_user(username,password) values(?,?)";
        try{
    
        	Class.forName(driverClass);
			conn = DriverManager.getConnection(url, user, password);
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, uName);
			pstm.setString(2, uPwd);
			rs=pstm.executeQuery();
			if(rs.next()){
    
				out.print(" User registration successful !");
				response.sendRedirect("login.jsp");
			}else{
    
				out.print(" User registration failed !");
				response.sendRedirect("reg.jsp");
			}
        }catch (ClassNotFoundException e) {
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
			try {
    
				if(rs!=null) {
    	
					rs.close();		
					rs =null;
				}
				if(pstm!=null) {
    	
					pstm.close();
					pstm =null;
				}
				if(conn!=null) {
    	
					conn.close();
					conn =null;
				}
			} catch (SQLException e) {
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}       
	%>
</body>
</html>
原网站

版权声明
本文为[Yuanxiaobai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/179/202206280521409150.html