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.
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
- It will always work whether cookie is disabled or not.
Disadvantage of Hidden Form Field:
- It is maintained at server side.
- Extra form submission is required on each pages.
- 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.
index.html
FirstServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String n=request.getParameter("userName"); out.print("Welcome "+n); //creating form that have invisible textfield out.print("
“); out.close(); }catch(Exception e){System.out.println(e);} } }
SecondServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SecondServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); //Getting the value from the hidden field String n=request.getParameter("uname"); out.print("Hello "+n); out.close(); }catch(Exception e){System.out.println(e);} } }
web.xml
s1 FirstServlet s1 /servlet1 s2 SecondServlet s2 /servlet2
Leave A Comment