This section illustrates you the procedure of viewing complete html code of a page i.e. running on the server.
Downloading and Viewing html source of a page i.e. running on the server
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:
import java.io.*;
|
Program Description:
Following program takes an url name which should be started with "http://" otherwise it will generate an error like this:
C:\work\chandan>java SourceViewer 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.
Code Description:
Various methods and APIs have been used in the above program. These are illustrated as follows:
HttpURLConnection:
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.
getResponseCode():
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.
getResponseMethod():
This is also the method of the HttpURLConnection class that show the message
with the http response code from the server.
getHeaderFieldKey(int j):
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:
C:\work\chandan>javac SourceViewer.java C:\work\chandan>java SourceViewer 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 Content-Location: index.html.en Vary: negotiate,accept-language,accept-charset TCN: choice Last-Modified: Sun, 21 Nov 2004 14:35:22 GMT ETag: "13745-5d6-a659be80;1375b-9dc-a659be80" Accept-Ranges: bytes Content-Length: 1494 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/html Content-Language: en <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Test Page for Apache Installation</title> </head> <!-- Background white, links blue (unvisited), navy (visited), red (active) --> <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#000080" alink="#FF0000"> <p>If you can see this, it means that the installation of the <a href="http://www.apache.org/foundation/preFAQ.html">Apache web 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 expected?</h2> <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 issues.</p> <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> </body> </html> C:\work\chandan>_ |