Communication technology between Java Applet and browsers

Source: Internet
Author: User

Abstract: This article studies the communication technology between Java Applet programs and Internet browsers, and focuses on the Usage skills of getparameter (), showstatus (), showdocument () and other methods.

Keywords: Java Applet, browser, Communication

 

When Java technology is used for software development, communication between Java Applet programs and Internet browsers is usually encountered. In Java. the Applet Class in the applet package provides many methods for communication with the browser, including getcodebase (), getdocumentbase (), getparameter (), getparameterinfo (), showstatus () and getappletinfo. In addition, there is the showdocument () method in the appletcontext class in the Java. Applet package.

This article focuses on the usage and skills of getparameter (), showstatus (), and showdocument.

1. getparameter () method

Java application accepts user parameters through the command line. In the applet, this task is completed by another special mark of the HTML file <param>. The following example shows how to use it:

<HTML>

<Body>

<APPLET code = "appletparam. Class" Height = 60 width = 300>

<Param name = varname value = "Liu">

<Param name = Varnum value = 30>

</APPLET>

</Body>

</Html>

This html file is embedded with an applet named appletparam. At the same time, two string parameters will be passed to the applet during the applet runtime: a parameter named varname with a value of "Liu "; the other parameter is Varnum and the value is 30. It can be seen that each <param> tag can only pass one character type parameter. The parameter name is used to distinguish it from other parameters and is specified by name, the parameter value of this parameter is specified with value.

The applet uses the getparameter () method to obtain parameters passed in HTML. The format of this method is:

String getparameter (string name );

The function of this method is to return the value set in HTML. The string parameter indicates the name of the HTML parameter to be obtained (that is, the parameter name specified by name ). The Return Value of the method is a string object, that is, the string specified by the value in the HTML file. If you want this parameter to be of another type, you need to convert the parameter type. When processing applet parameters, the default value is usually given to prevent the program from being executed because the user does not give this parameter or the invalid parameter value is given.

The following is the source code of applerparam. java. Observe how the applet obtains parameters from the HTML file.

Import Java. applet. applet; </P> <p> Import Java. AWT. graphics; </P> <p> public class appletparam extends applet {// defines the main class </P> <p> private string name; // program variable used to receive HTML parameters </P> <p> private int num; </P> <p> Public void Init () {</P> <p> name = getparameter ("varname"); // receives parameters passed in hmtl </P> <p> num = integer. parseint (getparameter ("Varnum"); </P> <p >}</P> <p> Public void paint (Graphics g) {</P> <p> G. drawstring ("hello" + name + "! Your are "+ num +" years old. ", 10, 20); </P> <p >}</P> <p>

Ii. showstatus () method

The applet can report the running status of the applet by displaying a brief status string. The status string is displayed at the bottom of the browser or appletviewer window. In a browser, all the applets on a web page share the same status display domain as the browser itself. The showstatus () method is used to display the status string of the applet. The showstatus () method is used to display information in the browser status bar. The format of this method is:

Void showstatus (string MSG );

The following example shows how to use the showstatus () method. The function of this program is to click the button to display the text in the TXT text box in the browser status bar.

Import Java. AWT. *; </P> <p> Import Java. AWT. event. *; </P> <p> Import Java. applet. *; </P> <p> public class uiapplet extends applet implements actionlistener {</P> <p> textfield txt; </P> <p> button; </P> <p> Public void Init () {</P> <p> TXT = new textfield (10); </P> <p> Add (txt ); // Add the text field component </P> <p> button = new button ("show"); </P> <p> button. addactionlistener (this); </P> <p> Add (button ); // Add a button </P> <p >}</P> <p> Public void actionreceivmed (actionevent E) {// event handling method for the response button event </P> <p> showstatus (txt. gettext (); // display the content of the text field in the status line </P> <p >}</P> <p>This information is usually temporary, so it should not be used to display important information or for debugging. Because the Status display domain location is not prominent and may be overwritten by browsers or other applets at any time, the status string is usually only auxiliary information that is not commonly used. For some important prompts and warnings, do not display them in the status line as a status string. They should be displayed in the applet area or standard output.

For important information or information to be used during debugging, the class system should be used to output it to the standard output, and the diagnostic information should be reported on the standard output for debugging the applet. In addition, some messages can be seen from the standard output when an uncaptured exception occurs in the applet lifecycle. The exact display position of the standard output is unknown. It depends on the implementation of the browser and the usage of the browser or appletveiwer. If the browser is used in the Unix shell window, the string displayed on the standard output will be output to the shell window, and the standard output will be sent to the console window when the browser is used in the X Window menu. Netscape navegator generally places the standard output of the applet in the Java Console window, which is displayed in options.

3. showdocument () method

The showdocument () method is the method of the appletcontext class. It can be used to request the browser to display the HTML file corresponding to a URL address and control the browser window to display. The format is as follows:

Void showdocument (URL );

Or

Void showdocument (URL, string target );

A showdocument () parameter indicates that the browser displays the relevant document at the specified URL. It does not indicate the window for displaying the document. The specified HTML file is displayed in the window where the applet is located.

Showdocurnent () with two parameters further specifies the window for displaying the document. The second parameter target can specify the window for displaying the applet, and the following values can be taken:

"_ Self" displays the document in the applet window.

"_ Parent" displays the document in the parent window of the applet window. If the applet is a top-level window, the effect is the same as "_ self ".

"_ Top" displays this document in the top-level window of the applet window.

"_ Blank" displays the document in a new untitled window.

"Windowname" displays the document in the window named windowname, which can be generated as needed.

The following program displays the homepage of Yantai University in the current browser window.

Import Java. applet. *; </P> <p> Import java.net. URL; </P> <p> Import java.net. malformedurlexception; </P> <p> public class SD extends applet {</P> <p> Public void Init () {</P> <p> string us = "http://www.ytu.edu.cn"; // www.ytu.edu.cn is the home address of Yantai University </P> <p> URL u = NULL; </P> <p> appletcontext AC = getappletcontext (); // define the object ac of the appletcontext class </P> <p> try {</P> <p> U = new URL (US ); </P> <p >}catch (malformedurlexception e) {</P> <p> System . Err. println ("malformed URL:" + US); </P> <p >}</P> <p> If (u! = NULL) {</P> <p> AC. showdocument (U ); // display the homepage of Yantai University in the current browser </P> <p >}</P> <p>If you set the statements in the above program

AC. showdocument (U );

Change

AC. showdocument (u, "_ blank ");

Then the program will keep the original browser window and open a new browser to display the homepage of Yantai University.

References

1. Liu qicheng and others. Basic tutorial on Java language. University of Petroleum Press, 2001

2. Liu qicheng and others. Java-based DHTML technology and its applications. minicomputer and application. 2000, 12

3. Liu qicheng and others. Object-oriented Flexible Design and Control Theory. Computer Engineering and application. Vol 38, No. 18

4. Liu qicheng and others. Design and Implementation of chat rooms based on Java Datagram communication technology. Information technology. 2002, 8

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.