Share on Google+Share on Google+

Java Servlet : Hidden Field

In this tutorial, we will discuss about Hidden Field. It is one of session tracking technique.

Java Servlet : Hidden Field

In this tutorial, we will discuss about Hidden Field. It is one of session tracking technique.

Hidden Field :

Session tracking is a technique to maintain about the requests of a same user for a specific time period. You can maintain session tracking in three ways - Hidden form field, Cookies, URL rewriting.

Hidden form field is a way of session tracking so that you can save the information in client browser itself. For that you can use hidden field for securing fields.
So at the time of page display, no one can see these fields. When client submit the form, the browser also transfer these hidden value to the server with other fields.

You can secure your field as -

<input type =?hidden? name =?name? value=?value?/>

In servlet you can get hidden form field as -

String name= request.getParameter(?name?);

Example :  In this example, we are mentioning two form field hidden which are name and location in HiddenFieldPage.jsp and accessing these field in to our servlet

HiddenFieldPage.jsp -


<title>Hidden Field Example</title>
<h2>Click on Submit to check hidden field </h2>
<form action="HiddenFieldServlet" method="post"><input
type="hidden" name="name" value="Roseindia"> <input
type="hidden" name="location" value="New Delhi"> <input
type="submit" value="Submit"></form>
</html> -


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HiddenFieldServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();

String name = request.getParameter("name");
String location = request.getParameter("location");

out.println("<form action=HiddenFieldPage.jsp>");
out.println("<b>Company Name : </b>" + name);
out.println("<br><b>Locaion : </b>" + location);
out.println("<br><br><b>Click on GoBack to go on JSP page </b>");
out.println("<br><input type=submit value='GoBack'>");




web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
xmlns="" xmlns:web=""
id="WebApp_ID" version="2.5">


Output :


Posted on: October 8, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null