In this tutorial you will learn about the Servlet Hidden Form Field and its application with practical example.
Servlet Hidden Form Field
Hidden form field can be used for session management for a particular client. In this case a hidden textfield is used to maintain the state of an user. We store the information in hidden form field and get it from another servlet.
Syntax :-
The syntax which is using to store the data in hidden field is below.
1 |
<input type="hidden" name="uname" value="W3adda.com"> |
The advantage of this thing over cookie: It works even cookie is disabled.
There are also some disadvantage of this thing: We need extra form submission on each page, it maintained from server side.
Example: Let’s understand it by an example.
- html: This will be our welcome file for hidden form field.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<html> <head> <title>W3adda.com</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="FirstServlet"> Name:<input type="text" name="userName"/><br/> <input type="submit" value="go"/> </form> </body> </html> |
2. FirstServlet.java: From this servlet file we will store hidden data. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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("<form action='SecondServlet'>"); out.print("<input type='hidden' name='uname' value='"+n+"'>"); out.print("<input type='submit' value='go'>"); out.print("</form>"); out.close(); }catch(Exception e){System.out.println(e);} } } |
3. SecondServlet.java: We will get our hidden data here. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Intel */ public class SecondServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, 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);} } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> } |
Output :-
When we will run this simple code so we will get output like below images.
index.html
This is output of index.html file.
This will come when we will click on go button of index.html file.
This output comes for SecondServlet class, here we got our hidden field data.