Back to: JSP Tutorials for Beginners and Professionals
Registration and Login Form in JSP
In this article, we will see how to develop the Registration and Login Form in JSP. To develop a registration form we will need to connect our JSP application with the database. Here we are using the MySQL database. First, we will create a database name students. The SQL statement will be: Create database students;
Now, create the users table inside the students database as follows:
CREATE TABLE users (
fname varchar(45),
lname varchar(45),
email varchar(100),
userid varchar(45),
password varchar(45)
);
In this example, we will create four pages:
- Index.html
- Login.jsp
- Register.html
- registrationProcess.jsp
In the index.html page, we are giving two fields where the user will enter his/her “username” and “password” in case the user is already registered. If the user not registered, the user needs to click on the “Login Here” link available. After that user will be redirected to the register.html page where the user has to enter his/her credentials which will redirect to registrationProcess.jsp. This JSP page will save the entered record into the “users” table in the “students” database. After successful registration user can click on the “Login” link which will redirect the user to the index.html page. Where the user can enter the credentials, which will redirect the user to login.jsp page.
index.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="login.jsp" method="post"> User name :<input type="text" name="usr" /><br><br> password :<input type="password" name="password" /><br> <br><input type="submit" /> </form> <p> New user. <a href="register.html">Login Here</a> </body> </html>
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="java.sql.*,java.util.*"%> <% String userid = request.getParameter("userid"); session.putValue("userid", userid); String password = request.getParameter("password"); Class.forName("com.mysql.jdbc.Driver"); java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", "root"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from users where userid='" + userid + "' and password='" + password + "'"); try { rs.next(); if (rs.getString("password").equals(password) && rs.getString("userid").equals(userid)) { out.println("<h2>Welcome " + fname "</h2>"); } else { out.println("Invalid password or username."); } } catch (Exception e) { e.printStackTrace(); } %>
register.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="registrationProcess.jsp" method="post"> First name :<input type="text" name="fname" /> <br><br>Last name :<input type="text" name="lname" /> <br><br>Email ID :<input type="text" name="email" /><br><br> User name :<input type="text" name="userid" /><br><br> password :<input type="password" name="password" /> <br><br><input type="submit" /> </form> </body> </html>
registrationProcess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="java.sql.*,java.util.*"%> <% String fname = request.getParameter("fname"); String lname = request.getParameter("lname"); String email = request.getParameter("email"); String userid = request.getParameter("userid"); String password = request.getParameter("password"); try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", "root"); Statement st = conn.createStatement(); int i = st.executeUpdate("insert into users(fname,lname,email,userid,password)values('" + fname + "','" + lname + "','" + email + "','" + userid + "','" + password + "')"); out.println("Thank you for register ! Please <a href='index.html'>Login</a> to continue."); } catch (Exception e) { System.out.print(e); e.printStackTrace(); } %>
Output
Run your index.html page, you will get the following output:
If you are a new user click on the “Login Here” link, you will get the following page where you have to enter the details and click on the “Submit” button.
After clicking on the “Submit” button, you will get the following output. Now to log in click on the “Login” link.
After clicking on the link you will get the following output, where you can enter your credentials to log in.
After entering the correct credentials, you will get the following output:
In the next article, I am going to discuss How to develop a Page Redirection Application in JSP. Here, in this article, we develop the Registration and Login Form in the JSP application which will register the user into the database table and I hope you enjoy this article.