What is session? What are different ways of tracking sessions? Write down suitable servlet for tracking session.

 Session simply means a particular interval of time. A session is a conversation between the server and a client. A conversation consists series of continuous requests and responses.

Session Tracking is a way to maintain the state (data) of a user. It is also known as session management in servlet. There are four techniques used in Session tracking:

1. Cookies

2. Hidden Form Field

3. URL Rewriting

4. HttpSession


Cookies

A webserver can assign a unique session ID as a cookie to each web client and for subsequent requests from the client they can be recognized using the recieved cookie.

This may not be an effective way because many time browser does not support a cookie, so I would not recommend to use this procedure to maintain the sessions.


Hidden Form Fields

  • A web server can send a hidden HTML form field along with a unique session ID as follows −

<input type = "hidden" name = "sessionid" value = "12345">

  • This entry means that, when the form is submitted, the specified name and value are automatically included in the GET or POST data. Each time when web browser sends request back, then session_id value can be used to keep the track of different web browsers.
  • This could be an effective way of keeping track of the session but clicking on a regular (<A HREF...>) hypertext link does not result in a form submission, so hidden form fields also cannot support general session tracking.


URL Rewriting

  • You can append some extra data on the end of each URL that identifies the session, and the server can associate that session identifier with data it has stored about that session.
  • For example, with http://tutorialspoint.com/file.htm;sessionid = 12345, the session identifier is attached as sessionid = 12345 which can be accessed at the web server to identify the client.
  • URL rewriting is a better way to maintain sessions and it works even when browsers don't support cookies. The drawback of URL re-writing is that you would have to generate every URL dynamically to assign a session ID, even in case of a simple static HTML page.


The HttpSession Object

  • Apart from the above mentioned three ways, servlet provides HttpSession Interface which provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
  • The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user.
  • You would get HttpSession object by calling the public method getSession() of HttpServletRequest, as below −

HttpSession session = request.getSession();

Example

//session.html file

<html>

<body>

<form action="SetSession">

Name:<input type="text" name="uname"/><br/> <input type="submit" value="go"/> </form>

</body> </html>

//SetSession.java File- Sets session

import java.io.*;

import javax.servlet.*; import javax.servlet.http.*;

public class SetSession extends HttpServlet

{

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter(); String n=request.getParameter("uname"); out.print("Welcome"+n);

HttpSession session=request.getSession();

session.setAttribute("uname", n); out.print("<a href='ReadServlet'>Visit Here</a>");

}

}

//ReadSession.java File- Reads session

import java.io.*; import javax.servlet.*;

import javax.servlet.http.*;

public class ReadSession extends HttpServlet

{

public  void doGet (HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException

{ response.setContentType("text/html"); Printwriter out = response.getWriter(); HttpSession session=request.getSession (false); String n=(String) session.getAttribute("uname"); ¹ out.print("Hello "+n);

out.close();

}

}

Output

Output will be hilar to previous program 

Comments

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Discuss classification or taxonomy of virtualization at different levels.

Pure Versus Partial EC