Handling cookies

A cookie is a string containing a key/value pair representing information of interest to the server such as user preferences. It is sent from a server to a browser. The browser should save the cookie to a file so that it can be used later.

A cookie is a string that consists of a name followed by an equal sign and then a value. The following is one possible cookie:

userID=Cookie Monster

A cookie can have multiple values. These values will be separated by a semicolon and white space.

We will use the HTTPServer class and the HttpURLConnection classes to demonstrate the handling of cookies. In the MyHTTPServer class server's handler class's handle method, add the following code after the other headers:

 responseHeaders.set("Set-cookie", ...

Get Learning Network Programming with Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.