❮ Previous Next ❯

Hidden Form Field

It is another session management technique where we use a hidden text field to store session information for a particular client for maintaining the state of an user. Here information stored in hidden field value and we will get that information from another Servlet.

The code to store value in hidden field as:

<input type="hidden" name="uname" value="Trilochan Tarai">

Here, uname is the hidden field name and Trilochan Tarai is the hidden field value.

Advantages:

Disadvantages:

Extra form submission is required on every page.

Let's see a demo program:

index.html

<form method="post" action="validate">
UserName:<input type="text" name="uname" />
Password:<input type="text" name="pwd">
<input type="submit" value="submit">
</form>

MyServlet1.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
//getting value submitted in form from HTML file
String user = request.getParameter("uname");
//creating a new hidden form field
pw.println("<form action='Second'>");
pw.println("<input type='hidden' name='user' value='"+user+"'>");
pw.println("<input type='submit' value='submit'>");
pw.println("</form>");
}
}

MyServlet2.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
//getting parameter from the hidden field
String user = request.getParameter("user");
pw.println("Welcome "+user);
}
}

web.xml

<web-app...>
<servlet>
<servlet-name>First</servlet-name>
<servlet-class>MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/MyServlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Second</servlet-name>
<servlet-class>MyServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Second</servlet-name>
<url-pattern>/MyServlet2</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Here we created a hidden field in MyServlet1 , populated the value of user, and sent it to the MyServlet2, now MyServlet2 also has the user information. Similarly we will have to keep sending this information, wherever we need this, using hidden fields.

❮ Previous Next ❯

Follow Us





  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved