JAVA programming Chapter 7 small application design and Network Programming

Source: Internet
Author: User

Outline:

1. Small applications: programs running on web pages or appletviewer that inherit the Applet or JApplet subclass. Differences between Applet and JApplet: the layout of the two is different. Applet adopts FlowLayout and JApplet adopts BorderLayout. The method for adding components is different. Applet. add (child) JApplet. getContentPane. add (child );

2. Life Cycle of a small application: This method is executed when the public void init () application is started for the first time; public void start () this method is executed when a small application is restored or re-accessed from a minimal point of view. This method is executed when a public void stop () Small application is minimized or when a connection is redirected. public void destroy () run this method when the application is closed. That is to say, when the page is opened for the first time, execute the init () method and start () method of the small application, and close the page to execute the destroy () method (if the page is displayed, execute the stop method ); the stop () method is executed when the page is redirected or minimized; the start () method is executed when the page is returned or restored. (It must be the start method after init, and the stop method before destroy ). Appletviewer usage: appletviewer **. html;

3. Functions of small applications: Drawing webpages, playing sounds, message responses, and network connections. You cannot call an API through an unauthorized network connection to read/write data.

4. Small Application Html Tag: CODE is the file name (class file) of a small application loaded on a webpage. codebase specifies the url to indicate the path of the class file, ARCHIVE specifies the jar compression document required by the small application, and the ALT attribute specifies the prompt text information (when applet is not supported ).

5. Pass parameters to the small application: <param name = "name" VALUE = "value"> between <applet> and </applet> in the HTML file mediation, small applications call java. applet. the Applet's public String getParameter (String name), for example, defining the parameter <param name = "name" VALUE = "value"> in HTML, the small application's Paint () call getParameter ("name") in the method to return the String value.

6. Both applications and small applications: class can be executed either by application or by applet. Inherits from JApplet and implements the main () method at the same time.

7. sound and image: supported by java. au. wav. mid. aif. aiff format sound AudioClip sound = getAudioClip (getDocumentBase (), "AudioName"); load images, Image class getImage (getCodeBase (), "t1.gif "); call drawImage () of Graphics in the paint Method ()

8. URL: the package java.net contains various types of network data processing classes. Commonly Used classes include InetAddress URL Socket ServerSocket, where java.net. InetAddress indicates the Internet address. The URL format is divided into five parts: Protocol host file port reference. The port number range is 0--65535, of which 1024 or less is a specific service. The client uses the port service provided by the host. Java.net. URL indicates the specific URL throwsMalformedURLException. Common method: getProtocol (). getHost (). getPort (). getFile (). getRef (). The advantage of using a URL is to use a URL to retrieve server data.

9. TCP-based network programming server and client for reliable data transmission through the TCP protocol. Relying on serversocket-like and socket-like methods, the data can be reliably transmitted on two hosts Based on socket communication, and the network connection is like file and I/O data streams.

Follow these steps to create a server: A establishes a serversocket instance on the server. B waits for the connection to return the socket object through the accept method. c obtains the inputstream and outputstream OF THE SOCKET through the getinputstream and getoutputstream methods. e closes streams and socket.

Four steps to create a client: A. Create a socket on the client. instance object B. Obtain inputstream and outputstream C of the socket. Process Communication Data. D. Disable streams and socket.

Supplement:

The smallest unit in the program is the statement. The program enters the main function and executes the statement and function call.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.