java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.applet arrow Send a message from an Applet to another Applet on a different page
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Send a message from an Applet to another Applet on a different page E-mail
User Rating: / 7
PoorBest 

In this example, FirstApplet encodes the message for SecondApplet in the search (or query) section of SecondApplet.html URL. Then SecondApplet decodes the search section of its URL and extract the message coming from FirstApplet.

FirstAppletJ.html

<HTML><HEAD></HEAD><BODY>
<APPLET CODE="FirstAppletJ.class" 
        HEIGHT=100 
        WIDTH=300>
</APPLET></BODY></HTML>

FirstAppletJ.java

import java.applet.*;    
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class FirstAppletJ extends Applet implements
    ActionListener {
 Button b;
 TextField t;
 public void init() {
  add(new Label("Message to 2nd applet :"));
  add(t= new TextField(20));
  add(b = new Button("Load 2nd applet"));
  b.addActionListener(this);
  }

 public void actionPerformed(ActionEvent ae) {
  if (ae.getSource() == b) {
    try {
     getAppletContext().showDocument
       (new URL(getCodeBase()
         "SecondAppletJ.html?" "message=" + URLEncoder.encode(t.getText())));
     }
    catch (Exception e) {
     e.printStackTrace();
     }
    
   }
 }

SecondAppletJ.html

<HTML><HEAD></HEAD><BODY>
<APPLET CODE="SecondAppletJ.class" 
        HEIGHT=100 
        WIDTH=400>
</APPLET></BODY></HTML>

SecondApplet.java

import java.applet.*;
import java.awt.*;
import java.net.*;

public class SecondAppletJ extends Applet {
 public void init() {
   Label l = new Label("Message from 1st Applet");
   add (l);
   TextField tf = new TextField50 );
   add(tf);
   
   // complete current URL
   String s = getDocumentBase().toString();
   
   // extract the search (or query) section
   String theMessage = s.substring(s.indexOf('?'1);

   // remove message header
   theMessage = theMessage.substring("message=".length());

   // decode the string (incomplete)
   theMessage = theMessage.replace('+',' ');

   /* with JDK1.2, the decoding can be done with java.net.URLDecoder.decode(theMessage).
   
      you to convert from a MIME format called "x-www-form-urlencoded" to a String
      To convert to a String, each character is examined in turn:
      . The ASCII characters 'a' through 'z',
        'A' through 'Z', and '0'
        through '9' remain the same.
      . The plus sign '+'is converted into a
        space character ' '.
      . The remaining characters are represented by 3-character
        strings which begin with the percent sign,
        "%xy", where xy is the two-digit hexadecimal 
        representation of the lower 8-bits of the character.
   */


   
   tf.setText(theMessage);
   }
 }

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.