Open In App

Servlet – Hidden Form Field

The Server embeds new hidden Fields in every dynamically generated From page for the client. when the client submits the form to the server the hidden fields identify the client. Hidden Box is an invisible text box of the form page, hidden box value goes to the server as a request parameter when the form is submitted.

Syntax for hidden fields:



<input type=hidden name=”name” value=”value”>

  • name: is a hidden box name or Request parameter Name.
  • value: is a hidden box value or Request parameter value.

Advantages of using Hidden Form Field

Disadvantages of using Hidden Form Field

Example

In this example, we are showing the user name and the visited time.



home.jsp




<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>Home Page</title>
   </head>
  
   <body>
       <%
           java.util.Date today= new java.util.Date();
           java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("hh:mm;ss");
           String str =sdf.format(today);
         %>
  
         <form action ="welcome.jsp" metgod="post">
             Enter your name:<input type="text" name="username"/>
             <input type="hidden" value="<%=str%>" name="visittime"/>
             <br>
             <input type="submit" value="Show Message"/>    
          </form>
   </body>
  
</html>

Welcome.jsp




<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSP Page</title>
   </head>
  
   <body>
       <%
           String name=request.getParameter("username");
           String time=request.getParameter("visittime");
        %>
        <h3>Hello <%=name%>,Welcome to our Page !</h3>
        You visited Home page at <%= time %>
   </body>
  
</html>

Output:

 

                                               


Article Tags :