5. Display other HTML documents on the network
Use GetAppletContext () provided by Java. ShowDocument (URL)
Show other nodes of HTML documents, the image of other nodes on the network,
There are two formats, one of the following:
● Program 8 format one
Import java.applet. *;
Import java.awt. *;
Import java.net. *;
Public Class showdoc Extends applet
{
URL Docur = NULL;
Public void paint (graphics g) {
Try {
Docur = New URL ("http://www.shu.edu.cn/~xyx/doc/manhua.html");
}
Catch (Malformedurlexcection E) {
System.out.println ("Can't Open THE URL");
}
IF (docur! = null) {
GetAppletContext (). showdocument (Docur, "_ blank");
}
}
}
● Procedure 9 format two
Import java.applet. *;
Import java.awt. *;
Import java.net. *;
Public class showdoc2 extends applet
{
URL Docur = NULL;
Public void paint (graphics g) {
Try {
GetAppletContext (). showdocument (new url ("http://www.shu.edu.cn/
~ XYX / DOC / MANHUA.HTML ");
}
Catch (Malformedurlexcection E) {
System.out.println ("Can't Open THE URL");
}
}
}
6. Read the contents of the file on the network
The aforementioned network function is just an image, sound and an image, sound and
The HTML document does not process its content. In fact, Java can also read the network.
The content of the file is handled, and its content is processed.
The steps to read the contents of the file on the network can be as follows:
1. Create an object of a URL type
Such as:
String Url = "ftp://202.120.127.218/incoming/test/readtxt.html";
URL fileur;
Try {
Fileur = New URL (URL);
Catch (Malformedurlexcection E) {
System.out.println ("CAN't Get URL:");
}
2. Use the OpenStream () of the URL class to obtain the object of the corresponding inputStream class
Such as:
InputStream filecon = file.openstream ();
3. Translate the InputStream object to the object of the DataInputStream class
Such as:
DataInputStream FileData = New DataInputStream (FileCon);
4. Read the content
For example, the front of FileData can be read in one line of FileData.Readline ()
Take the content, or use a flydata.readchar a character to read the content. Correct
The content read, can be processed by the Java Applet, and use the results
Various ways are shown.
The following example is to read an example of the content of http://www.shu.edu.cn/~xyx/doc/manhua.html file, for the sake of simplicity, this example only reads the contents of the file,
And displayed in the text area.
● Procedure 10
Import java.io. *;
Import java.net. *;
Import java.awt. *;
Import java.applet. *;
Public class showfile extends applet {
URL fileur;
Textarea Showarea = New Textarea ("please wait a while for get
TEXT ", 10, 70);
Public void init () {
String Url = "http://www.shu.edu.cn/~xyx/doc/manhua.html";
Try {fileur = new url (url);}
Catch (Malformedurlexcection E) {
System.out.println ("CAN't Get URL:");
}
Add (Showarea);
}
Public void paint (graphics g) {
InputStream filecon = null;
DataInputStream fileData = null;
String fibeline;
Try {
Filecon = fileur.openStream ();
FileData = New DataInputStream (FileCon);
While (FileLine = fileData.readline ())! = null) {
Showarea.AppendText (fileline "/ n");
}
}
Catch (IOException E) {
System.out.println ("Error In I / O:" E.getMessage ());
}
}
}
Seven, dynamically use the network resources
On the basis of the examples described earlier, resources on the network can be dynamically utilized.
Its method is to prepare a thread and automatically read the latest nodes every other time.
content. This article is no longer expanded in this article, and readers can refer to the article or direct set.
Use the following example.
For example, read http://www.shu.edu.cn/~xyx/doc/manhua
. Example of the contents of .html file, add the thread as shown below. This example is updated every 5 seconds
One data. If http://www.shu.edu.cn/~xyx/doc/manhua.html
Some of the faster information such as the stock market, and there are programs to dynamically
Update its content, add this Java Applet in the Web, allows the traffickeeper to get
Dynamic information. Further, the data can be processed in the program, and graphical
Mode display processing results. For example, draw data into a curve, an evanger
See the curve of dynamic changes.
// Program 11
Import java.io. *;
Import java.net. *;
Import java.awt. *;
Import java.applet. *;
Public class Dynashow Extends Java.Applet.Applet
Implements runnable {
Thread Dthread;
URL fileur;
Textarea Showarea = New Textarea ("Wait for A While ...", 10, 70);
Public void init () {string url = "http://www.shu.edu.cn/~xyx/doc/manhua.html";
Try {fileur = new url (url);}
Catch (Malformedurlexcection E) {
System.out.println ("CAN't Get URL:");
}
Add (Showarea);
}
Public void start () {
IF (DTHREAD == NULL)
{
Dthread = New Thread (this);
Dthread.start ();
}
}
Public void stop () {
IF (DTHREAD! = null) {
Dthread.stop ();
Dthread = NULL;
}
}
Public void run () {
InputStream filecon = null;
DataInputStream fileData = null;
String fibeline;
While (true) {
Try {
Filecon = fileur.openStream ();
FileData = New DataInputStream (FileCon);
While (FileLine = fileData.readline ())! = null) {
Showarea.AppendText (fileline "/ n");
}
}
Catch (IOException E) {
System.out.println ("Error In I / O:" E.getMessage ());
}
Try {
DThread.sleep (5000);
}
Catch (InterruptedExcect E) {}
Repaint ();
}
}
}
Eight, Java network capabilities limit
For security considerations, Java Applet can only be used when browsing with Netscape.
Its host has established a connection, so most of the procedures can be stored after compilation
On http://www.shu.edu.cn/~xyx, on the host. When stored in other hosts
You need to change the node address in the program. Otherwise, the browser will display the security error.
But there is no such limit on other HTML documents on the network, such as programs 8, 9), read
You can make the program after compiling the program to any WWW server or FTP server can be transported.
Row.
In addition, when the browser opens the HTML document that calls Java Applet from this site,
Not limited by this. Therefore, all the programs in this article can be stored in this site compilation, only
To open with Netscape's File / Open File menu, you can run correctly.
For another Java program - Java Application, there is no such limit,
For example, the program 10 reads the file content on the network, the corresponding Java Application can
As the following programming:
● Program 11
Import java.io. *;
Import java.net. *;
Import java.awt. *;
Class showfile2 {
Public static void main (string args []) {
InputStream filecon = null;
DataInputStream fileData = null;
String fibeline;
String Url = "http://www.shu.edu.cn/~xyx/doc/manhua.html";URL fileur
Try {
Fileur = New URL (URL);
Filecon = fileur.openStream ();
FileData = New DataInputStream (FileCon);
While (FileLine = fileData.readline ())! = null) {
System.out.println (fileline "/ n");
}
}
Catch (IOException E) {
System.out.println ("Error In I / O:" E.getMessage ());
}
}
}
Take it with ShowFile2.java store, compile with javac showfile2.java,
Simply execute "java showfile2" can be printed on the screen
Http://www.shu.edu.cn/~xyx/doc/manhua.html file content.
Nine, the method of creating a URL object
In the previous example, we are uniformly created using the New URL (URL string).
URL object. In fact, Java provides four forms of creating a URL object:
1.New URL (URL String) This format is used in this format, such as:
New url ("http://www.shu.edu.cn/~xyx/doc/manhua.html")
2.new URL (protocol, host name, file name or path), such as program 2
String Url = "http://www.shu.edu.cn/~xyx/img/shnet.jpg";
Image = GetImage (New URL (URL)); section can be changed to:
Image = GetImage (New URL ("http", "www.shu.edu.cn", "/ ~ xyx /img/shnet.jpg"));
3.new URL (protocol, host name, port number, file name or path) 1
Such as: New URL ("http", "www.shu.edu.cn", 80, "/ ~xyx/doc/manhua.html")
4.NEW URL (Benchmark URL, File Name or Path)
Ten, other methods of realizing network functions
The above emphasizes the use of Java's URL class to get sound from the network,
Programming method of icon, HTML document and file data. Java's network function is very powerful, except
In addition to the above, you can use the URLConnection class to achieve a wider range of network functions.
Can, send information to the CGI program on the WWW server; through Socket and
Serversocket class, you can write customer software and service software yourself, and you can
Design communication protocol.