Skip to main content

Java URL Connection Interview Questions

Explain the difference between a URL instance and a URL connection instance.

URL instance represents the location of a resource, and a URLConnection instance represents a link for accessing or communicating with the resource at the location

Explain how to make a connection to a URL.

Connection to the remote object represented by the URL is only initiated when the connect() method of the URLConnection is called. Doing this initializes a communication link between your Java program and the URL over the network.

The following code show how a connection to a URL is made.

try
{
URL xyz = new URL(http://www.xyz.com/);
URLConnection xyzConnection = xyz.openConnection();
xyzConnection.connect();
}

catch (MalformedURLException e)
{ // new URL() failed
. . .
} catch (IOException e)
{ // openConnection() failed
. . .
}

Explain how to read from a remote file when we have its URL.

With the help of the following code you could directly read from the URL:

import java.net.*;
import java.io.*;

public class URLReader {
public static void main(String[] args) throws Exception {
URL xyz = new URL(http://www.xyz.com/);
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

In the above program, the openStream() method gets an input stream.
The openStream() gets an input stream on the URL specified. A BufferedReader is then opened on the input stream and the URL is read from the BufferedReader.

Another way to do this is:

import java.net.*;
import java.io.*;

public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL xyz = new URL(http://www. xyz.com/);
URLConnection xyzcon = xyz.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
xyzcon.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

Here, a URLConnection object is explicitly retrieved and an input stream is fetched from the connection. The connection is opened implicitly by calling getInputStream. a BufferedReader is created on the input stream and is read from BufferedReader.

Explain the difference between a URL instance and a URL connection instance.

A URL instance is used to represents the location of a resource. On the other hand, a URL connection instance is used to represent a link for accessing communicating with the resource at the location. The URL class provides an abstraction of a URL the World Wide Web's pointer. While the URL instance specifies how to reach a resource.

What is Uniform Resource Identifier (URI)?

A URI is in the form of a string of characters for the purpose of identifying an abstract or physical resource. URIs provides a means that is simple and extensible for identifying a resource. A URI can be classified as a locator or a name. The URL is a subset URI which identifies the resources through a representation of their accessing mechanism. Some examples of URI are:

shttp://www.polyu.edu.hk
http://proxy.polyu.edu.hk:8181
ftp://ftp.isi.edu/in-notes/rfc2234.txt
ftp://yourusername:yourpassword@the.site.youwant/the/path/of/yourfile
mailto:entchsun@polyu.edu.hk

Discuss about Java URL handling architecture.

Java URL architecture is handled by the java.net.URL object instances. It is utilized to handle the URL string. The methods are of public access specifications which basically reflect the private fields of an object of URL. They also provide the access to the URL strings individual parts. Most of the methods are dedicated for data representation such as equals() to compare two URL objects, hashCode() which can be used to Map the key objects and saveFile() is used to compare two URL objects to see if everything is saved the "ref" field is same .

Brief about java.net.URL class.

URL is represented to identify or to point to a resource [a web portal / web page] on the WWW. A file or a directory or a reference to an object makes the resource for URL. An URL can optionally be specified a port number to which TCP connection is made to a remote host machine. If the port number or port or host name is not specified, the value is inherited for the specified by the URL.

Brief about java.net.HttpURLConnection class.

This class inherits from URLConnection. The HTTP specific features are supported by this class. Each instance of HttpURLConnection is used to make a single request, but HTTP server which is under the network connection can be transparent to the shared instances. A HttpURLConnection request may be freed by using close() method on the InputStream or the OutputStream. The underlying socket can be closed by invoking disconnect() method.

Brief about java.net.JarURLConnection class.

This class is used to establish a connection to a JAR file or to an entry of the JAR file. The syntax is

jar:<url>!/{entry} where the entry is an entry of the jar file such as .class

If the entry is omitted, it refers to the whole JAR file. The developer need to cast the generic URLConnection with JarURLConnecton like

JarURLConnection jarConnection = (JarURLConnection)url.openConnection();

Comments

Popular posts from this blog

Asynchronous Vs. Synchronous Communications

Synchronous (One thread):   1 thread -> |<---A---->||<----B---------->||<------C----->| Synchronous (multi-threaded):   thread A -> |<---A---->| \ thread B ------------> ->|<----B---------->| \ thread C ----------------------------------> ->|<------C----->|

WebSphere MQ Interview Questions

What is MQ and what does it do? Ans. MQ stands for MESSAGE QUEUEING. WebSphere MQ allows application programs to use message queuing to participate in message-driven processing. Application programs can communicate across different platforms by using the appropriate message queuing software products. What is Message driven process? Ans . When messages arrive on a queue, they can automatically start an application using triggering. If necessary, the applications can be stopped when the message (or messages) have been processed. What are advantages of the MQ? Ans. 1. Integration. 2. Asynchrony 3. Assured Delivery 4. Scalability. How does it support the Integration? Ans. Because the MQ is independent of the Operating System you use i.e. it may be Windows, Solaris,AIX.It is independent of the protocol (i.e. TCP/IP, LU6.2, SNA, NetBIOS, UDP).It is not required that both the sender and receiver should be running on the same platform What is Asynchrony? Ans. With messag...

Advantages & Disadvantages of Synchronous / Asynchronous Communications?

  Asynchronous Communication Advantages: Requests need not be targeted to specific server. Service need not be available when request is made. No blocking, so resources could be freed.  Could use connectionless protocol Disadvantages: Response times are unpredictable. Error handling usually more complex.  Usually requires connection-oriented protocol.  Harder to design apps Synchronous Communication Advantages: Easy to program Outcome is known immediately  Error recovery easier (usually)  Better real-time response (usually) Disadvantages: Service must be up and ready. Requestor blocks, held resources are “tied up”.  Usually requires connection-oriented protocol