当前位置:网站首页>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: : 
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 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 :
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 :
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>
边栏推荐
- 双向电平转换电路
- Dart学习——函数、类
- Oracle 常用基础函数
- Lumiprobe cell imaging analysis: PKH26 cell membrane labeling kit
- 如何做好水库大坝安全监测工作
- PCR/qPCR研究:Lumiprobe丨dsGreen 用于实时 PCR
- When excel copies the contents of a row, the columns are separated by the tab "\t"
- WordPress zibll sub theme 6.4.1 happy version is free of authorization
- 二级造价工程师考试还没完?还有资格审核规定!
- Deeplearning ai-week1-quiz
猜你喜欢

To batch add background pictures and color changing effects to videos

Biovendor sRAGE protein solution

Based on the order flow tool, what can we see?

線條動畫

MySQL 45 talk | 05 explain the index in simple terms (Part 2)

How to learn programmable logic controller (PLC)?

Why is point shield cloud forced to quit playing?

codeforces每日5题(均1700)

Sqlmap tool user manual

Gorm transaction experience
随机推荐
Organize the online cake mall project
Gorm transaction experience
Leetcode 88: merge two ordered arrays
109. simple chat room 12: realize client-side one-to-one chat
msa. h: There is no such file or directory
Informatics Orsay all in one 1360: strange lift
Object detection with OpenCV
JS中的链表(含leetcode例题)<持续更新~>
How to learn programmable logic controller (PLC)?
Don't roll! How to reproduce a paper with high quality?
MySQL 45 talk | 05 explain the index in simple terms (Part 2)
How to design an awesome high concurrency architecture from scratch (recommended Collection)
WordPress zibll sub theme 6.4.1 happy version is free of authorization
Operation of 2022 power cable judgment question simulation examination platform
Question bank and answers of 2022 materialman general basic (materialman) operation certificate examination
To batch add background pictures and color changing effects to videos
[JVM] - memory partition in JVM
Store inventory management system source code
jq图片放大器
SlicePlane的Heading角度与Math.atan2(y,x)的对应转换关系