Program to validate a user using JSP

JSP stands for Java Server Pages and is used for the purpose of developing web based applications. A single JSP page consists of HTML tags for static content and JSP tags to construct dynamic content. The JSP tags start with ‘<%’ and end with ‘%>’. We save our JSP file with the extension ‘.jsp’.

Validating a user means simply checking whether the user has entered correct login details or not. The validation process in JSP is quite simple and straightforward. This article will guide you to create a program to validate a user using JSP.

Steps to create a Program to Validate a user using JSP

To develop this application follow these steps −

Step 1

Open your Netbeans IDE and create a new Java Web Application through the following path − File -> New Project -> Java Web -> Java Web Application.

Step 2

Give a suitable name to your project and click Next. We have given ‘Validation’.

Step 3

Now, leave everything as it is and click Next.

Step 4

Click on Finish.

Step 5

Locate your default index.html file under the source package and delete it. Right click on your project folder and create another file named index.jsp through the following path − New -> JSP.

Step 6

When the index.jsp file gets created, copy and paste the below code.

index.jsp −


<% @page contentType = "text/html" pageEncoding = "UTF-8" %>
<!DOCTYPE html>
     <title> Tutorials Point </title>
     <meta charset = "UTF-8">
     <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
       Input {
         margin: 10px;
       Body {
     <div>Welcome to Tutorials Point</div>
     <form action = "login.jsp" method="POST">
       <label> Enter your Username: </label>
       <input type = "text" name = "uname">
       <label>Enter your Password: </label>
       <input type = "password" name = "pswd">
     <input type = "submit">

The above code will create the web UI where a user can enter their username and password, later it will be validated by login.jsp file. We have used the <form> tag which is used to accept input from the keyboard. Inside <input> tag we have declared the type of input and name to uniquely identify the text field.

Step 7

Now, create another JSP page name it login.jsp. Again, right click on your project folder then New -> JSP.

Step 8

Once the login.jsp file gets created, copy and paste the below code −


   String username = request.getParameter(" uname ");
   String password = request.getParameter(" pswd ");
   if(username.equals(" Tutorials ") && password.equals(" 12345 ") ) {
     out.println(" You are logged in!! ");
   } else {
     out.println(" Try Again!! Wrong Credentials ");

In the above code, we declared two string variables ‘username’ and ‘password’ which will retrieve the username and password entered by user respectively. For this, we have used an inbuilt method named ‘request.getParameter()’. Now, the if-else block performs the actual validation through ‘equals()’ method. If the user enters the username as "Tutorials" and password as "12345", the if block will get executed otherwise else block.

The following snapshots represent the outputs of the application −

When a user provides correct credentials then, the user will be redirected to the following window −

When a user provides incorrect credentials then, the user will be redirected to the following window −


We can say JSP is an extension of Java Servlet which is also a server side technology to build web applications using Java programming language. The JSP was created to eliminate the limitation of Servlets. In this article, we have created a program to validate a user through JSP.

Updated on: 16-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started