SIGN UP MEMBER LOGIN:    
ARTICLE

Hidden field in Servlet

Posted by Satyapriya Nayak Articles | Java September 08, 2011
In this article we will discuss about the use of the hidden field in Servlets.
Reader Level:
Download Files:
 

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor