MySQL Application Hosting Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting

Alden Hosting provides professional, efficient, and reliable business-class Web hosting services to small- and medium-sized businesses.

WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

MySQL Application Hosting Welcome MySQL Application Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting MySQL Application Hosting Fund Raising, Fundraising, web hosting MySQL Application Hosting Resellers, web Hosting MySQL Application Hosting Web Design, web Hosting MySQL Application Hosting Extra Services,  web Hosting MySQL Application Hosting Traffic Booster, web hosting MySQL Application Hosting Traffic Booster, web hosting MySQL Application Hosting Technical Support,  web Hosting MySQL Application Hosting webmaster tips,  web Hosting MySQL Application Hosting 30 Day Money Back, web hosting MySQL Application Hosting Legal Notices for Web Hosting MySQL Application Hosting Glossary Computer Terms for web Hosting MySQL Application Hosting Contact Information - web hosting

Site Map

  MySQL Application Hosting Web Hosting Sign-Up   MySQL Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting    MySQL Application Hosting Resellers web hosting, website hosting, web site hosting   MySQL Application Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Custom CookieManager (The Java™ Tutorials > Custom Networking > Working With Cookies)
Trail: Custom Networking
Lesson: Working With Cookies
Home Page > Custom Networking > Working With Cookies
Custom CookieManager
Two aspects of the CookieManager class can be customized, the CookiePolicy and the CookieStore.

CookiePolicy

For convenience, CookiePolicy defines the following pre-defined policies for accepting cookies:
  • CookiePolicy.ACCEPT_ORIGINAL_SERVER only accepts cookies from the original server.
  • CookiePolicy.ACCEPT_ALL accepts all cookies.
  • CookiePolicy.ACCEPT_NONE accepts no cookies. You can also define your own cookie policy by implementing the shouldAccept method of CookiePolicy. You can then use this CookiePolicy by passing it to the multi-argument CookieManager constructor or by calling the setCookiePolicy(cookiePolicy) method to change an already existing cookie manager.

    The following is an example of a cookie policy that rejects cookies from domains that are on a blacklist, before applying the CookiePolicy.ACCEPT_ORIGINAL_SERVER policy:

    import java.net.*;
    
    public class BlacklistCookiePolicy implements CookiePolicy
    {
        String[] blacklist;
    
        public BlacklistCookiePolicy(String[] list) {
            blacklist = list;
        }
    
        public boolean shouldAccept(URI uri, HttpCookie cookie)  {
            String host;
            try {
                host =  InetAddress.getByName(uri.getHost()).getCanonicalHostName();
            } catch (UnknownHostException e) {
                host = uri.getHost();
            }
    
            for (int i=0; i<blacklist.length; i++) {
    	    if (HttpCookie.domainMatches(blacklist[i], host)) {
                    return false;
                }
            }
    
            return CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, cookie);
        }
    }
    
    When you create a BlacklistCookiePolicy instance, you pass it an array of strings representing the domains that you do not want to accept cookies from. Then, you set this BlacklistCookiePolicy instance as the cookie policy for your CookieManager. For example:
    String[] list = new String[] { ".bar.com" };
    CookieManager cm = new CookieManager(null, new BlacklistCookiePolicy(list));
    CookieHandler.setDefault(cm);
    
    The sample code will not accept cookies from hosts such as the following:
    foo.bar.com
    goo.bar.com
    
    However, this sample code will accept cookies from hosts such as the following:
    y.com
    bar.com
    x.foo.bar.com
    

CookieStore

A CookieStore is an interface that represents a storage area for cookies. CookieManager adds the cookies to the CookieStore for every HTTP response and retrieves cookies from the CookieStore for every HTTP request.

You can implement this interface to provide your own CookieStore and pass it to the CookieManager during creation. You cannot set the CookieStore after the CookieManager instance has been created. However, you can get a reference to the cookie store by calling CookieManager.getCookieStore(). Doing so can be useful as it enables you to leverage the default in-memory CookieStore implementation that is provided by Java SE and complement its functionality.

For example, you might want to create a persistent cookie store that would save cookies so that they can be used even if the Java Virtual Machine is restarted. Your implementation would work similar to the following:

  1. Any cookies that were previously saved are read in.
  2. During runtime, cookies are stored and retrieved from memory.
  3. Cookies are written out to persistent storage before exiting.
The following is an incomplete example of this cookie store. This example shows you how to leverage the Java SE default in-memory cookie store and how you might extend its functionality.
import java.net.*;
import java.util.*;

public class PersistentCookieStore 
   implements CookieStore, Runnable
{
    CookieStore store;

    public PersistentCookieStore() {
        // get the default in memory cookie store
        store = new CookieManager().getCookieStore();

        // todo: read in cookies from persistant storage
        // and add them store

        // add a shutdown hook to write out the in memory cookies
        Runtime.getRuntime().addShutdownHook(new Thread(this)); 
    }

    public void run() {
        // todo: write cookies in store to persistent storage
    }

    public void	add(URI uri, HttpCookie cookie) {
        store.add(uri, cookie);
    }

    public List<HttpCookie> get(URI uri) {
        return store.get(uri);
    }

    public List<HttpCookie> getCookies() {
        return store.getCookies();
    }
    
    public List<URI> getURIs() {
        return store.getURIs();
    }

    public boolean remove(URI uri, HttpCookie cookie) {
        return store.remove(uri, cookie);
    }

    public boolean removeAll()  {
        return store.removeAll();
    }
}
Previous page: Default CookieManager
Next page: End of Trail
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

http://alden-servlet-Hosting.com
JSP at alden-servlet-Hosting.com
Servlets at alden-servlet-Hosting.com
Servlet at alden-servlet-Hosting.com
Tomcat at alden-servlet-Hosting.com
MySQL at alden-servlet-Hosting.com
Java at alden-servlet-Hosting.com
sFTP at alden-servlet-Hosting.com
http://alden-tomcat-Hosting.com
JSP at alden-tomcat-Hosting.com
Servlets at alden-tomcat-Hosting.com
Servlet at alden-tomcat-Hosting.com
Tomcat at alden-tomcat-Hosting.com
MySQL at alden-tomcat-Hosting.com
Java at alden-tomcat-Hosting.com
sFTP at alden-tomcat-Hosting.com
http://alden-sftp-Hosting.com
JSP at alden-sftp-Hosting.com
Servlets at alden-sftp-Hosting.com
Servlet at alden-sftp-Hosting.com
Tomcat at alden-sftp-Hosting.com
MySQL at alden-sftp-Hosting.com
Java at alden-sftp-Hosting.com
sFTP at alden-sftp-Hosting.com
http://alden-jsp-Hosting.com
JSP at alden-jsp-Hosting.com
Servlets at alden-jsp-Hosting.com
Servlet at alden-jsp-Hosting.com
Tomcat at alden-jsp-Hosting.com
MySQL at alden-jsp-Hosting.com
Java at alden-jsp-Hosting.com
sFTP at alden-jsp-Hosting.com
http://alden-java-Hosting.com
JSp at alden-java-Hosting.com
Servlets at alden-java-Hosting.com
Servlet at alden-java-Hosting.com
Tomcat at alden-java-Hosting.com
MySQL at alden-java-Hosting.com
Java at alden-java-Hosting.com
sFTP at alden-java-Hosting.com
JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . .
.
.
. .
. . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting