Session Management with Hidden Form Field - BunksAllowed

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

Community

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

<!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>

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

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 } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String surname = request.getParameter("surname"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h2>Hello " + name + " " + surname + "</h2>"); out.println("<form action = \"SecondServ\" method=\"post\">"); out.println("<label> What is your pets name?<br/>"); out.println("<input type=\"text\" name=\"petsname\"/><br/>"); //Hidden Form Field utilized out.println("<input type=\"hidden\" name = \"name\" value=\"" + name +"\"\\>"); //Hidden Form Field utilized out.println("<input type=\"hidden\" name = \"surname\" value=\"" + surname +"\"\\>"); out.println("<input type=\"submit\" value=\"Go!\"\\>"); out.println("</form>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }

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

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 } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String ownerName = request.getParameter("name"); String ownerSurname = request.getParameter("surname"); String petsName = request.getParameter("petsname"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Hello " + ownerName + " " + ownerSurname + "! How is your " + petsName + " ?"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }

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!

No comments:

Post a Comment

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