- Servlet Sessions
- Using Hidden Form Fields
- Working with Cookies
- URL Rewriting
- Session Tracking with the Servlet API
- Summary
Using Hidden Form Fields
Using hidden form fields is one of the simplest session tracking techniques. Hidden form fields are HTML input types that are not displayed when read by a browser. The following sample HTML listing includes hidden form fields:
<HTML> <BODY> <FORM ACTION="someaction" METHOD="post"> <INPUT TYPE="hidden" NAME="tag1" VALUE="value1"> <INPUT TYPE="hidden" NAME="tag2" VALUE="value2"> <INPUT TYPE="submit"> </FORM> </BODY> </HTML>
When you open this HTML document in a browser, the input types marked as hidden will not be visible. They will, however, be transmitted in the request.
Let's create a simple example that shows how this technique works. You'll create a servlet that can service both POST and GET methods. In the doGet() method, you'll build a form that contains hidden fields and an action that points to the servlet's doPost() method. The doPost() method will then parse the hidden values sent in the request and echo them back to the client. The example is found in Listing 5.1.
Listing 5.1 HiddenFieldServlet.java
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class HiddenFieldServlet extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>HiddenFieldServlet" + "</title></head>"); out.println("<body>"); // Create the Form with Hidden Fields out.println("<FORM ACTION=" + "\"/djs/servlet/HiddenFieldServlet\" METHOD=\"POST\">"); // These values would be uniquely generated out.println("<INPUT TYPE=\"hidde" NAME=" + "\"user\" VALUE=\"James\">"); out.println("<INPUT TYPE=\"hidde" NAME=" + "\"sessio" VALUE=\"12892\">"); // These are the currently selected movies out.println("<INPUT TYPE=\"hidde" NAME=" + "\"movie\" VALUE=\"Happy Gilmore\">"); out.println("<INPUT TYPE=\"hidde" NAME=" + "\"movie\" VALUE=\"So I Married an Axe Murderer\">"); out.println("<INPUT TYPE=\"hidde" NAME=" + "\"movie\" VALUE=\"Jaws\">"); out.println("<INPUT TYPE=\"submit\" VALUE=" + "\"Submit\">"); out.println("</FORM>"); out.println("</body></html>"); out.close(); } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>HiddenFieldServlet" + "</title></head>"); out.println("<body>"); // Get the hidden inputs and echo them String user = request.getParameter("user"); String session = request.getParameter("session"); out.println("<H3>" + user + ", the contents of your Shopping Basket are:</H3><BR>"); String[ ] movies = request.getParameterValues("movie"); if ( movies != null ) { for ( int x = 0; x < movies.length; x++ ) { out.println(movies[x] + "<BR>"); } } out.println("</body></html>"); out.close(); } //Get Servlet information public String getServletInfo() { return "HiddenFieldServlet Information"; } }
When you have this servlet installed, open your browser to the servlet's URL. The URL on my local box is listed as follows:
http://localhost/djs/servlet/HiddenFieldServlet
When the servlet is loaded, you should only see a Submit button. If you view the current HTML source, you will see a listing similar to this snippet:
<html> <head><title>HiddenFieldServlet</title></head> <body> <FORM ACTION="/djs/servlet/HiddenFieldServlet" METHOD="POST"> <INPUT TYPE="hidden" NAME="user" VALUE="James"> <INPUT TYPE="hidden" NAME="session" VALUE="12892"> <INPUT TYPE="hidden" NAME="movie" VALUE="Happy Gilmore"> <INPUT TYPE="hidden" NAME="movie" VALUE="So I Married an Axe Murderer"> <INPUT TYPE="hidden" NAME="movie" VALUE="Jaws"> <INPUT TYPE="submit" VALUE="Submit"> </FORM> </body></html>
Notice the hidden fields. Now click the Submit button. The form invokes the doPost() method of the HiddenFieldServlet. This method parses the hidden fields out of the request and displays them in a "shopping cart" listing. Figure 5.1 shows the results of the HiddenFieldServlet's doPost() method.
Figure 5.1 Output of HiddenFieldServlet.
You can see that hidden form fields have their advantages. They are easy to implement and are supported by most browsers. This technique also has its disadvantages. The hidden fields must be created in a particular sequence. You are not able to click the Back button on your browser without losing the additional fields added to the current page. You are also restricted to dynamically generated documents.