Hidden field in Servlet

Hidden field

 
This is a process to store information present in the current request in a form field called a hidden field. Whenever a form containing a hidden field gets submitted then the destination servlet can find the value of the hidden field by using the getParameter() method of the request.
 
Example: - Servlet to accept the name of the user like the first name and last name and showing the full name using a hidden field. 
  1. import javax.servlet.*;  
  2. import javax.servlet.http.*;  
  3. import java.io.*;  
  4. public class hidden extends HttpServlet   
  5. {  
  6.  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {  
  7.   String s1 = req.getParameter("t1");  
  8.   String s2 = req.getParameter("t2");  
  9.   PrintWriter out = res.getWriter();  
  10.   out.println("<html><body>");  
  11.   if (s1 == null)   
  12.   {  
  13.    out.println("<form>");  
  14.    out.println("<h1>FName<input type='text' name='t1'></h1>");  
  15.    out.println("<input type='submit' value='submit'>");  
  16.    out.println("</form>");  
  17.   }   
  18.   else if (s2 == null)   
  19.   {  
  20.    out.println("<form>");  
  21.    out.println("<h1>LName<input type='text' name='t2'></h1>");  
  22.    out.println("<input type='hidden' name='t1' value='" + s1 + "'>");  
  23.    out.println("<input type='submit' value='submit'>");  
  24.    out.println("</form>");  
  25.   }   
  26.   else   
  27.   {  
  28.    out.println("<h1>Welcome " + s1 + " " + s2 + "</h1>");  
  29.   }  
  30.   out.println("</body></html>");  
  31.  }  
  32. }  
Web.xml settings 
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!--  
  3. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with  
  4. this work for additional information regarding copyright ownership.  
  5. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with  
  6. the License. You may obtain a copy of the License at  
  7.   
  8. http://www.apache.org/licenses/LICENSE-2.0  
  9. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,  
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and  
  11. limitations under the License.  
  12. -->  
  13. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  14. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  15. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  16. version="2.5">  
  17.   
  18. <servlet>  
  19. <servlet-name>hidden</servlet-name>  
  20. <servlet-class>hidden</servlet-class>  
  21. </servlet>  
  22.   
  23. <servlet-mapping>  
  24. <servlet-name>hidden</servlet-name>  
  25. <url-pattern>/hidden</url-pattern>  
  26. </servlet-mapping>  
  27.   
  28. </web-app>  
Compile 
 
javac -cp servlet-api.jar hidden.java (for tomcat 6.0) 
 
Hidden field in Servlet
  
Running in web-browser 
 
Run the tomcat then write the below line in the URL
 
Here the test is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.
  
http://localhost: 8081/test/hidden
  
Hidden field in Servlet
  
Thanks for reading.