This section illustrates you the procedure of viewing complete html code of a page i.e. running on the server. This section provides you an example with the complete code of the program which views the html source code of your given page running on server. Following program strictly takes a url (Uniform Resource Locator) started with the "http://" otherwise it will generate an error which is caught by the MalformedURLException in catch block.
Here is the code of the program:
Following program takes an url name which should be started with "http://" otherwise it will generate an error like this:
Enter url of local for viewing html source code: www.google.com
www.google.com is not a valid URL.
And if the file name (given through the url) exists then the program gives you the complete html source code of the file.
Various methods and APIs have been used in the above program. These are illustrated as follows:
This is the abstract class of the java.net package. This class extends the URLConnection class of the java.net package and facilitates all same facilities of the URLConnection class with the specific HTTP features specially.
This is the method of the HttpURLConnection class that returns a integer value which is the code for the status from the http response message.
This is also the method of the HttpURLConnection class that show the message with the http response code from the server.
Above method returns the key for the given jth header field. Given j is the argument of the method. And this method returns the String type value that will be the key value.
Output of the program:
Enter url of local for viewing html source code: http://192.168.10.3/
HTTP/1.x 200 OK
Date: Sat, 23 Dec 2006 12:15:34 GMT
Server: Apache/2.0.55 (Win32) PHP/5.0.0
Last-Modified: Sun, 21 Nov 2004 14:35:22 GMT
Keep-Alive: timeout=15, max=100
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<title>Test Page for Apache Installation</title>
<!-- Background white, links blue (unvisited), navy (visited), red
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF"
<p>If you can see this, it means that the installation of the <a
server</a> software on this system was successful. You may now add
content to this directory and replace this page.</p>
<hr width="50%" size="8" />
<h2 align="center">Seeing this instead of the website you
<p>This page is here because the site administrator has changed the
configuration of this web server. Please <strong>contact the person
responsible for maintaining this server with questions.</strong>
The Apache Software Foundation, which wrote the web server software
this site administrator is using, has nothing to do with
maintaining this site and cannot help resolve configuration
<hr width="50%" size="8" />
<p>The Apache <a href="manual/">documentation</a> has been included
with this distribution.</p>
<p>You are free to use the image below on an Apache-powered web
server. Thanks for using Apache!</p>
<div align="center"><img src="apache_pb.gif" alt="" /></div>
Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles