Login and Registration Form in JSP
We offer you a brighter future with industry-ready online courses - Start Now!!
This article discusses the development of login and registration programs in JSP and using MVC architecture. So let us learn how to make this form and proceed ahead.
1. Login and Logout Form in JSP
This example shows a login form in JSP. Here we take username and password from the user as two fields in the form. Then we have a submit button.
When we click on the submit button, we will receive a welcome message.
There will be a logout option as well. By clicking on it, we will get redirected to the login page.
The example is as below:
login.jsp <html> <head> <title>Login Form</title> </head> <body> <h3> Login here </h3> <form action="user_login" method="post"> <table style="width: 20%"> <tr> <td>UserName</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password" /></td> </tr> </table> <input type="submit" value="Login" /></form> </body> </html>
Explanation of the code:
This is the main login page. This form transfers action to servlet user_login. The method of passing info is posted. It takes two parameters from the user. They’re username and password. There is a submit button with the value as login. On clicking it, the information will go to user_login where these values are taken into a request object.
user_login.java(servlet) package demotest; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class user_login extends HttpServlet { public user_login() { super(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if(username.isEmpty() || password.isEmpty() ) { RequestDispatcher requ = request.getRequestDispatcher("login.jsp"); requ.include(request, response); } else { RequestDispatcher requ = request.getRequestDispatcher("login_2.jsp"); requ.forward(request, response); } } }
Explanation of the code:
This code contains the necessary imports. User_login servlet extends HttpServlet. This code contains doPost() method as our method in login form is ‘POST’. Request object gets the username and password that are required for login.
Then we have used if and else. If any parameter is empty the page is redirected to login.jsp else the user is directed to login_2.jsp.
login_2.jsp <html> <head> <title>User Logged In</title> </head> <body> <table style="width: 20%"> <tr><td> <% String username = request.getParameter("username"); %> <a>Welcome user, you have logged in.</a></td></tr> <tr></tr><tr>td></td><td><a href=login.jsp"><b>Logout</b></a></td></tr> </table> </body> </html>
Explanation of the code:
In this code, we are getting the username using the request object. We can print the username optionally. A welcome message pops along with a logout link. This logout link redirects users to the initial login page. This code gives the following output:
Output:
On running Login.jsp, we get two fields that are “username” and “password” and a Login button.
When clicking the login button, we get the following message and a logout link.
On clicking logout, it redirects us to the login page.
2. Registration Form in JSP
In the Registration form, the user fills details like name, username, password, address, etc. Registration forms take data and store it in a database or cache.
In this example, we have the following fields for registration
- First Name
- Last Name
- Username
- Password
- Address
Then a submit button.
After clicking it, the details store in the database. For this, we create a database and table.
Database Creation
CREATE database dbuser ( `first name` varchar(20) NOT NULL, `last name` varchar(20) NOT NULL, `Username` varchar(20) NOT NULL, `Address` varchar(60) NOT NULL, PRIMARY KEY (`Username`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ; The details will be stored in this database. register.jsp <html> <head> <title>Registration Form</title> </head> <body> <h1>User Register Form</h1> <form action="user_register" method="post"> <table style="with: 20%"> <tr> <td>First Name</td> <td><input type="text" name="first_name" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="last_name" /></td> </tr> <tr> <td>UserName</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password" /></td> </tr> <tr> <td>Address</td> <td><input type="text" name="address" /></td> </tr> </table> <input type="submit" value="Submit" /></form> </body> </html>
Explanation of the code:
This registration form contains an action as a servlet (user_register) that will handle the request. ‘POST’ method is useful to process the request.
The inputs are first name, last name, username, password (input type as password so that password is hidden when typed), address. Then we have a submit button. It will correspond to user_register. Parameter values thus will be passed as request.
user_register.java (servlet) package demotest; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class user_register extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String first_name = request.getParameter("first_name"); String last_name = request.getParameter("last_name"); String username = request.getParameter("username"); String password = request.getParameter("password"); String address = request.getParameter("address"); if(first_name.isEmpty() || last_name.isEmpty() || username.isEmpty() || password.isEmpty() || address.isEmpty() || contact.isEmpty()) { RequestDispatcher req = request.getRequestDispatcher("register.jsp"); req.include(request, response); } else { RequestDispatcher req = request.getRequestDispatcher("register_2.jsp"); req.forward(request, response); } } }
Explanation of the code:
user_servlet extends HttpServlet. doPost() method is called as we used POST in JSP form. using request.getParameter, we are fetching the values from request i.e first_name, last_name, username, password, address.
We take an if condition to check if any of the parameters is empty or not. If any of the parameters is empty, then we redirect to the initial register.jsp page. Here we include request and response objects.
If the parameters are all filled then the else body works. We fetch requestDispatcher object using req as request object. The request is forwarded to register_2.jsp along with forwarding request and response objects.
register_2.jsp <html> <head> <title>Success Page</title> </head> <body> <h5> --DataFlair-- </h5> <a><b>Welcome User..</b></a> </body> </html>
Explanation of the code:
If we properly fill all the parameters, then we get a welcome message. The above code produces the following output.
Output:
When we run register.jsp, then we will get a user registration form and will take specified parameters. On clicking the submit button, the user will get a welcome message.
Conclusion
In this article, we learned the making of sample programs like login and logout form and registration form in JSP. We saw detailed examples with step by step explanations for the code. Thus we implemented what we have learnt till now to make these sample programs.
You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google