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 name of the user as first name and last name and showing the full name using a hidden field.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class hidden extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
String s1=req.getParameter("t1");
String s2=req.getParameter("t2");
PrintWriter out=res.getWriter();
out.println("<html><body>");
if(s1 ==null)
{
out.println("<form>");
out.println("<h1>FName<input type='text' name='t1'></h1>");
out.println("<input type='submit' value='submit'>");
out.println("</form>");
}
else if(s2 ==null)
{
out.println("<form>");
out.println("<h1>LName<input type='text' name='t2'></h1>");
out.println("<input type='hidden' name='t1' value='"+s1+"'>");
out.println("<input type='submit' value='submit'>");
out.println("</form>");
}
else
{
out.println("<h1>Welcome "+s1+" "+s2+"</h1>");
}
out.println("</body></html>");
}
}

Web.xml settings

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
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
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<servlet>
<servlet-name>hidden</servlet-name>
<servlet-class>hidden</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hidden</servlet-name>
<url-pattern>/hidden</url-pattern>
</servlet-mapping>

</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 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.

Up Next
    Ebook Download
    View all
    Learn
    View all