Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles Class in Java

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

ResponseCache in java is used for constructing implementation of URLConnection caches, and it nominates which resource has to be cached and up to what time duration a resource needed to be cached.

An instance of ResponseCache can be created using the system by doing :


The instance created by using the above statement will call an object of ResponseCache in order to :

  1. For storing resource data that has been which has been retrieved from an external source into the cache.
  2. For fetching a resource that has been stored in the cache on request.
  3. Response cache can be imported through package

Methods of ResponseCache class :

Method Description
get(URI uri, String rqstMethod, Map<String,List<String> > rqstHeaders) This method is used for retrieving the cached response depending upon the requesting URI, request method and request headers.
getDefault() This method is used for retrieving the system-wide cache response.
put(URI uri, URLConnection conn) The protocol handler calls this method whenever a resource has been retrieved and the ResponseCache must decide whether to store the resource in its cache.
setDefault(ResponseCache responseCache) This method is used to set or unset the system-wide cache

Applications of ResponseCache class :   

1. In the package, ResponseCache is used for implementing for caching of resources for various network applications such as :

  1. Email
  2. File Transfer
  3. Remote Terminal Access
  4. Loading web pages

2. In, ResponseCache is applied in fetching out system-wide response cache.

public static ResponseCache.getDefault()

3. In, ResponseCcahe is used in setting or unsetting out the system-wide cache.

public static void ResponseCache.setDefault(ResponseCache responseCache)

Java program for implementing :


import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class JavaResponseCacheExample1 {
    public static void main(String args[]) throws Exception
        // passing the string uri
        String uri = "";
        // Calling the constructor of the URI class
        URI uri1 = new URI(uri);
        // passing the url
        URL url = new URL("");
        // calling the constructor of the URLConnection
        URLConnection urlcon = url.openConnection();
        ResponseCache responseCache = new ResponseCache() {
            // calling the abstract methods
            public CacheResponse get(
                URI uri, String rqstMethod,
                Map<String, List<String> > rqstHeaders)
                throws IOException
                return null;
            public CacheRequest put(URI uri,
                                    URLConnection conn)
                throws IOException
                return null;
        // The sets the system-wide response cache.
        // The getDefault() method returns
        // the system-wide ResponseCache .
        System.out.println("Default value: "
                           + ResponseCache.getDefault());
        Map<String, List<String> > maps
            = new HashMap<String, List<String> >();
        List<String> list = new LinkedList<String>();
        // put() method sets all the applicable cookies,
        // present in the response headers into a cookie
        // cache
        maps.put("1", list);
            "The put() method has been called...");
        // The put() method returns the
        // CacheRequest for recording
            "The put() method returns: "
            + responseCache.put(uri1, urlcon));
            "The get() method has been called...");
        // The get() method returns a CacheResponse
        // instance if it is available
            "The get() method returns: "
            + responseCache.get(uri1, uri, maps));

Output :

My Personal Notes arrow_drop_up
Last Updated : 29 Mar, 2021
Like Article
Save Article
Similar Reads
Related Tutorials