Java network function and programming
Summary: Java language is the most popular programming language on the Internet. This article is aimed at Java's network function, which has made a preliminary introduction from the programming method such as image, sound, HTML document and text files from the Internet. The method of resources on the network has been introduced. A large number of concise understandable examples are provided. Key words: Java; Internet; Network Java language is an emerging programming language on the Internet. There are many articles for Java's characteristics and basic programming methods. However, the majority of Java enthusiasts more want to learn more about Java's deeper programming methods, this article, this article, a preliminary introduction to Java's network function and its programming method. In order to facilitate the first time to contact Java readers, this article briefly introduces some of Java programming. I. Introduction to Java Programming 1. Programming Environment: For most readers, the following configuration is a more economical option: operating system Win95 compile software JDK1.01 browsing software Netscape 2.0 or more (32-bit) 2. Programming method: first Enter the Java program with the text editor such as Edit, Notepad, etc., with .java as the file name suffix store. Run the command line again: Javac file name to compile the Java program. After compiling, generate the character code file that suffix .class. Finally, if you are Java Applitcation, execute the command line: Java byte file name to run the Java program. If it is a java applet, enter the HTML document of the Java Applet with the text editor, with .htm as the file name subsequent replication. Perform a command line: AppletViewer HTML file name to run Java Applet. Or open the HTML document with Netscape. 3. Description of the procedures in this article In order to make the program to be the most concise programming method, the procedures in this paper generally use the simplest form, omitting the thread and other content. Therefore, this document is not a good program, but it is easier to understand the beginner. After all the programs in this article, the generated byte code file and the corresponding HTML document are uploaded to http://www.shu.edu.cn/~xyx/test/jvnet, can run correctly. The reader connected to the Internet can open the address with the browser to view the running effect. Readers connecting the Internet can also enter and compile the program in the local hard drive. Open the HTML document with Netscape's File / Open File menu, experience the programming method and view the running effect. If the reader wants to put Java Applet on your host or other FTP server, use the HTTP protocol or FTP protocol in Netscape, for security restrictions, should be modified as follows: If the reader has an account on a WWW host You can do your personal homepage (usually in the user's root directory to create a WWW or public_html directory, HOMEPAGE's address is http: // Hostname / ~ Personal account), you can put the corresponding http://www.shu.edu in this article .cn / ~ xyx / part is modified to reader's own web node address, and then load the generated byte code file and the corresponding HTML document to your own node. If the reader's computer is connected to the Internet, you can also find an FTP node that can be uploaded, such as: ftp: //ftp.shnet.edu.cn/incoming, the corresponding http: //www.shu in this article. The edu.cn/~xyx/ section is changed to the address of the FTP node, and the generated byte code file and the corresponding HTML document are uploaded to the node to view the running effect.
If the reader's computer is not connected, you can run the web service software on a single machine such as WebStar for Win95, and modify the corresponding http: // www. Shu.edu.cn/~xyx/ part of this article to http: // Local The IP address forms to simulate network programming. Second, Java Network Functions and General Steps Java programs for access to networks can obtain resources such as images, sounds, HTML documents, and texts such as network nodes, and can process the obtained resources. For example, a Java program can read the latest data provided by a node every other time, and displayed in the form of a chart. On programming processing, it is generally manually made into a URL type object, and then obtains the resources represented by the object with the corresponding method (Method) in Java. Several examples of Java network functions are described below, and there are several different programming methods. Third, obtain image Java Applet from the network can directly obtain an image directly from the network and display it. In order to understand the programming method and from the local display image, we don't consider the network function first, let's take a simple image display: ● Program 1 Import java.applet. *; Import java.awt. *; public class imag0 extends applet {image image; public void init () {image = getImage (GetDocumentBase (), "Test.gif");} public void Paint (Graphics G) {g.drawImage (image, 0, 0 ,}} This is an example of the simplest acquisition and display image. In this example, first call the image Test.gif from the HTML document in the location (GetDocumentBase (), Image File Name). And thus generate an object image of an Image type, then display the image on the screen with DrawImage (Image, 0, 0, 0, THIS). If you want to get images from the other nodes on the network, the key is to create an object corresponding to other nodes on the network. Once the object of the iMage type is obtained, you can make any possible image operations. Java provides the following ways to create images corresponding to other nodes: GetImage (new url) can have two types: string url = "node URL"; image Image; try; (NEW URL (URL));} catCH (Exception E) {system.out.println ("CAN`t Open THE URL");} or url imgur = null; image image; try {imgur = new url ("knot Point URL ");} Catch (Malformedurlexce E) {system.out.println (" CAN`t Open THE URL ");} Image = GetImage (IMGUR); the previous format generates a URL object with the New URL (URL), And directly as the parameters of GetImage, the latter format first generates a URL object with the New URL (URL), and then passed to GetImage. The two formats are essentially the same. In both formats, parts generating the URL object are included in the try {obtaining URL object} catch (MalforMedurLexception E) {Error Tip}.
For example, you want to call the image of http://www.shu.edu.cn/~xyx/img/shnet.jpg, the first format is complete, as follows: ● Program 2 Import java.applet. *; Import Java .NET. *; Import java.awt. *; public class imag extends applet {image image; public void = "http://www.shu.edu.cn/~xyx/img/shnet. JPG "; try {image = getImage (new url (url));} Catch (Exception E) {}} PUBLIC VOID PAINT (GRAPHICS G) {g.drawImage (image, 0, 0, this);}} The complete procedures are the following procedures: ● Programs 3 Import java.applet. *; Import java.net. *; Import java.awt. *; Public class imag2 extends applet {image image; url imgur = null; public void init () {Try {imgur = new url ("http://www.shu.edu.cn/ ~xyx/iMG/SHNET.JPG");} catch (mALFORMEDURLEXCEPTION E) {system.out.println ("CAN`t Open ");} image = getImage (imgur);} public void Paint (graphics g) {g.drawImage (image, 0, 0, this);}} The two programs are respectively imag.java and IMAG2, respectively. Java store, execute javac imag.java and javac imag2.java, will get the built-generated iMag.class and imag2.class, and finally create the HTML document that calls these two Java Applets, such as the HTML document corresponding to iMag.class can be as follows :
example title> head> < Center>