In this Hidden Form Fields session tracking
data of every individual client is given back to client browser as a dynamic
form with hidden text field along with parameters and values, so that the
client browser will dispatch a form by hiding those text fields.
When the same client click a button of this
hidden form field then the client browser will automatically send the value of
hidden text fields along with parameters to the application.
It will always work whether cookie is
disabled or not.
Let's
see the code to store value in hidden field.
<input
type="hidden" name="uname" value="Mohan">
Here,
uname is the hidden field name and Mohan is the hidden field value.
The
following example demonstrate how to use this hidden form fields session
tracking in servlet application.
index.html
=======
<form
action="servlet1">
Name:<input
type="text" name="userName"/><br/>
<input
type="submit" value="submit"/>
</form>
web.xml
======
<?xml
version="1.0" encoding="UTF-8"?>
<web-app
version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
The
URL pattern of this class must be /s1, this will receive the request with one
parameter "userName" and Submit as get Request.
FirstServlet.java
===========
import
java.io.*;
import
javax.servlet.ServletException;
import
javax.servlet.http.*;
public
class FirstServlet extends HttpServlet {
public
void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException,ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String
n=request.getParameter("userName");
out.print("Welcome "+n);
out.println("<br /><br
/>");
//creating form that have invisible
textfield
out.print("<form
action='servlet2'>");
out.print("<input type='hidden'
name='uname' value='"+n+"'>");
out.print("<br
/>");
out.print("<input type='text'
name='sname' >");
out.print("<br
/>");
out.print("<input type='submit'
value='Submit'>");
out.print("</form>");
out.close();
}
}
The
URL pattern of this class must be /s2, this will receive the form request with
two parameters "uname" and ""sname"" when we
click on Submit button.
SecondServlet.java
=============
import
java.io.IOException;
import
java.io.PrintWriter;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public
class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException,
IOException {
response.setContentType("text/html");
PrintWriter out =
response.getWriter();
//Getting the value from the hidden
field
String
uname=request.getParameter("uname");
//Getting the value from the form
field
String
sname=request.getParameter("sname");
out.println("Hello
"+uname);
out.print("<br
/>");
out.println("Hello
"+sname);
out.close();
}
}
No comments:
Post a Comment