.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Hidden field in servlet

Posted By:satyapriyanayak       Posted Date: May 27, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will discuss about the use of hidden field.
 

Hidden field: - This is a process to store information present in the current request in a form field called as hidden field. Whenever form containing hidden field gets submitted then the destination servlet can find value of hidden field by using 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 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)

 


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

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend