Mega Code Archive

 
Categories / Java Tutorial / Network
 

Parsing a URL

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 () import java.net.URL; public class MainClass {   public static void main(String[] args) throws Exception {     URL url = new URL("http://www.yahoo.com:80/en/index.html?name=joe#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());   } } protocol:http prot:80 host:www.yahoo.com path:/en/index.html file:/en/index.html?name=joe query:name=joe ref:first