Session Management Using Hidden Form Field in Servlet with Example – Syntax, Advantages, Disadvantages

Learn about the Session Management Technique in Servlet i.e Hidden Form Field. Before discussing this technique in detail let’s first understand about Hidden Form Field. In this tutorial, you will get acquainted with details such as Syntax for Hidden Form Field, its Advantages, Disadvantages, and Example Program Using Hidden Form Field in Servlet.

What is a Hidden Form Field?

Hidden Form Field is a technique used to support Session Tracking. The name itself implies, these are fields added to an HTML form and are not displayed in the client browser. They are sent back to the server when the form that contains them is submitted. You include Hidden Form Fields like this.

<input name="username" type="hidden" value="btechgeeks" />

In other words, hidden form defines constant variables for a form. To a servlet receiving a submitted form, there is no difference between a hidden field and a visible field. Using hidden fields, we can rewrite our shopping cart servlets so that users can shop anonymously until checkout time.

Advantages of Hidden Form Field

Hidden fields are supported in all popular browsers, they demand no special server requirement, and they can be used with the client that hasn’t registered or logged in.

Disadvantages of the Hidden Form Field

One drawback of this technique, is that the session persists only through a sequence of dynamically generated forms. The session cannot be maintained with the static document, emailed documents, bookmarked documents, or browser shutdown.

Read More Techniques of Session Management in Servlet

Hidden Form Field Example

In this example, we are storing username and password in a hidden text field and getting this information in another servlet.

index.html file:

Session Management Using Hidden Form Field in Servlet with Example 1

HiddenFormField1.java file:

Session Management Using Hidden Form Field in Servlet with Example 2

HiddenFormField2.java file:

Session Management Using Hidden Form Field in Servlet with Example 3

Web.xml file:

Session Management Using Hidden Form Field in Servlet with Example 4

Session Management Using Hidden Form Field in Servlet with Example 5

Session Management Using Hidden Form Field in Servlet with Example 6

Session Management Using Hidden Form Field in Servlet with Example 7