Show TOC

Handling Cookies in ServletsLocate this document in the navigation structure

Use

You can use cookies in your servlets to exchange small bits of information between the client browser and the server. They can serve as convenient means to do session tracking, to facilitate application load balancing, and so on.

Once set on the client browser, cookies are always sent with the request to the server. This way, the server can easily identify a client.

Process

A servlet sets a cookie on the client browser when the servlet is first requested by that client, and that request does not contain a cookie. With each subsequent request, the servlet can use the HTTP request object and retrieve the information that the cookie contains. The servlet can change the value of the cookie if necessary.

There are cases when the client does not support or accept cookies. If this is the case, you can use URL rewriting as an alternative to cookies. With URL rewriting, the same information that the cookie contains is transmitted by appending it to the request URL.

The AS Java defines two types of cookies that you can use in your Web applications:

  • Session cookie - use it for session tracking.

  • Load balancing cookie - use it for load balancing your distributed Web applications.

More information: AS Java Cookies .