Tag Archives: Cookies

Setting Cookies


An HTTP cookie (also called web cookie, Internet cookie, browser cookie, or simply cookie) is a small piece of data sent from a website and stored on the user’s computer by the user’s web browser while the user is browsing. Cookies were designed to be a reliable mechanism for websites to remember stateful information (such as items added in the shopping cart in an online store) or to record the user’s browsing activity (including clicking particular buttons, logging in, or recording which pages were visited in the past). They can also be used to remember arbitrary pieces of information that the user previously entered into form fields such as names, addresses, passwords, and credit card numbers. To set a cookie in a JSP, the following code is used:

Set-Cookie: cookie-value; expires=date; path=path; domain=domain-name; secure


Setting a http Cookie


Http cookies are basically name/value pairs which can be saved to a Webbrowser to “remember” what the client (user) did. It helps to overcome the disadvantages of the stateless http protocol.

This example shows how to create a cookie and store it in the URLConnection property.

try {
    // Create a URLConnection object for a URL
    URL url = new URL("http://hostname:80");
    URLConnection conn = url.openConnection();

    // Set the cookie value to send
    conn.setRequestProperty("Cookie", "name1=value1; name2=value2");

    // Send the request to the server
} catch (MalformedURLException e) {
} catch (IOException e) {