Java Program for Showing the Use of Various Methods of URL Class

«
»

This is a Java Program for Showing the Use of Various Methods of URL Class

Problem Description

We have to write a program in Java such that it illustrates the use of methods of URL class using an applet.

Expected Input and Output

For illustrating methods of URL class, we can have the following different sets of input and output.

advertisement

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:

advertisement

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:

advertisement

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"
Problem Solution

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.

Program/Source Code

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.

  1. /*Java Program to Illustrate the Use of Methods of URL Class using Applet*/
  2. /*Java Applet to demonstrate the URL Class*/
  3. import java.net.*;
  4. import java.applet.*;
  5. import java.awt.event.*;
  6. import java.awt.*;
  7. public class URL_Class extends Applet implements ActionListener
  8. {
  9.     TextField url_field;
  10.     TextField out;
  11.     //Function to initialize the applet
  12.     public void init()
  13.     {
  14. 	setBackground(Color.white);
  15. 	setLayout(null);
  16. 	//Create a label
  17. 	Label label = new Label();
  18. 	label.setText("Enter an URL : ");
  19. 	label.setBounds(50,50,100,50);
  20. 	this.add(label);
  21. 	//Create a text field
  22. 	url_field = new TextField();
  23. 	url_field.setBounds(150,50,250,50);
  24. 	this.add(url_field);
  25. 	//Create a textfield for output
  26. 	out = new TextField();
  27. 	out.setBounds(100,250,300,50);
  28. 	this.add(out);
  29.     }
  30.     //Function to set features to the applet
  31.     public void start()
  32.     {
  33. 	//Create buttons for method options
  34. 	Button protocol = new Button("Get Protocol");
  35. 	protocol.setBounds(0,150,100,50);
  36. 	protocol.addActionListener(this);
  37. 	this.add(protocol);
  38. 	Button host = new Button("Get Host");
  39. 	host.setBounds(100,150,100,50);
  40. 	host.addActionListener(this);
  41. 	this.add(host);
  42. 	Button port = new Button("Get Port");
  43. 	port.setBounds(200,150,100,50);
  44. 	port.addActionListener(this);
  45. 	this.add(port);
  46. 	Button query = new Button("Get Query");
  47. 	query.setBounds(300,150,100,50);
  48. 	query.addActionListener(this);
  49. 	this.add(query);
  50. 	Button file = new Button("Get File");
  51. 	file.setBounds(400,150,100,50);
  52. 	file.addActionListener(this);
  53. 	this.add(file);
  54.     }
  55.     //Function to perform the option selected
  56.     public void actionPerformed(ActionEvent e)
  57.     {
  58. 	String button = e.getActionCommand();
  59. 	try
  60. 	{
  61. 	    URL url = new URL(url_field.getText());
  62.             if(button.equals("Get Protocol"))
  63. 		out.setText("Protocol : "+url.getProtocol());
  64.  
  65. 	    else if(button.equals("Get Host"))
  66. 		out.setText("Host :"+url.getHost());
  67.  
  68. 	    else if(button.equals("Get Port"))
  69. 		out.setText("Port : "+url.getPort());
  70.  
  71. 	    else if(button.equals("Get Query"))
  72. 		out.setText("Query String : "+url.getQuery());
  73.  
  74.             else
  75. 		out.setText("File : "+url.getFile());
  76. 	}
  77. 	catch(Exception exc)
  78. 	{
  79. 	    out.setText(exc.getMessage());
  80. 	}		
  81.     }
  82. }
  83. /*
  84. <applet code = URL_Class.class width=500 height=500>
  85. </applet>
  86. */

To compile and run the applet use the following commands :

>>>javac URL_Class.java
>>>appletviewer URL_Class.java
Program Explanation

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.

advertisement
Runtime Test Cases

Here’s the run time test cases for illustrating the methods of URL class.

Test case 1 – To Get the Protocol of the URL
java-applet-url-class-protocol

Test case 2 – To Get the Host of the URL
java-applet-url-class-host

Test case 3 – To Get the Port of the URL
java-applet-url-class-port

Test case 4 – To Get the Query String of the URL
java-applet-url-class-query

Test case 5 – To Get the File of the URL
java-applet-url-class-file

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn