JSP Login & Registration Form

Hi frnds, here I am going to represent a registration form and a login form in JSP. In registration form whatever we will put the value of username and password, that value when we will put in login form then login will be successful. For creating registration form, we must have a table in the database. You can write the database logic in JSP file.

Required Files:

 

  1. login.jsp
  2. reg.jsp
  3. regprocess.jsp
  4. welcome.jsp

Example of Registration Form in JSP

In this example, we are using the Oracle10gXE database to connect with the database. Let's first create the table in the Oracle database:

CREATE TABLE registration 
(
Id varchar2(100) primary key,
F_namevarchar2(100),
L_namevarchar2(100),
Email varchar2(100),
Unamevarchar2(100),
Password varchar2(100)
);

We have created the table named registration here.

login.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="green">
<form method=”post”action=”loginprocess.jsp”>
<center>
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<thcolspan="2">Login Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
<tr>
<td colspan="2">Yet Not Registered!! <a href="reg.jsp">Register Here</a></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>

 

reg.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Form</title>
</head>
<body bgcolor="green">
<form method="post" action="registration.jsp">
<center>
<table border="1" width="30%" cellpadding="5">
<thead>
<tr>
<thcolspan="2"><h2>Registration Form</h2></th>
</tr>
</thead>
<tbody>
<tr>
<td>First Name</td>
<td><input type="text" name="fname" value="" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname" value="" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="" /></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
<tr>
<td colspan="2">Already registered!! <a href="index.jsp">Login Here</a></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>

 

regprocess.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
    String user = request.getParameter("uname");   
    String pwd = request.getParameter("pass");
    String fname = request.getParameter("fname");
    String lname = request.getParameter("lname");
    String email = request.getParameter("email");
int i=0;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con = null;
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
            "system", "oracle");
    Statement st=con.createStatement();
    i=st.executeUpdate("insert into registrationdemo (ID,F_NAME,L_NAME,EMAIL,UNAME,PASS) values (seq_registration.nextval,'" + fname + "','" + lname + "','" + email + "','" + user + "','"
                                                                   + pwd +"')");
          if (i > 0) {
          response.sendRedirect("welcome.jsp");
    }
else{
response.sendRedirect("index.jsp");}
    }
catch(Exception e){e.printStackTrace();}
%>
</body>
</html>

 

welcome.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>welcome</title>
</head>
<body>
<center><h1>Registration is Successful.</h1></center>
Please Login Here <a href='login.jsp'>Go to Login</a>
</body>
</html>

 

loginprocess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import ="java.sql.*" %>
<%@ page import ="oracle.jdbc.driver.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
    String user = request.getParameter("uname");   
    String pwd = request.getParameter("pass");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
            "system", "oracle");
    Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from registration where uname='" + user + "' and pass='" + pwd + "'");
if (rs.next()) {
session.setAttribute("user", user);
response.sendRedirect("success.jsp");
    } else {
out.println("Invalid password <a href='login.jsp'>try again</a>");
    }
    }
catch(Exception e){}
%>
</body>
</html>

 

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment