Hidden Form Field in JSP

Introduction

This article explains how to read hidden form fields in JSP. The Netbeans IDE is used to create the sample application.

Hidden form fields

This field is basically used for maintaining the session. In hidden form fields the HTML entry will be like this.

<input type="hidden" name="SomeName" value="SomeValue">

This field is embedded with the submit button, in other words when we click on the submit button it is encoded with a get/post request of the form page and sends it to another page. If we want this information then we call it using the "request.getParameter" attribute.

This field plays and important role in web development; for example it is used to maintain a session, used in authentication, etcetera.

Let's start creating this app

Use the following procedure to create this sample app.

Step 1

Open the NetBeans IDE.

NetBeans IDE

Step 2

Choose "Java web" -> "web application" as shown below.

Java Web App

Step 3

Type your project name as "GetHiddenFormApp" as in the following.

ProjectName

Step 4

Now choose your Java version and server wizard as shown below.

Server and Version wizard

Step 5

Now replace the default code of the "index.jsp" file with following code.

index.jsp

<!DOCTYPE html>

<html>

    <head>

        <title>Hidden Form</title>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta name="viewport" content="width=device-width">

    </head>

    <body>

        <form action="printhiddendata.jsp" method="post">

        Enter your name: <input type="text" name="uname">

        <br><input type="hidden" name="hide" value="What are you doing">

        <input type="submit" value="Go">

        </form>

    </body>

</html>

Step 6

Create another JSP page named "printhiddendata.jsp" and provide following code there.

printhiddendata.jsp

<!DOCTYPE HTML>

<html>

    <body>   

        <b>Welcome

            <% out.println(request.getParameter("uname"));

            %>

            <br>Hidden Fields Are:

            <% out.println(request.getParameter("hide"));

            %>

        </b>

    </body>

</html>

Step 7

Now our application is ready to run.

Right-click on the "index.jsp" file and select "Run". The following output will be generated.

Output

Step 8

Now provide your name; I typed in my name as in the following.

Enter-Name

Step 9

After clicking on the "submit" button you will see the following output.

Hidden Data


Similar Articles