×
☰ See All Chapters

Hidden Form Field in Servlet

In Hidden Form Field technique, we write the response to client such that if client makes a request again against a response some value will be sent to server together with the request unknowingly. When Hidden Form Field is used for session management we have to write the response such that we have to move values explicitly between client and server. In case of Hidden Form Field a hidden (invisible) text field is used for maintaining the state of a user. In such case, we store the information in the hidden field and get it from another servlet. This approach is better if we have to submit form in all the pages and we don't want to depend on the browser.

Example for hidden form field

Project directory structure

hidden-form-field-0
 

home.html

<!DOCTYPE html>

<html>

<body>

        <form action="../servlet1" method="post">

                Enter Personal Details:<br /> Name:<input type="text" name="name" /><br />

                <input type="submit" value="go" />

        </form>

</body>

</html>

FirstServlet.java

package com.java4coding;

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class FirstServlet extends HttpServlet {

        public void doPost(HttpServletRequest request, HttpServletResponse response) {

                try {

                        response.setContentType("text/html");

                        PrintWriter out = response.getWriter();

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

                        out.print("Welcome " + name);

                        out.print("<form action='servlet2' method='post'> ");

                        out.print("Enter Contact Details:<br/>");

                        out.print("Address:<input type='text' name='address'/><br/>");

                        out.print("<input type='hidden' name='name' value='" + name + "'>");

                        out.print("<input type='submit' value='go'/> ");

                        out.print("</form>");

                        out.close();

                } catch (Exception e) {

                        System.out.println(e);

                }

        }

}

SecondServlet.java

package com.java4coding;

 

import java.io.*;

import javax.servlet.http.*;

 

public class SecondServlet extends HttpServlet {

        public void doPost(HttpServletRequest request, HttpServletResponse response) {

                try {

                        response.setContentType("text/html");

                        PrintWriter out = response.getWriter();

                        String address = request.getParameter("address");

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

                        out.print("<form action='servlet3' method='post'> ");

                        out.print("Enter Eduction Details:<br/>");

                        out.print("Education:<input type='text' name='education'/><br/>");

                        out.print("<input type='hidden' name='name' value='" + name + "'>");

                        out.print("<input type='hidden' name='address' value='" + address + "'>");

                        out.print("<input type='submit' value='go'/> ");

                        out.print("</form>");

                        out.close();

                } catch (Exception e) {

                        System.out.println(e);

                }

        }

}

ThirdServlet.java

package com.java4coding;

 

import java.io.*;

 

import javax.servlet.*;

import javax.servlet.http.*;

 

public class ThirdServlet extends HttpServlet {

        public void doPost(HttpServletRequest request, HttpServletResponse response) {

                try {

                        response.setContentType("text/html");

                        PrintWriter out = response.getWriter();

                        out.print("Name: " + request.getParameter("name") + "</br>");

                        out.print("Address: " +request.getParameter("address") + "</br>");

                        out.print("Education: " + request.getParameter("education"));

                        out.close();

                } catch (Exception e) {

                        System.out.println(e);

                }

        }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:web="https://xmlns.jcp.org/xml/ns/javaee">

  <servlet>

    <servlet-name>s1</servlet-name>

    <servlet-class>com.java4coding.FirstServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>s1</servlet-name>

    <url-pattern>/servlet1</url-pattern>

  </servlet-mapping>

  <servlet>

    <servlet-name>s2</servlet-name>

    <servlet-class>com.java4coding.SecondServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>s2</servlet-name>

    <url-pattern>/servlet2</url-pattern>

  </servlet-mapping>

  <servlet>

    <servlet-name>s3</servlet-name>

    <servlet-class>com.java4coding.ThirdServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>s3</servlet-name>

    <url-pattern>/servlet3</url-pattern>

  </servlet-mapping>

</web-app>

Output

hidden-form-field-1
 

All Chapters
Author