Using CookieStore : Cookie « JDK 6 « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » JDK 6 » Cookie 




Using CookieStore
 

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class WebClient {
  public static void main(String[] argsthrows Exception {
    CookieStore store = new MyCookieStore();
    CookiePolicy policy = new MyCookiePolicy();
    CookieManager handler = new CookieManager(store, policy);
    CookieHandler.setDefault(handler);
    URL url = new URL("http://localhost:8080/cookieTest.jsp");
    URLConnection conn = url.openConnection();

    InputStream in = conn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String input;
    while ((input = reader.readLine()) != null) {
      System.out.println(input);
    }
    reader.close();

  }
}

class MyCookiePolicy implements CookiePolicy {
  public boolean shouldAccept(URI uri, HttpCookie cookie) {
//    String host = uri.getHost();
  //  return host.equals("localhost");
    return true;
  }
}

class MyCookieStore implements CookieStore {
  private Map<URI, List<HttpCookie>> map = new HashMap<URI, List<HttpCookie>>();

  public void add(URI uri, HttpCookie cookie) {
    List<HttpCookie> cookies = map.get(uri);
    if (cookies == null) {
      cookies = new ArrayList<HttpCookie>();
      map.put(uri, cookies);
    }
    cookies.add(cookie);
  }

  public List<HttpCookie> get(URI uri) {
    List<HttpCookie> cookies = map.get(uri);
    if (cookies == null) {
      cookies = new ArrayList<HttpCookie>();
      map.put(uri, cookies);
    }
    return cookies;
  }

  public List<HttpCookie> getCookies() {
    Collection<List<HttpCookie>> values = map.values();
    List<HttpCookie> result = new ArrayList<HttpCookie>();
    for (List<HttpCookie> value : values) {
      result.addAll(value);
    }
    return result;
  }

  public List<URI> getURIs() {
    Set<URI> keys = map.keySet();
    return new ArrayList<URI>(keys);

  }

  public boolean remove(URI uri, HttpCookie cookie) {
    List<HttpCookie> cookies = map.get(uri);
    if (cookies == null) {
      return false;
    }
    return cookies.remove(cookie);
  }

  public boolean removeAll() {
    map.clear();
    return true;
  }
}


The cookieTest.jsp page


<%
    Cookie cookie = new Cookie ("username""guest");

    cookie.setMaxAge (100);
    response.addCookie (cookie);
%>
<html>
<head>
<title>HttpCookie Demo</title>
</head>
<body>
Add cookie. Cookie name = <%=cookie.getName () +". Cookie value = " + cookie.getValue () %>
</body>
</html>



//Reference:
//Java 6 New Features: A Tutorial
//by Budi Kurniawan 
//Brainy Software Corp. 2006
//Chapter 4 - Networking
//# ISBN-10: 0975212885
//# ISBN-13: 978-0975212882

        














Related examples in the same category
1.Fetch Cookie with CookieHandler, CookieManager and CookieStore
2.Using CookieHandler in Java 5
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.