getParameter() - Passing data from client to 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’.

The getParameter() method of JSP takes an argument and retrieves data associated with it from the source and further pass it to the destination. The source could be an HTML or JSP page and the destination could be another JSP page.

Syntax

request.getParameter("source");

Steps of Passing data from client to JSP using getParameter()

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 ‘Parameter’.

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 −

Example

<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<!DOCTYPE html>
<html>
   <head>
      <title> Tutorials Point </title>
      <meta charset = "UTF-8">
      <meta name = "viewport" content = "width = device-width, initial-scale=1.0">
      <style>
         input{
            margin: 10px;
         }
         body {
            background-color : #FC4BE9;
            text-align : center;
         }
      </style>
   </head>
   <body>
      <h1> Welcome to Tutorials Point </h1>
      <form action = "Data.jsp" method = "POST">
         <label> Enter Data1: </label>
         <input type = "text" name = "data1">
         <br>
         <label>Enter Data2: </label>
         <input type = "text" name = "data2">
         <br>
         <input type = "submit">
      </form>
   </body>
</html> 

The above code will create the web form where a user can enter data in the given text fields, later these data will be retrieved by Data.jsp file. This file name is mentioned in the action attribute of <form>. We have used the <form> tag which is used to accept input from the client. 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 Data.jsp. To create, right-click on your project folder then New  JSP.

Step 8

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

Data.jsp −

Example

<%@page contentType = "text/html" pageEncoding = "UTF-8"%>
<!DOCTYPE html>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">     
      <title> Tutorials Point </title>
   </head>
   <body>
      <h1> Example of Get Parameter </h1>
      <h2> Client has Entered </h2>
      <% String data1 = request.getParameter("data1"); %>
      <h3>Data1: <% =data1 %> </h3>
      <% String data2 = request.getParameter("data2"); %>
      <h3>Data2: <% =data2 %> </h3>
   </body>
</html>

In the above code, we have used request.getParameter() method to retrieve the data that client will enter in the text field of index.jsp file. We first store the information in two String variables and then display them by using the opening and closing tags of JSP.

Now, Run the code.

Output

The following snapshots represent the outputs of the application −


We have passed Tutorials and Point as data from client side to JSP. After clicking ‘Submit’ button the client will be redirected to the following window where the entered text will be displayed on the screen:

Conclusion

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 built an application to pass data from the client to a JSP page using getParameter() method. Also, we discovered the process of creating JSP pages in Netbeans IDE.

Updated on: 21-Jul-2023

225 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements