当前位置:网站首页>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>
边栏推荐
- Liuhaiping's mobile phone passes [[uiapplication sharedapplication] delegate] window. safeAreaInsets. The height of the bottom security zone is 0
- Gee learning notes 3- export table data
- Jdbc的使用
- Assembly common instructions
- Simple usage of GSAP
- [leetcode] 12. Integer to Roman numeral
- Redis 的 最新windows 版本 5.0.14
- 線條動畫
- How to develop the language pack in the one-to-one video chat source code
- [untitled] drv8825 stepping motor drive board schematic diagram
猜你喜欢

OpenSSL client programming: SSL session failure caused by an obscure function

2022 new version NFT source code source code of China meta universe digital collection art trading platform

Feign implements path escape through custom annotations

Shutter nestedscrollview sliding folding head pull-down refresh effect

双向电平转换电路

【JVM系列】JVM调优

Latest Windows version 5.0.14 of redis

Biovendor sRAGE protein solution

A guide to P2P network penetration (stun) for metartc5.0 programming
![[Linux] - using xshell to install MySQL on Linux and realize the deployment of webapp](/img/07/e044a6ef14a6576dbee1c6a009ab4f.png)
[Linux] - using xshell to install MySQL on Linux and realize the deployment of webapp
随机推荐
2022 special operation certificate examination question bank and simulation examination for safety management personnel of fireworks and firecrackers business units
Can wechat applets import the data in the cloud development database into makers with one click in the map component
線條動畫
When using the MessageBox of class toplevel, a problem pops up in the window.
What does mysql---where 1=1 mean
Create NFS based storageclass on kubernetes
Lumiprobe cell imaging analysis: PKH26 cell membrane labeling kit
Programmer - Shepherd
WordPress zibll sub theme 6.4.1 happy version is free of authorization
博客登录框
How does the power outlet transmit electricity? Simple problems that have plagued my little friend for so many years
Gee learning notes 3- export table data
JS text box loses focus to modify width text and symbols
codeforces每日5题(均1700)
关系数据库与文档数据库对比
2022 low voltage electrician examination questions and answers
Yunda's cloud based business in Taiwan construction 𞓜 practical school
改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP
? How to write the position to output true
二级造价工程师证书含金量到底有多高?看这些就知道了