Uniform Resource Locator (URL) signifies a source on the WWW (World Wide Web). A URL can be split up into protocol (http, https, etc), host (authority), port, path (filename), reference and query. URL Processing in Java is done using two classes namely URL Class and URLConnection class.
- URL Class
The java.net.URL class includes a collection of methods to operate URL in Java. The URL class contains numerous constructors for building URLs:
- public URL(String protocol,Stringhost, int port, String file) throws MalformedURLException and generates a URL by bringing the provided parts collectively.
- public URL(String protocol, String host, String file) throws MalformedURLException , here the standard port for a protocol is used.
- public URL(String url) throws MalformedURLException generates URL from a String.
- public URL(URL context, String url) throws MalformedURLException generates URL by syntactically analyzing the string and URL arguments together.
The URL class offers the following methods for executing different parts of the URL:
- public String getPath() delivers the path.
- public String getQuery() delivers the query portion.
- public String getAuthority() delivers the authority.
- public int getPort() delivers the port.
- public int getDefaultPort() delivers the standard port a protocol.
- public String getProtocol() delivers the protocol.
- public String getHost() delivers the host.
- public String getFile() delivers the filename.
- public String getRef() delivers the reference.
- public URLConnection openConnection() throws IOException develops a link to the URL, permitting a user-resource communication.
- URLConnection Class
The openConnection mode of URL class provides a URL Connection whose lower classes signify different types of URL connections. i.e., when a URL with http protocol is linked, openConnection mode delivers httpUrlConnection object.
This URLConnection class includes the following methods to fix or fetch information about the connection:
- Object getContent() and Object getContent(Class[] classes) are used for fetching the corresponding contents of the connection.
- Methods like (a) String getContentEncoding(), (b) int getContentLength(), (c) String getContentType(), (d) int getLastModified(), (e) long getExpiration(), (f) long getIfModifiedSince() deliver the corresponding estimates of the header field.
- public void setDoInput(boolean input) method generally has input value to be true as the users normally read from a URLConnection.
- public void setDoOutput(boolean output) method generally has input value to be false as several forms of URL do not accept writing.
- public InputStream getInputStream() throws IOException delivers the input stream to read from the source.
- public OutputStream getOutputStream() throws IOException delivers the output stream to write to the source.
- public URL getURL() delivers the URL, which is linked to the given URLConnection.
URL Processing in Java Example
Here is a simple example to demonstrate how a URL will be processed in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.IOException; import java.net.URL; public class URLProcessingExample { public static void main(String [] args) { try { URL url = new URL("http://209.97.166.197/category/design-patterns/"); System.out.println(url.toString() +" is the URL"); System.out.println(url.getProtocol()+" is the Protocol"); System.out.println(url.getAuthority()+" is the Authority"); System.out.println(url.getFile()+" is the File Name"); System.out.println(url.getHost()+" is the Host"); System.out.println(url.getPath()+" is the Path"); System.out.println(url.getPort()+" is the Port"); System.out.println(url.getDefaultPort()+" is the Default Port"); }catch(IOException e) { e.printStackTrace(); } } } |
Output will be:
https://j2eereference.com/category/design-patterns/ is the URL
https is the Protocol
j2eereference.com is the Authority
/category/design-patterns/ is the File Name
j2eereference.com is the Host
/category/design-patterns/ is the Path
-1 is the Port
443 is the Default Port
Leave a Reply