0% found this document useful (0 votes)
11 views4 pages

ajp_prac_15

Uploaded by

saeedarwatkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

ajp_prac_15

Uploaded by

saeedarwatkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical No.

15

 Program code: Execute the following code and write the output.
import java.net.*;

class URLDemo

public static void main(String args[])throws MalformedURLException

URL hp=new URL("http://www.javatpoint.com/javafx-tutorial");

System.out.println("Protocol : "+hp.getProtocol());

System.out.println("Port : "+hp.getPort());

System.out.println("Host : "+hp.getHost());

System.out.println("File : "+hp.getFile());

System.out.println("Ext : "+hp.toExternalForm());

Output:
C:\Internship\url>javac URLDemo.java

C:\Internship\url>java URLDemo

Protocol : http

Port : -1

Host : www.javatpoint.com

File : /javafx-tutorial

Ext : http://www.javatpoint.com/javafx-tutorial
 Exercise:

1) Write a program using URL class to retrieve the host, protocol, port and file of the URL
http://www.msbte.org.in

import java.net.*;

class saee_15_1

public static void main(String args[])throws MalformedURLException

URL hp=new URL("http://www.msbte.org.in ");

System.out.println("Protocol : "+hp.getProtocol());

System.out.println("Port : "+hp.getPort());

System.out.println("Host : "+hp.getHost());

System.out.println("File : "+hp.getFile());

Output:
C:\Internship\url>javac saee_15_1.java

C:\Internship\url>java saee_15_1

Protocol : http

Port : -1

Host : www.msbte.org.in

File :
2) Write a program using URL and URLConnection class to retrieve the data, content
type, content length information of any entered URL.
import java.io.*;

import java.net.*;

class saee_15_2

public static void main(String args[]) throws MalformedURLException, IOException

URL ur1 = new URL("http://www.msbte.org.in");

URLConnection obj = ur1.openConnection();

long d = obj.getDate();

if (d == 0) {

System.out.println("Date is not available");

} else {

System.out.println("Date: " + d);

d = obj.getExpiration();

if (d == 0) {

System.out.println("Expiration is not available");

} else {

System.out.println("Expiration date: " + d);

d = obj.getLastModified();
if (d == 0) {

System.out.println("Last Modification is not available");

} else {

System.out.println("Last Modified: " + d);

System.out.println("Content type: " + obj.getContentType());

int l = obj.getContentLength();

if (l == -1) {

System.out.println("Content length is not available");

} else {

System.out.println("Content length: " + l);

Output:
C:\Internship\url>javac saee_15_2.java

C:\Internship\url>java saee_15_2

Date: 1728061416000

Expiration is not available

Last Modified: 1728023909000

Content type: text/html

Content length: 833425

You might also like