<< Chapter < Page | Chapter >> Page > |
Sending cookies to the browser
In the Java API, cookies are saved one at a time into such HTTP response headers, using the addCookie method.
According to the Java documentation , The browser is expected to support 20 cookies for each web server, 300 cookies total, and may limit cookie size to 4 KB each.
Servers assign cookies, using fields added to HTTP response headers.
Retrieving cookies from the browser
Cookies are passed from the browser to the server using fields added to HTTP request headers.
In the Java API, HTTP cookies are retrieved using the getCookies method. This method returns all of the cookies found in the request sent by theclient.
Description of the Cookie constructor
An abbreviated description of the constructor for the Cookie class is shown in Figure 3 .
Figure 3 - Description of the Cookie constructor. |
---|
public Cookie(String name, String value) Constructs a cookie with the specified name and value. The name of a cookie cannot be changed once the cookie has been created. The value can be anything the server chooses to send. The cookie's value can be changed after creation with the setValue method. Parameters:
|
The name of the servlet program is Java4570 I will discuss this program in fragments. A complete listing of the program is provided in Listing 10 .
Displays an HTML form
Each time the servlet is called, it displays an HTML form on the client screen as shown in Figure 1 . The form contains:
A unique session ID
The first time the servlet is called by a given browser, code written into the servlet creates a unique session ID and stores it in a cookie on the browser. This session ID isnot used for any purpose in this sample program. It is included in the servlet simply to show how to create and save a unique session ID.
A cookie containing the submitted value
Each time the servlet is called, it creates a cookie containing the field value submitted by the user and stores it on the browser.
When the servlet is called, it gets all of the cookie information stored on the browser and displays that information on the client screen as shown in Figure 1 . The display includes the unique session ID and all of the field values submitted by the user during that session.
The maximum age of the cookies
The cookie containing the session ID is deleted when the browser is exited.
The maximum age for all of the other cookies is set to 60 seconds. Therefore, if you submit some names, wait more than 60 seconds, and then submitsome more names, the first batch of names submitted will have expired and will have disappeared from the list.
The lifetime of a session
A session doesn't end just because the user visits another page. If the user returns to the Java4570 servlet without exiting the browser, the cookies written by Java4570 that have not yet expired will continue to exist on the browser and the sessionwill continue.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?