当前位置:网站首页>JSP connecting Oracle to realize login and registration
JSP connecting Oracle to realize login and registration
2022-06-28 05:27: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.
UserDao.java
UserDao Interface , Define the lookup user (findUser) Methods and registered users (addUser) Method .
package jsp2.dao;
import jsp2.po.User;
public interface UserDao {
/** * Find user * */
public User findUser(User user);
/** * Registered users * */
public void addUser(User user);
}
BaseDao.java
abstract class , Realization Database connection , close .
package jsp2.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public abstract class BaseDao {
private final static String url="jdbc:oracle:thin:@localhost:1521:orcl";
private final static String name="scott";
private final static String pass="tiger";
public Connection conn = null;
public PreparedStatement pstm = null;
public ResultSet rs = null;
/** * Database connection * */
public Connection getConn() {
try {
// Load database driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Create database connection object
conn = DriverManager.getConnection(url, name, pass);
}catch(ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/** * Close method * */
public void close() {
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();
}
}
}
UserDaoImpl.java
UserDao Implementation class
package jsp2.dao.impl;
import java.sql.SQLException;
import jsp2.dao.BaseDao;
import jsp2.dao.UserDao;
import jsp2.po.User;
public class UserDaoImpl extends BaseDao implements UserDao {
@Override
public User findUser(User user) {
// TODO Auto-generated method stub
User u = null;
try {
String sql="select username,password from t_user where username=? "
+ "and password=?";
conn = getConn();
pstm =conn.prepareStatement(sql);
pstm.setString(1, user.getUsername());
pstm.setString(2, user.getPassword());
rs= pstm.executeQuery();
while (rs.next()) {
u = new User(rs.getString("username"),rs.getString("password"));
}
} catch (SQLException e) {
// TODO: handle exception
}finally {
close();// Call the close method
}
return u;
}
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
try {
String sql="insert into t_user(username,password) values(?,?)";
conn = getConn();
pstm =conn.prepareStatement(sql);
pstm.setString(1, user.getUsername());
pstm.setString(2, user.getPassword());
pstm.executeQuery();
} catch (SQLException e) {
// TODO: handle exception
}finally {
close();
}
}
}
User.java
Encapsulates a user object .
package jsp2.po;
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserService.java
UserService Interface , Inherit UserDao The method in , Define whether the user repeats the method .
package jsp2.service;
import jsp2.dao.UserDao;
import jsp2.po.User;
public interface UserService extends UserDao {
/** * Whether the user repeats */
public boolean isReg(User user);
}
UserServiceImpl.java
package jsp2.service.impl;
import jsp2.dao.UserDao;
import jsp2.dao.impl.UserDaoImpl;
import jsp2.po.User;
import jsp2.service.UserService;
public class UserServiceImpl implements UserService {
public UserDao uDao = new UserDaoImpl();
@Override
public User findUser(User user) {
// TODO Auto-generated method stub
return uDao.findUser(user);
}
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
uDao.addUser(user);
}
@Override
public boolean isReg(User user) {
// TODO Auto-generated method stub
User u =uDao.findUser(user);
if(u==null) {
return true;
}
return false;
}
}
login.jsp
The login page , Used to log in users , Log in successfully and jump to success page , Failure to return login page .
<%@ 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>
index.jsp
Login detection , call UserService Medium java Code ( Method ), Implement user login
<%@page import="jsp2.service.impl.UserServiceImpl"%>
<%@page import="jsp2.service.UserService"%>
<%@page import="jsp2.po.User" %>
<%@ 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>
<%
request.setCharacterEncoding("UTF-8");
String uName = request.getParameter("name");// obtain name value
String uPwd = request.getParameter("pwd");// obtain pwd value
UserService userService = new UserServiceImpl();
User user=userService.findUser(new User(uName,uPwd));// call findUser Method , The user login
if(user!=null){
// Login successful
session.setAttribute("info", user);
// Set up session value , Put the data you want in session From here on out
response.sendRedirect("success.jsp");
}else{
// Login failed
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
reg.jsp
Registration page , For registering users , After successful registration, jump to login page , Failure to return reg page .
<%@ 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 4 Within characters " maxlength="4"></td>
</tr>
<tr>
<th> Input password :</th>
<td><input type="password" name="pwd" id="password"
placeholder=" Input 12 Within characters " maxlength="12"></td>
</tr>
<tr>
<th> Confirm the password :</th>
<td><input type="password" name="nwd" id="newword"
placeholder=" Reenter the password " maxlength="12"></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>
index2.jsp
Register to detect , call UserService Medium java Code ( Method ), Implement user registration
<%@page import="jsp2.service.impl.UserServiceImpl"%>
<%@page import="jsp2.service.UserService"%>
<%@page import="jsp2.po.User" %>
<%@ 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>
<%
request.setCharacterEncoding("UTF-8");
String uName = request.getParameter("name");// Get output name value
String uPwd = request.getParameter("pwd");// Get output pwd value
UserService userService = new UserServiceImpl();
boolean fg = userService.isReg(new User(uName,uPwd));// call isReg Method
if(fg){
// Registered successfully
userService.addUser(new User(uName,uPwd));// Registered users
response.sendRedirect("login.jsp");
}else{
// Registration failed
response.sendRedirect("reg.jsp");
}
%>
</body>
</html>
success.jsp
After logging in successfully, jump to the page
<%@page import="jsp2.po.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Login successful </title>
</head>
<body>
<center>
<h3>
<%
if(session.getAttribute("info")==null){
response.sendRedirect("login.jsp");
}else{
User user=(User) session.getAttribute("info");
%>
Welcome :<%=user.getUsername() %>
<%
}
%>
<br/>
<a href="out.jsp"> Cancellation </a>
</h3>
</center>
</body>
</html>
out.jsp
Log off the currently logged in user , Exit the login success page , You need to log in again to enter the page
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Cancellation </title>
</head>
<body>
<%
session.removeAttribute("info"); // To empty the current session Specify the properties of the object in
response.sendRedirect("login.jsp");// Redirect ( Page Jump )
%>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>jsp2</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- That is to say, the longest interval between the client and the server is 2 minute , 2 Minutes later session.getAttribute() The value obtained is null -->
<session-config>
<session-timeout>2</session-timeout>
</session-config>
</web-app>
边栏推荐
- gorm事务体验
- IP datagram sending and forwarding process
- 北斗三号短报文终端在大坝安全监测方案的应用
- [Verilog quick start of Niuke online question brushing series] ~ one out of four multiplexer
- Informatics Orsay all in one 1360: strange lift
- Lumiprobe细胞成像分析:PKH26 细胞膜标记试剂盒
- 【C语言练习——打印空心正方形及其变形】
- MySQL export query results to excel file
- WordPress zibll sub theme 6.4.1 happy version is free of authorization
- Is it enough for the project manager to finish the PMP? no, it isn't!
猜你喜欢

Wedding studio portal applet based on wechat applet

CPG 固体支持物研究:Lumiprobe通用 CPG II 型

To batch add background pictures and color changing effects to videos

二级造价工程师证书含金量到底有多高?看这些就知道了

Online yaml to JSON tool

Docker installs mysql5.7 and starts binlog

Function reentry caused by Keil C51's data overlaying mechanism

店铺进销存管理系统源码

Dart learning - functions, classes

WordPress zibll sub theme 6.4.1 happy version is free of authorization
随机推荐
关系数据库与文档数据库对比
8VC Venture Cup 2017 - Elimination Round D. PolandBall and Polygon
[JVM] - Division de la mémoire en JVM
Liuhaiping's mobile phone passes [[uiapplication sharedapplication] delegate] window. safeAreaInsets. The height of the bottom security zone is 0
Can wechat applets import the data in the cloud development database into makers with one click in the map component
Docker installs mysql5.7 and starts binlog
Learn Taiji Maker - mqtt Chapter 2 (IV) esp8266 reserved message application
It is the latest weapon to cross the blockade. It is one of the fastest ladders.
Disable right-click, keyboard open console events
Voltage mode and current mode control of switching power supply
指定默认参数值 仍报错:error: the following arguments are required:
BioVendor sRAGE抗体解决方案
How high is the gold content of grade II cost engineer certificate? Just look at this
msa. h: There is no such file or directory
Simple usage of GSAP
Oracle 条件、循环语句
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
jsp连接Oracle实现登录注册
Opencv实现目标检测
北斗三号短报文终端在大坝安全监测方案的应用