🚀 Master Java Servlets & Web Development!
Subscribe to Ram N Java for clean, beginner-friendly coding tutorials and web development guides!
👉 Click Here to Subscribe NowWhat is Session Tracking using Hidden Form Fields?
The HTTP protocol is stateless, meaning each request sent by a browser is treated as completely independent by the web server.
To maintain user state across multiple pages, Java Servlets provide several session tracking techniques.
One of the simplest and most effective approaches is using Hidden Form Fields (<input type="hidden">).
Hidden form fields let you embed invisible information inside an HTML form. When the user submits the form, the hidden parameter is sent along with visible fields to the next servlet, allowing continuous state tracking without relying on browser cookies.
Step 1: Create the First HTML Form (index.html)
The user begins by entering initial information (such as their username) in a standard form that submits to the first servlet:
Enter your Name: <input type="text" name="userName" /><br><br>
<input type="submit" value="Continue" />
</form>
Step 2: Embed Hidden Field in First Servlet (FirstServlet.java)
The first servlet retrieves the submitted name and dynamically builds a second HTML form containing a hidden field with the user's data:
PrintWriter out = response.getWriter();
out.println("<h2>Welcome " + userName + "</h2>");
out.println("<form action='SecondServlet' method='post'>");
out.println("<input type='hidden' name='uname' value='" + userName + "'/>");
out.println("Enter Email: <input type='text' name='email'/><br><br>");
out.println("<input type='submit' value='Submit Details'/>");
out.println("</form>");
Step 3: Retrieve Hidden Values in Second Servlet (SecondServlet.java)
When the second form is submitted, the second servlet extracts both the visible form field and the hidden field using standard request parameter methods:
String email = request.getParameter("email");
PrintWriter out = response.getWriter();
out.println("<h3>Submission Received Successfully!</h3>");
out.println("<p>Username: " + userName + "</p>");
out.println("<p>Email: " + email + "</p>");
Advantages & Limitations of Hidden Form Fields
- Advantage: Always works even if the user disables cookies in their browser.
- Advantage: Lightweight and does not consume server-side session memory.
- Limitation: Only works with form submissions (POST/GET via form tags); cannot track state through standard hyperlink navigation.
- Limitation: Data is visible in the page source, so sensitive credentials should never be stored in plain text.
📺 Related Session Tracking Tutorials
Learn more about session management mechanisms in Java Servlets from our channel:
Click here to watch in Youtube : https://www.youtube.com/watch?v=64hZwZ8hDs0&list=UUhwKlOVR041tngjerWxVccw&index=4
Click the below Image to Enlarge
![]() |
| Servlets : Hidden form field Demo |
![]() |
| Servlets : Hidden form field Demo |
![]() |
| Servlets : Hidden form field Demo |
![]() |
| Servlets : Hidden form field Demo |
![]() |
| Servlets : Hidden form field Demo |
![]() |
| Servlets : Hidden form field Demo |






