Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans Register Login System Java Server Pages Mysql Database

Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans Register Login System Java Server Pages Mysql Database

This is a jsp application fot registering users. Registered users are saved in mysql database using Java Server Pages (JSP) server side programming language.

Registration Page

This page collects input from a user and inserts them to mysql database on submit button click.

SOURCE CODE

Index.html

<!DOCTYPE html>
<!--
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans 
Register Login System Java Server Pages Mysql Database.
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans Register Login System Java Server Pages Mysql Database
--> <html> <head> <title>New registration</title> </head> <body> <!--HTML Form--> <form action="process.jsp" method="get"> <!--Name input field--> Name :<input type="text" name="fname" /><br><br> <!--cnic text input field--> CNIC :<input type="text" name="cnic" /><br><br> <!--username input field--> User name :<input type="text" name="userName" /><br><br> <!--email input field--> Email ID :<input type="text" name="email" /><br><br> <!--password inout field--> password :<input type="password" name="password" /><br><br> <!--Submit button--> <input type="submit" name="submit" value="Register"/><br><br> <!--Login link--> <p>Already signed Up ? <a href="login.jsp">Login Here</a></p> </form> </body> </html>

Login Page

Once the user has been registered she/he can use the login page to gain access to users page and view retrieved data from mysql database.

SOURCE CODE

Login.jsp

<%-- 
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans 
Register Login System Java Server Pages Mysql Database.
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans Register Login System Java Server Pages Mysql Database
Document : login Created on : May 30, 2021, 6:46:10 PM Author : HP --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>Login</title> </head> <body> <!--HTML form--> <form action="process.jsp" method="post"> <!--username text input field--> User name :<input type="text" name="username" /><br><br> <!--password text input field--> password :<input type="password" name="password" /><br><br> <!--submit input field--> <input type="submit" name="submit" value="Login" /> <br><br> </form> <!--signup link--> <p>New user. <a href="index.html">Sign Up Here</a></p> </body> </html>

USER HOME PAGE

After successful login a user is redirected to this page. Here users can update their credentials and other account details. Changes made on this page also reflects in the database (Mysql database). Also data from mysql database is retrieved and displayed on this page using SQL “SELECT” Queries.

READ MORE  How To Download And Install Kali Linux ISO On Oracle Virtualbox

SOURCE CODE

USER.JSP

<%-- 
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans 
Register Login System Java Server Pages Mysql Database.
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans Register Login System Java Server Pages Mysql Database
Document : user Created on : May 30, 2021, 7:42:04 PM Author : HP --%> <% //Check If Session Exists. HttpSession sessions = request.getSession(false); if ((sessions == null) || (session.getAttribute("userNameSession") == null)) { response.sendRedirect("login.jsp"); return; // no need to process further (if in a filter no need to go down the chain) } %> <%@page import="java.sql.*"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>USERS PAGE!</h1> <% String sessionName = session.getAttribute("userNameSession").toString(); String name = ""; String userN = ""; String cnic = ""; String email = ""; String pass = ""; //Creating Connection Object. Connection conn = null; //Printind welcome message using session name. out.println("Welcome :" + sessionName); try { //Checking Myaql Connector Class.forName("com.mysql.jdbc.Driver"); //Creating jdbc mysql Connection by using conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/registrationloginformjsp?zeroDateTimeBehavior=convertToNull", "root", ""); //PreparedStatement PreparedStatement pst = conn.prepareStatement("SELECT * FROM users WHERE UserName=?"); pst.setString(1, sessionName); //ResultSet ResultSet rs = pst.executeQuery(); if (rs.next()) { out.println(""); name = rs.getString("Name"); userN = rs.getString("UserName"); cnic = rs.getString("CNIC"); email = rs.getString("Email"); pass = rs.getString("Password"); //Printing data hrom mysql database on webpage. out.println("Name : " + rs.getString("UserName") + " " + "CNIC " + rs.getString("CNIC")); } else { out.println("Invalid login credentials"); } } catch (Exception e) { System.out.print(e); e.printStackTrace(); } finally { if (conn != null) { try { //Closing Connection. conn.close(); } catch (Exception e) { System.out.print(e); e.printStackTrace(); } } %> <br><br> <!--HTML Form--> <form action="process.jsp" method="get"> <!--Name input field--> Name :<input type="text" name="fname" value="<%= name%>"/><br><br> <!--cnic text input field--> CNIC :<input type="text" name="cnic" value="<%= cnic%>"/><br><br> <!--email input field--> Email ID :<input type="text" name="email" value="<%= email%>"/><br><br> <!--password inout field--> password :<input type="password" name="password" value="<%= pass%>"/><br><br> <!--Submit button--> <input type="submit" name="submit" value="Update"/><br><br> <!--username input field--> <input type="hidden" name="un" value="<%= userN%> "/><br><br> <p>Want To Logout ? <a href="logout.jsp">Logout</a></p> </form> </body> </html>

LOGOUT PAGE

This page destroys all the sessions that had been previously created by this program.

READ MORE  15 Best Truecaller: Caller ID & Block Features: Tips And Tricks You Should Know

SOURCE CODE

Logout.php

&lt;%--
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans
Register Login System Java Server Pages Mysql Database.
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans Register Login System Java Server Pages Mysql Database
<pre><code>Document : logout Created on : May 30, 2021, 9:25:48 PM Author : HP</code></pre> --%&gt; <%@page contentType="text/html" pageEncoding="UTF-8"%> &lt;% session.invalidate(); <pre><code>out.println("&lt;script&gt;"); out.println("alert(\"Whole Session Deleted\")"); out.println("location=\"login.jsp\";"); out.println("&lt;/script&gt;");</code></pre> %&gt;

ACTION/PROCESSING PAGE

This is where all form data(input fields) is processed. Sql queries are executed in this page. First database connection is created and data is manipulated accordingly using appropriate SQL queries.

SOURCE CODE

PROCESS.JSP

<%--
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans 
Register Login System Java Server Pages Mysql Database.
Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans Register Login System Java Server Pages Mysql Database
Document : reg-process Created on : May 30, 2021, 4:24:37 PM Author : HP --%> <%@page import="java.sql.*" %> <% //Creating HttpSession Object. HttpSession sessions = request.getSession(); //Getting Submit button value. String action = request.getParameter("submit"); //Checking Submit button value. if (action.equals("Register")) { String nname = request.getParameter("fname"); String cnic = request.getParameter("cnic"); String userName = request.getParameter("userName"); String email = request.getParameter("email"); String password = request.getParameter("password"); Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/registrationloginformjsp?zeroDateTimeBehavior=convertToNull", "root", ""); Statement st = conn.createStatement(); int i = st.executeUpdate("INSERT INTO `users`(`Name`, `UserName`, `CNIC`, `Email`, `Password`) VALUES ('" + nname + "','" + userName + "','" + cnic + "','" + email + "','" + password + "')"); if (i > 0) { out.println("Thank you for register ! Please <a href='login.jsp'>Login</a> to continue."); } } catch (Exception e) { System.out.print(e); e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { System.out.print(e); e.printStackTrace(); } } } } else if (action.equals("Login")) {//Checking Submit button value. String username = request.getParameter("username"); String password = request.getParameter("password"); Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/registrationloginformjsp?zeroDateTimeBehavior=convertToNull", "root", ""); PreparedStatement pst = conn.prepareStatement("SELECT * FROM users WHERE UserName=? and Password=?"); pst.setString(1, username); pst.setString(2, password); ResultSet rs = pst.executeQuery(); if (rs.next()) { sessions.setAttribute("userNameSession", rs.getString("UserName")); out.println("<script>"); out.println("alert(\"logged in\")"); out.println("location=\"user.jsp\";"); out.println("</script>"); } else { out.println("Invalid login credentials"); } } catch (Exception e) { System.out.print(e); e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { System.out.print(e); e.printStackTrace(); } } } } else if (action.equals("Update")) {//Checking Submit button value. String name = request.getParameter("fname"); String cnic = request.getParameter("cnic"); String userName = request.getParameter("un"); String email = request.getParameter("email"); String password = request.getParameter("password"); Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/registrationloginformjsp?zeroDateTimeBehavior=convertToNull", "root", ""); String updateString = "UPDATE users SET Name=?,CNIC=?,Email=?,Password=? WHERE UserName = ?"; PreparedStatement pst = conn.prepareStatement(updateString); pst.setString(1, name); pst.setString(2, cnic); pst.setString(3, email); pst.setString(4, password); pst.setString(5, userName); int i = pst.executeUpdate(); if (i > 0) { out.println("<script>"); out.println("alert(\"UPDATED !\")"); out.println("location=\"user.jsp\";"); out.println("</script>"); } } catch (Exception e) { System.out.print(e); e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { System.out.print(e); e.printStackTrace(); } } } } %>

phpMyAdmin SQL Dump

-- phpMyAdmin SQL Dump
-- version 5.1.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: May 31, 2021 at 12:51 PM
-- Server version: 10.4.19-MariaDB
-- PHP Version: 8.0.6

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `registrationloginformjsp`
--
CREATE DATABASE IF NOT EXISTS `registrationloginformjsp` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `registrationloginformjsp`;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `Name` varchar(50) NOT NULL,
  `UserName` varchar(50) NOT NULL,
  `CNIC` varchar(30) NOT NULL,
  `Email` varchar(50) NOT NULL,
  `Password` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `Name`, `UserName`, `CNIC`, `Email`, `Password`) VALUES(5, 'Johnathan', 'Andrew', '77777777777777777', 'Andrew@email.com', '123');
INSERT INTO `users` (`id`, `Name`, `UserName`, `CNIC`, `Email`, `Password`) VALUES(6, 'Jane', 'Jane', '9798UIUI8787', 'Jane@gmail.com', '1234');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `UserName` (`UserName`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

DATABASE DESIGN

Insert, View And Update Jsp Xampp PhpMyAdmin Netbeans Register Login System Java Server Pages Mysql Database

DEMO VIDEO

Leave a Reply

Your email address will not be published. Required fields are marked *