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 :
ResponseCache.setDefault(ResponseCache)
The instance created by using the above statement will call an object of ResponseCache in order to :
- For storing resource data that has been which has been retrieved from an external source into the cache.
- For fetching a resource that has been stored in the cache on request.
- Response cache can be imported through java.net package
java.net.ResponseCache
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 java.net package, ResponseCache is used for implementing for caching of resources for various network applications such as :
- File Transfer
- Remote Terminal Access
- Loading web pages
java.net.ResponseCache
2. In java.net, ResponseCache is applied in fetching out system-wide response cache.
public static ResponseCache.getDefault()
3. In java.net, ResponseCcahe is used in setting or unsetting out the system-wide cache.
public static void ResponseCache.setDefault(ResponseCache responseCache)
Java program for implementing java.net.ResponseCache :
Java
import java.io.IOException; import java.net.*; 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 // Calling the constructor of the URI class URI uri1 = new URI(uri); // passing the url // calling the constructor of the URLConnection URLConnection urlcon = url.openConnection(); ResponseCache responseCache = new ResponseCache() { // calling the abstract methods @Override public CacheResponse get( URI uri, String rqstMethod, Map<String, List<String> > rqstHeaders) throws IOException { return null ; } @Override public CacheRequest put(URI uri, URLConnection conn) throws IOException { return null ; } }; // The sets the system-wide response cache. ResponseCache.setDefault(responseCache); // 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>(); list.add( "REema" ); // put() method sets all the applicable cookies, // present in the response headers into a cookie // cache maps.put( "1" , list); System.out.println( "The put() method has been called..." ); // The put() method returns the // CacheRequest for recording System.out.println( "The put() method returns: " + responseCache.put(uri1, urlcon)); System.out.println( "The get() method has been called..." ); // The get() method returns a CacheResponse // instance if it is available System.out.println( "The get() method returns: " + responseCache.get(uri1, uri, maps)); } } |
Output :