Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Hidden Form Field


In case of Hidden Form Field a hidden (invisible) textfield is used for maintaining the state of an 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.

Let’s see the code to store value in hidden field.

  1. <input type=“hidden” name=“uname” value=“Vimal Jaiswal”>

Here, uname is the hidden field name and Vimal Jaiswal is the hidden field value.


Real application of hidden form field

It is widely used in comment form of a website. In such case, we store page id or page name in the hidden field so that each page can be uniquely identified.


Advantage of Hidden Form Field

  1. It will always work whether cookie is disabled or not.

Disadvantage of Hidden Form Field:

  1. It is maintained at server side.
  2. Extra form submission is required on each pages.
  3. Only textual information can be used.

Example of using Hidden Form Field

In this example, we are storing the name of the user in a hidden textfield and getting that value from another servlet.

hidden

index.html

  1. <form action=“servlet1”>
  2. Name:<input type=“text” name=“userName”/><br/>
  3. <input type=“submit” value=“go”/>
  4. </form>

FirstServlet.java

  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class FirstServlet extends HttpServlet {
  5. public void doGet(HttpServletRequest request, HttpServletResponse response){
  6.         try{
  7.         response.setContentType(“text/html”);
  8.         PrintWriter out = response.getWriter();
  9.         String n=request.getParameter(“userName”);
  10.         out.print(“Welcome “+n);
  11.         //creating form that have invisible textfield
  12.         out.print(“<form action=’servlet2′>”);
  13.         out.print(“<input type=’hidden’ name=’uname’ value='”+n+“‘>”);
  14.         out.print(“<input type=’submit’ value=’go’>”);
  15.         out.print(“</form>”);
  16.         out.close();
  17.                 }catch(Exception e){System.out.println(e);}
  18.     }
  19. }

SecondServlet.java

  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class SecondServlet extends HttpServlet {
  5. public void doGet(HttpServletRequest request, HttpServletResponse response)
  6.         try{
  7.         response.setContentType(“text/html”);
  8.         PrintWriter out = response.getWriter();
  9.         //Getting the value from the hidden field
  10.         String n=request.getParameter(“uname”);
  11.         out.print(“Hello “+n);
  12.         out.close();
  13.                 }catch(Exception e){System.out.println(e);}
  14.     }
  15. }

web.xml

  1. <web-app>
  2. <servlet>
  3. <servlet-name>s1</servlet-name>
  4. <servlet-class>FirstServlet</servlet-class>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>s1</servlet-name>
  8. <url-pattern>/servlet1</url-pattern>
  9. </servlet-mapping>
  10. <servlet>
  11. <servlet-name>s2</servlet-name>
  12. <servlet-class>SecondServlet</servlet-class>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>s2</servlet-name>
  16. <url-pattern>/servlet2</url-pattern>
  17. </servlet-mapping>
  18. </web-app>

 

Comments

comments