From the time you fire the first request from a browser to a server, to the time you shut down the browser - is a single HTTP Session to this server.
All the requests the server received from this browser during this period belong to the same HTTP Session.
If you restart the browser, and send a new request to the server - that would start a new HTTP Session on the server.
If 10 (or n) people are sending requests to the server, then there are 10 (or n) simultaneous HTTP Sessions.
If you have multiple tabs open in your browser, each pointing to a different server, then you have that many HTTP Sessions open on your client side. Each server sees a single HTTP Session from you.
If you have a single tab open in your browser and send requests to multiple servers, you have that many HTTP Sessions open. Each server you sent a request to sees a single HTTP Session from you.
Note that a Login Session is different from a HTTP Session. Suppose I open a browser, login to my bank website, then logout, again login and then logout - and then shut down the browser. I had 2 Login Sessions within a single HTTP Session.
HTTP Sessions are timed out by the server if the administrator has configured an idle period timeout. Login Sessions are timed out by the application. Some internet email and news services allow you to maintain your Login Session over several days, even if you reboot your computer. HTTP Sessions will always end when you close your browser, regardless of the idle timeout set by the administrator.
Friday, December 2, 2011
Stateful HelloWorld
Let us make HellowWorld 'stateful' - i.e. make it maintain some data ("state") between requests.
We will store the exact time a request is received. If the next request is received later than a certain threshold, we can potentially timeout the session. Many websites do this for security.
In this example, we will not actually timeout - we just want to demonstrate how to maintain 'state'.
We will maintain a simple Hashtable which maps the user ID (that we get from the cookie) to the time the request is received.
Here is what HelloWorld looks like now:
Build HelloWorld.war, deploy it, then point your browser to:
http://localhost:8080/HelloWorld/home
Each time you refresh the browser, you will see the time that the last request was received.
We will store the exact time a request is received. If the next request is received later than a certain threshold, we can potentially timeout the session. Many websites do this for security.
In this example, we will not actually timeout - we just want to demonstrate how to maintain 'state'.
We will maintain a simple Hashtable which maps the user ID (that we get from the cookie) to the time the request is received.
Here is what HelloWorld looks like now:
package org.confucius;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
private static int nextUserId = 0;
private static HashMap<String, Date> requestsTracker = new HashMap<String, Date>();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cookieValue = getCookieValue(request.getCookies(), "userId");
if (cookieValue == null) {
cookieValue = String.valueOf(nextUserId);
Cookie userCookie = new Cookie("userId", cookieValue);
response.addCookie(userCookie);
nextUserId++;
}
Date now = new Date();
response.getWriter().write(
"Your last request was received on: "
+ requestsTracker.get(cookieValue) + ", updating to: "
+ now);
requestsTracker.put(cookieValue, now);
}
private String getCookieValue(Cookie[] cookies, String cookieName) {
if (cookies == null)
return null;
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return (cookie.getValue());
}
return null;
}
}
Build HelloWorld.war, deploy it, then point your browser to:
http://localhost:8080/HelloWorld/home
Each time you refresh the browser, you will see the time that the last request was received.
Thursday, December 1, 2011
ServletContext
Using a static class variable like we did (nextUserId) in a Servlet is not such a good idea - it is known to have some issues in multi-threading, and also because the exact lifecycle of a Servlet is controlled by the Servlet container.
So it is recommended to use ServletContext.
ServletContext is a global object which is created when the Application is started and exists until the Application is shut down (this is called 'Application-Scope').
Any Servlet can access the ServletContext object by calling getServletContext()
Here is our HelloWorld again, this time using ServletContext:
So it is recommended to use ServletContext.
ServletContext is a global object which is created when the Application is started and exists until the Application is shut down (this is called 'Application-Scope').
Any Servlet can access the ServletContext object by calling getServletContext()
Here is our HelloWorld again, this time using ServletContext:
package org.confucius;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet{
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String cookieValue = getCookieValue(request.getCookies(), "userId");
if (cookieValue == null){
ServletContext context = getServletContext();
Integer nextUserId = (Integer) context.getAttribute("nextUserId");
if (nextUserId == null){
nextUserId = new Integer(0);
context.setAttribute("nextUserId", nextUserId);
}
Cookie userCookie = new Cookie("userId", nextUserId.toString());
response.addCookie(userCookie);
nextUserId++;
context.setAttribute("nextUserId", nextUserId);
}
response.getWriter().write("User ID = " + cookieValue);
}
private String getCookieValue(Cookie[] cookies, String cookieName) {
if (cookies == null)
return null;
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return (cookie.getValue());
}
return null;
}
}
Subscribe to:
Posts (Atom)