Java Servlet Hidden Field Example

A hidden form field is a technique used to store the session information for a particular client and is one of an important Session Tracking Technique. In this tutorial, we will explain and show you how to handle the hidden fields in a Servlet.

1. Introduction

Session Tracking is a technique to maintain the requests of the same user for a specific time-period. Developers can maintain the session tracking in three ways:

  • Hidden Form Field
  • Cookies
  • URL Rewriting

A hidden form field is a way of session tracking so that developers can save the information in the client browser itself. For that, developers can use a hidden field for securing the fields. So at the time of page display, no one can see these fields. When a client submits the form, the browser also transfers these hidden value to the server with the other fields. Developers can secure the field as:

Syntax of Hidden Field

<input type="hidden" name="testHidden" value="testHiddenValue" />

In Servlet, developers can get the Hidden Form Field as:

Syntax of Request Parameter

String param1 = request.getParameter("testHidden");

1.1 Advantages

  • Does not have to depend on the browser whether the cookie is disabled or not.
  • Easier implementation.
  • Hidden boxes reside in the web pages of the browser window so they do not provide a burden to the server.

1.2 Disadvantages

  • Extra form submission is required on every page which is a big overhead.
  • Maintained at the server side.
  • More complex than URL rewriting.
  • Only textual information can be used.
  • Hidden boxes do not provide data security because their data can be viewed through the view source option.
  • It increases the network traffic because the hidden data travels over the network along with the request and response object.

Now, open up the Eclipse Ide and let’s see how to implement the Hidden Form field in a sample Java-based web application.

2. Java Servlet Hidden Field Example

In this example, we are mentioning the two Hidden Form fields (i.e. name and location) in the index.jsp and accessing these fields into our Servlet.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
	    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	    <title>Servlet 3.0 Hidden Field Example</title>
	    <style type="text/css">
	    	.paddingBtm {
	    		padding-bottom: 12px;
	        <h2>Click the Submit button to check the hidden field</h2>
	        <form name="hiddenForm" method="post" action="hiddenFieldServlet">	        		        		        	
	            <div class="paddingBtm">
	            	<input id="nameId" type="hidden" name="name" value="JavaCodeGeek" /> 
	            <div class="paddingBtm">
	            	<input id="locationId" type="hidden" name="location" value="Greece" />
	            <div id="loginBtn">
	            	<input id="btn" type="submit" value="Submit" />

Now in order to retrieve the hidden field value in a Servlet, we will use the HttpServletRequest interface’s getParameter() method.

package com.jcg.servlet;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HiddenFieldServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	// This Method Is Called By The Servlet Container To Process A 'POST' Request.
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		handleRequest(req, resp);

	public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {


		// Post Parameters From The Request
		String param1 = req.getParameter("name");
		String param2 = req.getParameter("location");

		// Print The Response
		PrintWriter out = resp.getWriter();
		out.println("<form action='index.jsp'>");
		out.println("<strong>Company Name: </strong>" + param1);
		out.println("<br><strong>Location: </strong>" + param2);
		out.println("<br><br><i>Click on 'GoBack' button to go on JSP page</i>");
		out.println("<br><input type='submit' value='GoBack'>");


3. Project Deploy & Run

As we are ready for all the changes, let us compile the project and deploy the application on the Tomcat7 server. Open your favorite browser and hit the following URL.


The output page will be displayed.

Fig. 1 - Application Output
Fig. 1 – Application Output

Click on Submit button.

Fig. 2: Hidden Form Fields Displayed
Fig. 2: Hidden Form Fields Displayed

That’s all for this post. Happy Learning!!

4. Conclusion

In this section, developers learned how to handle the hidden fields in a Servlet. Developers can download the sample application as an Eclipse project in the Downloads section. I hope this article served you with whatever developers were looking for.

5. Download the Eclipse Project

This was an example of Servlet application.

You can download the full source code of this example here: JavaServletHiddenFieldEx


An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button