Ajp Exp 40 Op
Ajp Exp 40 Op
Ajp Exp 40 Op
Practical Code:
1. Execute the following code and write the output(Refer manual pg. no. 81).
import java.net.*;
class URLDemo {
public static void main(String args[]) throws MalformedURLException {
URL hp = new URL("https://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());
}
}
XIII. Exercise:
1. Write a program using URL class to retrieve the host, protocol, port and file of URL
http://www.msbte.org.in .
import java.net.*;
public class Sample {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("https://msbte.org.in/");
System.out.println("Authority: " + url.getAuthority());
System.out.println("Default Port: " + url.getDefaultPort());
System.out.println("File: " + url.getFile());
System.out.println("Path: " + url.getPath());
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Reference: " + url.getRef());
}
}
2. Write a program using URL and URLConnection class to retrieve the date, content
type, content length information of any entered URL.
import java.io.*;
import java.net.*;
import java.util.*;
public class Sample1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String ipStr = "";
String hostnameStr = "";
try {
InetAddress inet = InetAddress.getLocalHost();
ipStr = "IP Address: " + inet.getHostAddress();
hostnameStr = "Hostname: " + inet.getHostName();
} catch (IOException e) {
ipStr = "IP Address: Unable to retrieve";
hostnameStr = "Hostname: Unable to retrieve";
}
String dateStr = new Date().toString();
System.out.println(dateStr);
System.out.println(ipStr);
System.out.println(hostnameStr);
System.out.print("Enter the URL: ");
String urlString = scanner.nextLine();
try {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.connect();
long dateMillis = connection.getHeaderFieldDate("Date", 0);
Date date = new Date(dateMillis);
System.out.println("Date: " + date);
String contentType = connection.getContentType();
System.out.println("Content-Type: " + contentType);
int contentLength = connection.getContentLength();
System.out.println("Content-Length: " + contentLength);
} catch (IOException e) {
System.out.println("Error fetching URL information: " + e.getMessage());
} finally {
scanner.close();
}
}
}