Hidden Form Field is a session tracking technique in Servlet where hidden input fields are used to store and transfer client information from one page to another. These fields are not visible to the user but their values are sent to the server when the form is submitted.
- Hidden fields help maintain user data between multiple requests.
- The data is transferred as request parameters.
- Commonly used when cookies are disabled.
Syntax:
<input type=hidden name="name" value="value">
Here,
- type: "hidden" creates an invisible input field.
- name: is a hidden box name or Request parameter Name.
- value: is a hidden box value or Request parameter value.
Use case of Hidden Form Field
- Passing user ID between pages.
- Maintaining session information.
- Storing visit time or page information.
- Passing values without displaying them to the user.
Note: Hidden form fields are suitable for small and non-sensitive data because the values can be viewed using the browser's page source option.
Example to show the user name and the visited time.
Step 1: Create home.jsp
The home.jsp page contains a form that accepts the username and stores the current visit time in a hidden field.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home Page</title>
</head>
<body>
<%
java.util.Date today= new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("hh:mm;ss");
String str =sdf.format(today);
%>
<form action ="welcome.jsp" metgod="post">
Enter your name:<input type="text" name="username"/>
<input type="hidden" value="<%=str%>" name="visittime"/>
<br>
<input type="submit" value="Show Message"/>
</form>
</body>
</html>
Step 2: Create welcome.jsp
The welcome.jsp page retrieves the username and visit time using request.getParameter().
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name=request.getParameter("username");
String time=request.getParameter("visittime");
%>
<h3>Hello <%=name%>,Welcome to our Page !</h3>
You visited Home page at <%= time %>
</body>
</html>
Step 3: Run the JSP Application
- First, deploy and run the project on the server such as Apache Tomcat.
- Open the following URL in the browser
http://localhost:2021/HiddenFieldDemo/home.jsp

The entered username and the hidden visit time field are both sent to the server when the form is submitted.

Output:
After submitting the form, the welcome.jsp page displays the username entered by the user along with the visit time stored in the hidden form field.

Explanation:
- The username entered in the form.
- The time at which the user visited the home page.
- The hidden field value is retrieved using request.getParameter("visittime") and displayed on the page.
Advantages of Hidden Form Field
- Simple and easy to implement.
- Works even when cookies are disabled.
- Requires only basic HTML knowledge.
- Supported by technologies like Servlet, JSP, PHP, and ASP.NET.
Disadvantages of Hidden Form Field
- Hidden data is visible in the page source.
- Can store only text/string values.
- Increases network traffic because data is sent with every request.
- Not suitable for storing sensitive information.