Magazine
 
Tips & Tricks
 

starting from “http://” otherwise it sends an error message indicating invalid url.

ViewSource.java

import java.io.*;
import java.net.*;
public class ViewSource {
public static void main (String[] args) throws
IOException{
System.out.print(“Enter url to view html
source code:”);
BufferedReader br = new
BufferedReader(new
InputStreamReader(System.in));
String url = br.readLine();
try{
URL u = new URL(url);
HttpURLConnection uc =
(HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
String response =
uc.getResponseMessage();
System.out.println(“HTTP/1.x “ + code +
“ “ + response);
for(int j = 1; ; j++){
String header = uc.getHeaderField(j);
String key = uc.getHeaderFieldKey(j);
if(header == null || key == null)
break;
System.out.println(uc.getHeaderFieldKey(j)
+ “: “ + header);
}
InputStream in = new
BufferedInputStream(uc.getInputStream());
Reader r = new InputStreamReader(in);
int c;
while((c = r.read()) != -1){
System.out.print((char)c);
}
}
catch(MalformedURLException ex){
System.err.println(url + “ is not a valid
URL.”);
}
catch(IOException ie){
System.out.println(“Input/Output Error: “
+ ie.getMessage());
}
}
}


 

In the program, HttpURLConnection 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() method of the HttpURLConnection class returns an integer value which is the code for the status from the http response message.
getResponseMethod() method of the HttpURLConnection class shows the message with the http response code from the server. getHeaderFieldKey(int j) method returns the key for the given jth header field.

Output of the program:

C:\JavaJazzUp>java ViewSource
Enter url of local for viewing html source
code: http://localhost:8080/JJU/index
.jsp
HTTP/1.x 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=
CC15E9CA0342CB43B2E2A3352E657528;
Path=/JJU
Content-Type: text/html;charset=ISO-8859-
1
Content-Length: 205
Date: Tue, 08 Jan 2008 05:59:07 GMT
<!DOCTYPE html PUBLIC “-//W3C//DTD
XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/
xhtml1-transitional.dtd”
>
<html>
<head>
<title>Index</title>
</head>
<body>
Hello........
</body>
</html>

3. Showing digital clock in Applet

This is an example of applet showing current time as in digital clock. This type of program is used to display the time on browser where your application is running.

In this example the time will be displayed in an

Feb 2008 | Java Jazz Up | 64
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 ,

Download PDF