Hidden field in servlet in java

Satyapriyanayak
Posted by Satyapriyanayak under Others category on | Points: 40 | Views : 740
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"?>

<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

Comments or Responses

Login to post response