Master java skills

Hidden form field

Hidden form field is another technique which is used for session tracking of a client. In this technique, a hidden field is used to store client state. In this case user information is stored in hidden field and retrieved in another servlet.

How to use hidden field

<input type=”hidden” name=”userName” value=”Saras Singh”>  

Pros and cons of hidden form field

  1. Hidden form field works whether or not cookies is enabled.
  2. It is maintained at server side
  3. Form submission is required
  4. Only textual informaiton can be sent

Hidden form field example

index.html

<html>
    <body>
        <form method="post" action="servletOne">
            Name:<input type="text" name="user" /><br/>
            <input type="submit" value="submit">
        </form>
    </body>
</html>

ServletOne.java

import java.io.PrintWriter;

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

public class ServletOne extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		try {

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			String userName = request.getParameter("userName");
			out.print("Welcome " + userName);

			out.print("<form action='servletTwo'>");
			out.print("<input type='hidden' name='uname' value='" + userName + "'><br>");
			out.print("<input type='submit' value='Get Value from Hidden Field'>");
			out.print("</form>");
			out.close();

		} catch (Exception e) {
			System.out.println(e);
		}
	}

}

ServletTwo.java

import java.io.PrintWriter;

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

public class ServletTwo extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			// Get the value from the hidden field
			// hidden field name is uname
			String uname = request.getParameter("uname");
			out.print("Hello " + uname);

			out.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

web.xml

<web-app>  
  
    <servlet>  
        <servlet-class>ServletOne</servlet-class>  
        <servlet-name>servletOne</servlet-name>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>servletOne</servlet-name>  
        <url-pattern>/servletOne</url-pattern>  
    </servlet-mapping>  
  
    <servlet>  
        <servlet-class>ServletTwo</servlet-class> 
        <servlet-name>servletTwo</servlet-name>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>servletTwo</servlet-name>  
        <url-pattern>/servletTwo</url-pattern>  
    </servlet-mapping>  
  
</web-app> 

Run the application and test

_____________________________________________________________

_____________________________________________________________

_____________________________________________________________

_____________________________________________________________