This is a Java Program for Showing the Use of Various Methods of URL Class
We have to write a program in Java such that it illustrates the use of methods of URL class using an applet.
For illustrating methods of URL class, we can have the following different sets of input and output.
1. To Get Protocol from the URL:
When the user enters an URL and selects the Get Protocol option,
then it is expected that the protocol of the URL is displayed.
For example, given url is "https://www.google.com/search?client=firefox-b-d&q=java+founder" then it is expected that the protocol of the URL is "https"
2. To Get Host from the URL:
When the user enters an URL and selects the Get Host option,
then it is expected that the host of the URL is displayed.
For example, given url is "https://www.google.com/search?client=firefox-b-d&q=java+founder" then it is expected that the host of the URL is "www.google.com"
3. To Get Port from the URL:
When the user enters an URL and selects the Get Port option,
then it is expected that the port of the URL is displayed.
For example, given url is "https://www.google.com/search?client=firefox-b-d&q=java+founder" then it is expected that the port of the URL is -1
4. To Get Query from the URL:
When the user enters an URL and selects the Get Query option,
then it is expected that the query string of the URL is displayed.
For example, given url is "https://www.google.com/search?client=firefox-b-d&q=java+founder" then it is expected that the query string of the URL is "client=firefox-b-d&q=java+founder"
5. To Get File from the URL:
When the user enters an URL and selects the Get File option,
then it is expected that the file of the URL is displayed.
For example, given url is "https://www.google.com/search?client=firefox-b-d&q=java+founder" then it is expected that the file of the URL is "File : /search?client=firefox-b-d&q=java+founder"
1. Create a text field for the user to enter an URL address.
2. Create a text field to display the output.
3. Create buttons to perform the operations.
4. Perform the respective operations using the methods of URL class.
Here is source code of the Java Program to illustrate the use of methods of URL class using applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/*Java Program to Illustrate the Use of Methods of URL Class using Applet*/
/*Java Applet to demonstrate the URL Class*/
import java.net.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class URL_Class extends Applet implements ActionListener
{
TextField url_field;
TextField out;
//Function to initialize the applet
public void init()
{
setBackground(Color.white);
setLayout(null);
//Create a label
Label label = new Label();
label.setText("Enter an URL : ");
label.setBounds(50,50,100,50);
this.add(label);
//Create a text field
url_field = new TextField();
url_field.setBounds(150,50,250,50);
this.add(url_field);
//Create a textfield for output
out = new TextField();
out.setBounds(100,250,300,50);
this.add(out);
}
//Function to set features to the applet
public void start()
{
//Create buttons for method options
Button protocol = new Button("Get Protocol");
protocol.setBounds(0,150,100,50);
protocol.addActionListener(this);
this.add(protocol);
Button host = new Button("Get Host");
host.setBounds(100,150,100,50);
host.addActionListener(this);
this.add(host);
Button port = new Button("Get Port");
port.setBounds(200,150,100,50);
port.addActionListener(this);
this.add(port);
Button query = new Button("Get Query");
query.setBounds(300,150,100,50);
query.addActionListener(this);
this.add(query);
Button file = new Button("Get File");
file.setBounds(400,150,100,50);
file.addActionListener(this);
this.add(file);
}
//Function to perform the option selected
public void actionPerformed(ActionEvent e)
{
String button = e.getActionCommand();
try
{
URL url = new URL(url_field.getText());
if(button.equals("Get Protocol"))
out.setText("Protocol : "+url.getProtocol());
else if(button.equals("Get Host"))
out.setText("Host :"+url.getHost());
else if(button.equals("Get Port"))
out.setText("Port : "+url.getPort());
else if(button.equals("Get Query"))
out.setText("Query String : "+url.getQuery());
else
out.setText("File : "+url.getFile());
}
catch(Exception exc)
{
out.setText(exc.getMessage());
}
}
}
/*
<applet code = URL_Class.class width=500 height=500>
</applet>
*/
To compile and run the applet use the following commands :
>>>javac URL_Class.java >>>appletviewer URL_Class.java
1. To create an URL use the URL class.
2. To get the protocol of the URL use getProtocol method.
3. To get the host of the URL use getHost method.
4. To get the port of the URL use getPort method.
5. To get the query string of the URL use getQuery method.
6. To get the file of the URL use getFile method.
Here’s the run time test cases for illustrating the methods of URL class.
Test case 1 – To Get the Protocol of the URL
Test case 2 – To Get the Host of the URL
Test case 3 – To Get the Port of the URL
Test case 4 – To Get the Query String of the URL
Test case 5 – To Get the File of the URL
Sanfoundry Global Education & Learning Series – Java Programs.
If you find any mistake above, kindly email to [email protected]- Practice BCA MCQs
- Check Java Books
- Apply for Java Internship
- Practice Programming MCQs
- Apply for Computer Science Internship