<< Chapter < Page | Chapter >> Page > |
Figure 3 - Output from access #3.
Figure 4 - Output from access #4.
Figure 5 - Output from access #5.
Figure 6 - Output from access #6.
Figure 7 - Output from access #7.
I will explain the code in this servlet in fragments. A complete listing of the servlet is provided in Listing 10 .
Beginning of the doGet method
Listing 1 shows typical code for beginning the definition of a servlet class and for beginning the definition of the doGet method. You have seen code like this in earlier modules so I won't discuss it further inthis module.
Listing 1 - Beginning of the doGet method. |
---|
import java.io.*;
import java.util.*;import javax.servlet.*;
import javax.servlet.http.*;public class Java4580a extends HttpServlet{public void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException{ |
Get the session object
I will continue the discussion with the code fragment shown in Listing 2 , where request is a reference to the standard object of type HttpServletRequest passed to the doGet method that begins in Listing 1 .
Listing 2 - Get the session object. |
---|
HttpSession session = request.getSession(true); |
An object of type HttpSession
The call to the getSession method in Listing 2 returns a reference to an object of the interface type HttpSession . As you can see, the reference is saved in the local variable named session .
The HttpSession object provides an association (a session) between an HTTP client and an HTTP server. This association, or session, persists over multiple connectionsand/or requests during a given time period. Sessions are used to maintain state and user identity across multiple page requests.
How is the session maintained?
Apparently the manner in which the association between the client and the server (the session) is maintained varies from one server to the next. One books states,
"A session can be maintained either by using cookies or by URL rewriting."
Another book states that the minimum requirement for servers is to maintain the session using cookies, andthat the server may optionally support URL rewriting as well. There are inferences in other books to the effect that some servers may not supportURL rewriting.
The HttpSession object
The HttpSession object returned by the code in Listing 2 behaves as a container for the storage of:
Client ID is transparent
The requirement to maintain the identification of each individual client is transparent to the programmer. That information is encapsulated in the HttpSession object and the process that backs it up.
The lifetime of a session
An HttpSession object represents an ongoing session with a particular client. The actual lifetime of a session isn't clear in the booksthat I have read.
The books seems to imply that the session can remain active over long periods of time. However, on my local Tomcat server, thesession ends when the browser is terminated. The behavior is similar to that which occurs with the default behavior of cookies that expire when the browser exits.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?