4/13/23, 8:32 PM Java URL class- javatpoint
[Link]
3 min read
Java URL class- javatpoint
next → ← prev
The Java URL class represents an URL. URL is an acronym for Uniform Resource
Locator. It points to a resource on the World Wide Web. For example:
1. https:
A URL contains many information:
1. Protocol: In this case, http is the protocol.
2. Server name or IP Address: In this case, [Link] is the server name.
3. Port Number: It is an optional attribute. If we write
http//[Link]/sonoojaiswal/ , 80 is the port number. If port number is
not mentioned in the URL, it returns -1.
4. File Name or directory name: In this case, [Link] is the file name.
Constructors of Java URL class
URL(String spec)
Creates an instance of a URL from the String representation.
[Link] 1/5
4/13/23, 8:32 PM Java URL class- javatpoint
Video Player is loading.
Current Time 0:00
Duration 18:10
Loaded: 0.37%
Stream Type LIVE
Remaining Time 18:10
URL(String protocol, String host, int port, String file)
Creates an instance of a URL from the given protocol, host, port number, and file.
URL(String protocol, String host, int port, String file, URLStreamHandler handler)
Creates an instance of a URL from the given protocol, host, port number, file, and handler.
URL(String protocol, String host, String file)
Creates an instance of a URL from the given protocol name, host name, and file name.
URL(URL context, String spec)
Creates an instance of a URL by parsing the given spec within a specified context.
URL(URL context, String spec, URLStreamHandler handler)
[Link] 2/5
4/13/23, 8:32 PM Java URL class- javatpoint
Creates an instance of a URL by parsing the given spec with the specified handler within
a given context.
Commonly used methods of Java URL class
The [Link] class provides many methods. The important methods of URL class are
given below.
Method Description
public String getProtocol() it returns the protocol of the URL.
public String getHost() it returns the host name of the URL.
public String getPort() it returns the Port Number of the URL.
public String getFile() it returns the file name of the URL.
public String getAuthority() it returns the authority of the URL.
public String toString() it returns the string representation of the URL.
public String getQuery() it returns the query string of the URL.
public String getDefaultPort() it returns the default port of the URL.
public URLConnection it returns the instance of URLConnection i.e. associated
openConnection() with this URL.
public boolean equals(Object obj) it compares the URL with the given object.
public Object getContent() it returns the content of the URL.
public String getRef() it returns the anchor or reference of the URL.
public URI toURI() it returns a URI of the URL.
Example of Java URL class
[Link] 3/5
4/13/23, 8:32 PM Java URL class- javatpoint
1.
2. import [Link].*;
3. public class URLDemo{
4. public static void main(String[] args){
5. try{
6. URL url=new URL("[Link]
7.
8. [Link]("Protocol: "+[Link]());
9. [Link]("Host Name: "+[Link]());
10. [Link]("Port Number: "+[Link]());
11. [Link]("File Name: "+[Link]());
12.
13. }catch(Exception e){[Link](e);}
14. }
15. }
Test it Now
Output:
Protocol: http
Host Name: [Link]
Port Number: -1
File Name: /java-tutorial
Let us see another example URL class in Java.
1.
2. import [Link].*;
3. public class URLDemo{
4. public static void main(String[] args){
5. try{
6. URL url=new URL("[Link]
q=javatpoint&oq=javatpoint&sourceid=chrome&ie=UTF-8");
7.
8. [Link]("Protocol: "+[Link]());
9. [Link]("Host Name: "+[Link]());
[Link] 4/5
4/13/23, 8:32 PM Java URL class- javatpoint
10. [Link]("Port Number: "+[Link]());
11. [Link]("Default Port Number: "+[Link]());
12. [Link]("Query String: "+[Link]());
13. [Link]("Path: "+[Link]());
14. [Link]("File: "+[Link]());
15.
16. }catch(Exception e){[Link](e);}
17. }
18. }
Output:
Protocol: https
Host Name: [Link]
Port Number: -1
Default Port Number: 443
Query String: q=javatpoint&oq=javatpoint&sourceid=chrome&ie=UTF-8
Path: /search
File: /search?q=javatpoint&oq=javatpoint&sourceid=chrome&ie=UTF-8
Next TopicURLConnection class
← prev next →
Generated with Reader Mode
[Link] 5/5