Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URLConnection setDefaultUseCaches(String protocol, boolean defaultVal)



Description

The Java URLConnection setDefaultUseCaches(String protocol, boolean defaultVal) method sets the default value of the useCaches field for the named protocol to the given value. This value overrides any default setting set by setDefaultUseCaches(boolean) for the given protocol. Successive calls to this method change the setting and affect the default value for all future connections of that protocol. The protocol name is case insensitive.

Declaration

Following is the declaration for java.net.URLConnection.setDefaultUseCaches(String protocol, boolean defaultVal) method

public static void setDefaultUseCaches(String protocol, boolean defaultVal)

Parameters

protocol − the protocol to set the default for

defaultVal −whether caching is enabled by default for the given protocol

Return Value

NA

Exception

NA

Example 1

The following example shows the usage of Java URLConnection setDefaultUseCaches() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getDefaultUseCaches(String protocol), we're getting the default value of the useCaches flag of URLConnection instance and printing the same. Then using setDefaultUseCaches() method we've changed the value of useCaches flag to false and printed the same again −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(URLConnection.getDefaultUseCaches("https"));
         URLConnection.setDefaultUseCaches("https", false);
         System.out.println(URLConnection.getDefaultUseCaches("https"));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

true
false

Example 2

The following example shows the usage of Java URLConnection setDefaultUseCaches() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getDefaultUseCaches(), we're getting the default value of the useCaches flag of URLConnection instance and printing the same. Then using setDefaultUseCaches() method we've changed the value of useCaches flag to false and printed the same again −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(URLConnection.getDefaultUseCaches("http"));
         URLConnection.setDefaultUseCaches("http", false);
         System.out.println(URLConnection.getDefaultUseCaches("http"));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

true
false

Example 3

The following example shows the usage of Java URLConnection setDefaultUseCaches() method for a url of google with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getDefaultUseCaches(), we're getting the default value of the useCaches flag of URLConnection instance and printing the same. Then using setDefaultUseCaches() method we've changed the value of useCaches flag to false and printed the same again −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(URLConnection.getDefaultUseCaches("http"));
         URLConnection.setDefaultUseCaches("http", false);
         System.out.println(URLConnection.getDefaultUseCaches("http"));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

true
false
java_urlconnection.htm
Advertisements