Session Management with Hidden Form Field - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

Session Management with Hidden Form Field

Share This

HTML forms support input tags with the attribute hidden ensuring whatever values are set to this input, are not visible in the browser. This property of the input tag can be effectively utilized to pass on session info through the server. The following describes a session management scenario where hidden form fields could be used efficiently.



Example of Managing Sessions with Hidden Form Field


Let us consider a very small application where a user will input his name and surname from an HTML form. This name and surname data will be carried along HTTPRequest to a servlet FirstServlet. It will generate a view where the user will be greeted with name and surname and will also ask for the name of his/her pet through another HTML form to be sent to another servlet SecondServ where all three data will be shown. 


Now it is to be noted that when the request reaches the SecondServ, SecondServ has no clue about the name and surname of the user, as HTTP, due to its virtue of statelessness, does not remember the request data.


Here, we can use Hidden Form Field to bind the name and surname data into the second HTML form so that it reaches the SecondServ upon the form submission. The following codes illustrate what we discussed just now.


Code for index.html - the landing page of the application

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>April Tutorials</title>
</head>
<body>
<h2>Log In To The System </h2>
<form action="FirstServ" method="post">
<label>Name</label>
<input type="text" name="name"/>
<br/>
<label>Surname</label>
<input type="text" name="surname"/>
<br/>
<input type="submit" value="Go!">
</form>
</body>
</html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Code for FirstServ.java - It is where the Hidden Form Field is being used

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FirstServ
*/
@WebServlet("/FirstServ")
public class FirstServ extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FirstServ() {
super();
// TODO Auto-generated constructor stub
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Code for SecondServ.java - which utilizes the data sent through Hidden Form Field

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SecondServ
*/
@WebServlet("/SecondServ")
public class SecondServ extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SecondServ() {
super();
// TODO Auto-generated constructor stub
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Advantages and Disadvantages of using Hidden Form Field


Hidden Form Field is simple to implement and it is supported by all the web browsers. But you need to make this point very clear that to use Hidden Form Fields, you need to have an extra form submission, which you might not like every time

Another concern is that if you try to pass a huge amount of data through Hidden Form Field, then the page performance might get affected as all the data are stored within the page. 

Lastly, please note that you can pass only textual data through Hidden Form Field.

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.