Servlets - SPLessons

Servlet Session Tracking

Home > Lesson > Chapter 22
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Session Tracking

Servlet Session Tracking

shape Description

Servlet Session Tracking, If a web application is capable of remembering a client data during a session across the multiple requests then that web application is called as a stateful Web application. Even though HTTP is a stateless protocol, web applications should be made as stateful web applications. For this, working with the following session tracking or session management techniques is necessary.

Hidden Form Field

shape Description

A hidden form field is an HTML element utilized to store the value similar to the text box, but here stored value will be invisible, so to extract that value servlet code or any server side code will be used. The form page having an invisible text box is called as a Hidden box. When one clicks Submit on the hidden text box, then the hidden box enters the value which goes to Web Resource Program along with a request as request parameter. Following is the syntax for hidden field case. [java]<input type="hidden" name="FormElementName" Value="Value name"/>[/java]

shape Conceptual figure

When user enter the details where user name and email id will be stored in servlet 1 hidden box , when user country details, all the details will be available through servlet 2.

shape Example

Servlet Session Tracking, Following is an example to understand the hidden form filed concept. index.html [html] <html> <form action="./welcome"> Enter name:<input type="text" name="username"/> Enter Email:<input type="text" name="emailId"/> <input type="submit" value="submit"/> </form> </html> [/html] Here the developer just created two text boxes for name and email. web.xml [xml] <web-app> <servlet> <servlet-name>DemoHiddenFields</servlet-name> <servlet-class>servlethiddenfield.DemoHiddenFields</servlet-class> </servlet> <servlet> <servlet-name>ShowAllData</servlet-name> <servlet-class>servlethiddenfield.ShowAllData</servlet-class> </servlet> <servlet-mapping> <servlet-name>DemoHiddenFields</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ShowAllData</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> </web-app> [/xml] Make sure that servlet name should be same and here the developer used two classes. DemoHiddenFields [java] package servlethiddenfield; 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 DemoHiddenFields extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String emailId = request.getParameter("emailId"); out.println("<HTML>"); out.println("<h2>your name is display in servlet1 :"+username+"</h2>"); out.println("<h2>your emaild is display in servlet1 :"+emailId+"</h2>"); out.println("</HTML>"); out.print("<form action='./servlet2'>"); //username and emailid in hidden to pass servlet2 out.print("<input type='hidden' name='username' value='"+username+"'>"); out.print("<input type='hidden' name='emailid' value='"+emailId+"'>"); out.print("Enter your country :<input type='text' name='country'>"); out.print("<input type='submit' value='submit'>"); out.print("</form>"); out.flush(); out.close(); } } [/java] username and emailid in hidden to pass servlet2 as follows. Sets the content type of the response being sent to the client, if the response has not been committed yet.The given content type may include a character encoding specification,request.getParameter() method in the servlet class, to retrieve the input values from HTML page. [java] out.print("<input type='hidden' name='username' value='"+username+"'>"); out.print("<input type='hidden' name='emailid' value='"+emailId+"'>"); [/java] Here the developer did not mention the hidden name to country. [java]Enter your country :<input type='text' name='country'>[/java] ShowAllData.java [java] package servlethiddenfield; 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 ShowAllData extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String emailId = request.getParameter("emailid"); String country = request.getParameter("country"); out.println("<HTML>"); out.println("<h2>your name is display in servlet2 :"+username+"</h2>"); out.println("<h2>your emaild is display in servlet2 :"+emailId+"</h2>"); out.println("<h2>your country is display in servlet2 :"+country+"</h2>"); out.println("</HTML>"); out.flush(); out.close(); } } [/java] Here all the details will be displayed. Here request.getParameter() method in the servlet class is used to retrieve the input values from HTML page.In doGet(), the parameters are appended to the URL and sent along with the header information. Output Following is the page where one can enter the details. After entering the details following message will be displayed. Final result will be as follows.

HTTP Session With URL Rewriting

shape Description

HTTP Session With URL Rewriting is another way to support anonymous session tracking. With URL rewriting, every local URL the user might click on is dynamically modified, or rewritten, to include extra information. The extra information can be in the form of extra path information, added parameters, or some custom, server-specific URL change. Due to the limited space available in rewriting a URL, the extra information is usually limited to a unique session ID. URL Rewriting in Servlets, which doesn’t use cookies to send and receive Session-Id from the web application

HTTP Cookies

shape Description

Cookies are allocated memory at the client side as a textual information by remembering client information throughout the session. The servlet programs of web application create cookies on the server side, but these cookies come to the client along with the response and allocate memory at the client side. The first request cookies are created and next request, cookies go back to the web application along with request given by the client.

Summary

shape Key Points

  • Servlet Session Tracking - Session can be defined as specific interval of time.
  • If HTTP is stateless then each request will be treated as new request from the web server.
  • To identify specific user session tracking will be used.