URL Components
In this chapter you will learn:
Get URL component
You can retrieve the various components of a URL object by using these methods:
public java.lang.String getFile ()
public java.lang.String getHost ()
public java.lang.String getPath ()
public int getPort ()
public java.lang.String getProtocol ()
public java.lang.String getQuery ()
The following code shows how to use the methods above.
import java.net.URL;
//from j ava2 s . c om
public class Main {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.java2s.com:80/en/index.html?name=demo#first");
System.out.println("protocol:" + url.getProtocol());
System.out.println("prot:" + url.getPort());
System.out.println("host:" + url.getHost());
System.out.println("path:" + url.getPath());
System.out.println("file:" + url.getFile());
System.out.println("query:" + url.getQuery());
System.out.println("ref:" + url.getRef());
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » URL URI