Java/JavaScript Communication

Lets assume you have a web page with some graphics on it. These graphics
are named one.jpg, two.jpg and three.jpg. These are configured as IMAGE
BUTTONS and have a VALUE of one.txt, two.txt and three.txt respectively.
We also have 2 directories on the hard disk of the NT server where this web
page resides. These directories are called c:\jb\store and c:\jb\play
There are three files in the store directory called one.txt, two.txt and
three.txt and more.
What I need is a Java routine that ...
When a user clicks on the Image Button the routine should COPY the file
whose name is in the VALUE field of the image button from the store directory to the play directory.
That is all. Just read the VALUE field and copy.
Thanks.... Its really important.


rsunithAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rsunithAuthor Commented:
i'm using swing applets, will liveconnect work with 1.2 applets. if yes, can u send me a sample code? i've done it using 1.1 applets - but then here the security issue comes into the picture. but with 1.2 applets i can atleast change the policy file and grant permissions. any help is appreciated.
thanx in advance
0
OviCommented:
Yes, you can do LiveConnect with Swing. A greather help for you will be if you try to use netscape.javascript.JSObject.

LiveConnect example witch simulate a clipboard (copy and paste to and from HTML, used for importing or exporting text in an Editor applet) :

  public static String copyFromHTML() {
    String copy = "";
    String msg[]=null;
    JSObject win=null;
    try {
          msg = new String[1];
          msg[0] = new String("");
          win = JSObject.getWindow(context.getApplet("Editor"));
    } catch (Exception e1) {
          System.out.println("Exceptie la FROM HTML : "+e1.toString());
    }
    try {
        copy = (win.call("getClip",msg)).toString();
    } catch (Exception e2) {
          System.out.println("Exceptie1 la FROM HTML : "+e2.toString());
    }        
      return copy;
  }
 
  public static void pasteToHTML(String text) {
    String msg[]=null;
    JSObject win=null;
    try {
          msg = new String[1];
          msg[0] = text;
          win = JSObject.getWindow(context.getApplet("Editor"));
    } catch (Exception e1) {
          System.out.println("Exceptie la TO HTML : "+e1.toString());
    }
    try {
        win.call("setClip",msg);
    } catch (Exception e2) {
          System.out.println("Exceptie1 la TO HTML : "+e2.toString());
    }        
  }

////////////////////////////////////////HTML file ////////////////////////////////


<HTML><BODY ><FORM NAME="FSM"><CENTER><APPLET NAME="Editor" archive="swingall.jar" CODE="Editor.class" WIDTH=580 HEIGHT=350 MAYSCRIPT></APPLET><textarea name="txt" rows=6 cols=80></textarea><INPUT TYPE="TEXT" NAME="done" VALUE="no"><INPUT TYPE="TEXT" NAME="CreatedLink" VALUE="Abort"><INPUT TYPE="BUTTON" NAME="Abort" Value="Abort !" ONCLICK="Abort_()"></FORM><p align=left>
Click <A HREF="swingall.jar">here</A> if you want to download swingall.jar.<p align=right><font color="darkbrown">created by Ovi</font></p>
</A>
<SCRIPT LANGUAGE=JavaScript>
function getClip() {
      return document.forms[0].txt.value;
}
function Abort_(){
document.FSM.CreatedLink.value='Abort';
document.FSM.done.value='yes'
}
function setClip(str) {
      document.forms[0].txt.value=str;
      return "true";
}
</SCRIPT>
</BODY>
</HTML>


....And using JSObject, communication with CGI :


import java.awt.*;
import java.net.*;
import java.io.*;
import netscape.javascript.JSObject;

public class GlobalCGIConnection {
    public static String result;
    String url;
   
    public GlobalCGIConnection(String url) {
          result = new String("");
          this.url = url;
          result = makeConnection();
      }
      
      public String makeConnection() {
            String result = "";
            String msg[] = null;
            JSObject mainwin    = null;
            JSObject newwin     = null;
            JSObject document   = null;
            JSObject form0          = null;
            JSObject doneButton = null;

            
            try {
                  msg = new String[3];
                  msg[0] = url;
                  msg[1] = new String("NEWWIN");
                  msg[2] = new String("status,resizable,width=800,height=600");

                  mainwin = JSObject.getWindow(Editor.context.getApplet("Editor"));
                  System.out.println("1111 ");
            }catch(Exception e) {
                  System.out.println("mainwin : ");
                  e.printStackTrace();
            }
            
            try {
                  newwin = (JSObject) mainwin.call("open",msg);
                  System.out.println("2222 ");

            }
            catch(Exception ex) {
                        System.out.println("opener : ");
                  ex.printStackTrace();
            }
                  try{
                        document = (JSObject) mainwin.getMember("document");
                        form0 = (JSObject) document.getMember("FSM");
            }
                  catch(Exception ex) {
                  }


            boolean done=false;
            
            while (!done){
                  try{
                        doneButton =  (JSObject) form0.getMember("done");
                        if ((doneButton.getMember("value")).toString().equals("yes")) {
                              System.out.println("The user pressed DONE");
                              JSObject valueObj = (JSObject) form0.getMember("CreatedLink");
                              result = valueObj.getMember("value").toString();
                              done=true;
                        }
                  }
                  catch(Exception ex) {
                  }
            }

            doneButton.setMember("value","no");
    newwin.call("close",null);

            return result;
      }
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rsunithAuthor Commented:
i used swingall.jar , but i'm getting a classformaterror! where did i go wrong?
0
OviCommented:
Please explain in a greather detail what you are trying to do and post the entire stack trace. I use Swing1.1.1 too in my applet, with loading of swingall.jar and all works perfect.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.