Solved

Calling a lot of JavaScript from Java

Posted on 1998-07-28
8
205 Views
Last Modified: 2010-04-16
I just created a web site with JavaScript and HTML.  However, I'd like to embed all of the JavaScript, including functions, variable definitions, arrays, and image caching process  within a Java applet or Java class or whatever Java will work(I'm not too familiar with Java) so I can compile my JavaScript and prevent it from being viewed from the Net.  Currently, I'm trying something like this:

import netscape.javascript.JSObject
public void init()
{
     JSObject win = JSObject.getwindow(this);
     win.eval('<--All of the JavaScript in here-->');}
This doesn't seem to be working?!?  Please give the Java code that will call an entire script (if it can be done).
I'm using netscape 4 and JavaScript1.2
0
Comment
Question by:jtman
  • 4
  • 4
8 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 100 total points
ID: 1228704
What you posted takes you in the right direction, however there is a little bit more to an applet. Try this:

import java.applet.*;
import netscape.javascript.JSObject

public class TestApplet extends Applet
{
    public void init()
    {
         JSObject win = JSObject.getwindow(this);
         win.eval('<--All of the JavaScript in here-->');
    }
}

Now you need an HTML file with an APPLET tag which references your applet (you probably know all that, I am just making sure you are on the right track). Try calling some JS code first.

I am not sure it is a good idea to put all the JS code in one eval() call. You can either break it into pieces, or even better, define JS functions and then call them using

JSObject.call(methodName, argArray)

Chapter 5 (LiveConnect) of JS 1.2 documentation (it is available online) addresses those issues.


0
 

Author Comment

by:jtman
ID: 1228705
I created a Java Source file with the code you suggested, but when I attempted to "Build", I received three errors.  
1) Undefined package 'lang'
2)Undefined package 'applet'
3)Undefined name 'netscape.javascript.JSObject'
any suggestions.
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1228706
You seem to be using some development tool. What is it? Could you post the entire source file. Can you use JDK to compile the code?
0
 

Author Comment

by:jtman
ID: 1228707
I'm using a trial version of Visual J++.  I have purchased the full version, but it hasn't arrived yet.  I tried to compile the code, but the developer studio just reports the three errors.  Anyway, the entire source file is this:

import java.applet.*;
import netscape.javascript.JSObject;

public class TestApplet extends Applet
{
      public void init()
      {
            JSObject win = JSObject.getwindow(this);
            win.eval("var currentlung; var submitmessage; var lungcount;");

win.eval("lapicalarray = new Array (7); rapicalarray = new Array (7); lposteriorarray = new Array (7); rposteriorarray = new Array (7); lanteriorarray = new Array (7); ranteriorarray = new Array (7); llateralarray = new Array (7); rlateralarray = new Array (7); lmedialarray = new Array (7); rmedialarray = new Array (7); lsuperiorarray = new Array (7); rsuperiorarray = new Array (7); lanteriorbasalarray = new Array (7); ranteriorbasalarray = new Array (7); llateralbasalarray = new Array (7); rlateralbasalarray = new Array (7); lposteriorbasalarray = new Array (7); rposteriorbasalarray = new Array (7); lungarray = new Array (lapicalarray, rapicalarray, lposteriorarray, rposteriorarray, lanteriorarray, ranteriorarray, llateralarray, rlateralarray, lmedialarray, rmedialarray, lsuperiorarray, rsuperiorarray, lanteriorbasalarray, ranteriorbasalarray, llateralbasalarray, rlateralbasalarray, lposteriorbasalarray, rposteriorbasalarray);");

win.eval("cachimages = new Array (8); cachimages[0] = new Image(); cachimages[1] = new Image(); cachimages[2] = new Image(); cachimages[3] = new Image(); cachimages[4] = new Image(); cachimages[5] = new Image(); cachimages[6] = new Image(); cachimages[7] = new Image(); cachimages[0].src = 'Pictrpo.gif'; cachimages[1].src = 'Pictlpo.gif'; cachimages[2].src = 'Pictlao.gif'; cachimages[3].src = 'Pictrao.gif'; cachimages[4].src = 'Pictanterior.gif'; cachimages[5].src = 'Pictposterior.gif'; cachimages[6].src = 'Pictrlateral.gif'; cachimages[7].src = 'Pictllateral.gif';");

win.eval("function normalize_all_lungs (){document.patientinfo.norm_or_abnorm[0].checked = true; document.patientinfo.defectsize[0].checked = true; document.patientinfo.degree[0].checked = true; document.patientinfo.segmental[0].checked = true; document.patientinfo.matching[0].checked = true; document.patientinfo.cxropacity[0].checked = true; for(var x=0; x<=17; x++){ for(var a=0; a<=1; a++) if(document.patientinfo.norm_or_abnorm[a].checked == true){ break; } lungarray[x][0] = document.patientinfo.norm_or_abnorm[a].value; for(var b=0; b<=3; b++) if(document.patientinfo.defectsize[b].checked == true){ break; } lungarray[x][1] = document.patientinfo.defectsize[b].value; for(var c=0; c<=3; c++) if(document.patientinfo.degree[c].checked == true){ break; } lungarray[x][2] = document.patientinfo.degree[c].value; for(var d=0; d<=2; d++) if(document.patientinfo.segmental[d].checked == true){ break; } lungarray[x][3] = document.patientinfo.segmental[d].value; for(var e=0; e<=2; e++) if(document.patientinfo.matching[e].checked == true){ break; } lungarray[x][4] = document.patientinfo.matching[e].value; for(var f=0; f<=3; f++) if(document.patientinfo.cxropacity[f].checked == true){ break; } lungarray[x][5] = document.patientinfo.cxropacity[f].value; hidden_field();}}");

win.eval("function arraysetter (){for(var x=0; x<= 17; x++) if(document.patientinfo.lung[x].checked == true){ break; }      for (var j=0; j<= 3; j++)if(document.patientinfo.defectsize[j].checked == true){ break; }lungarray[x][1] = document.patientinfo.defectsize[j].value; for (var k=0; k<= 3; k++) if(document.patientinfo.degree[k].checked == true){ break }lungarray[x][2] = document.patientinfo.degree[k].value; for (var l=0; l<= 2; l++) if(document.patientinfo.segmental[l].checked == true){ break } lungarray[x][3] = document.patientinfo.segmental[l].value; for(var m=0; m<= 2; m++) if(document.patientinfo.matching[m].checked == true){ break } lungarray[x][4] = document.patientinfo.matching[m].value; for(var n=0; n<= 3; n++) if(document.patientinfo.cxropacity[n].checked == true){ break } lungarray[x][5] = document.patientinfo.cxropacity[n].value; if(j==0 && k==0 && l==0 && m==0 && n==0){lungarray[x][0] = document.patientinfo.norm_or_abnorm[0].value; document.patientinfo.norm_or_abnorm[0].checked = true;} if(j!=0 || k!=0 || l!=0 || m!=0 || n!=0){lungarray[x][0] = document.patientinfo.norm_or_abnorm[1].value; document.patientinfo.norm_or_abnorm[1].checked = true;} setn(); hidden_field();}");

win.eval("function hidden_field (){document.vqprodata.apicall.value = lungarray[0].join(''); document.vqprodata.apicalr.value = lungarray[1].join(''); document.vqprodata.posteriorl.value = lungarray[2].join(''); document.vqprodata.posteriorr.value = lungarray[3].join(''); document.vqprodata.anteriorl.value = lungarray[4].join(''); document.vqprodata.anteriorr.value = lungarray[5].join(''); document.vqprodata.laterall.value = lungarray[6].join(''); document.vqprodata.lateralr.value = lungarray[7].join(''); document.vqprodata.mediall.value = lungarray[8].join(''); document.vqprodata.medialr.value = lungarray[9].join(''); document.vqprodata.superiorl.value = lungarray[10].join(''); document.vqprodata.superiorr.value = lungarray[11].join(''); document.vqprodata.antbasall.value = lungarray[12].join(''); document.vqprodata.antbasalr.value = lungarray[13].join(''); document.vqprodata.latbasall.value = lungarray[14].join(''); document.vqprodata.latbasalr.value = lungarray[15].join(''); document.vqprodata.posbasall.value = lungarray[16].join(''); document.vqprodata.posbasalr.value = lungarray[17].join('');}");

win.eval("function setcurrentlung (){for(var x=0; x<=17; x++) if(document.patientinfo.lung[x].checked == true){ break; } currentlung = x;}");

win.eval("function normal_click_reset (){if(document.patientinfo.norm_or_abnorm[0].checked == true){document.patientinfo.defectsize[0].checked = true; document.patientinfo.degree[0].checked = true; document.patientinfo.segmental[0].checked = true; document.patientinfo.matching[0].checked = true; document.patientinfo.cxropacity[0].checked = true; arraysetter();}}");

win.eval("function validateabnormal (){if((lungarray[currentlung][0] == '01') && (lungarray[currentlung][1] == '0' || lungarray[currentlung][2] == '0' || lungarray[currentlung][3] == '0' || lungarray[currentlung][4] == '0')){window.alert('You have indicated an abnormal lung section but have failed to enter all data completely.  Please do so now, making sure that the Defect Size, Degree, Segmental, and Matching data have been entered with non-normal values.'); document.patientinfo.lung[currentlung].checked = true;} if((lungarray[currentlung][1] != '0' && lungarray[currentlung][2] != '0' && lungarray[currentlung][3] != '0' && lungarray[currentlung][4] != '0') || lungarray[currentlung][0] == '10'){setfield(); retriever(); setcurrentlung();}}");
 
win.eval("function setfield (){for(var s=0; s <= 17; s++) if(document.patientinfo.lung[s].checked == true){ break; } document.patientinfo.lungsect.value = document.patientinfo.lung[s].value;}");

win.eval("function changepictl (){for(var l=0; l <=7; l++) if(document.patientinfo.viewl[l].checked == true){ break; } document.patientinfo.leftpict.src = document.patientinfo.viewl[l].value + '.gif';}");

win.eval("function changepictr (){for(var r=0; r <=7; r++) if(document.patientinfo.viewr[r].checked == true){ break; } document.patientinfo.rightpict.src = document.patientinfo.viewr[r].value + '.gif';}");

win.eval("function retriever (){for(var x=0; x<=17; x++) if(document.patientinfo.lung[x].checked == true){ break; } for(var q=0; q<=1; q++) if(document.patientinfo.norm_or_abnorm[q].value == lungarray[x][0]){ break; } document.patientinfo.norm_or_abnorm[q].checked = true; for(var y=0; y<= 3; y++) if(document.patientinfo.defectsize[y].value == lungarray[x][1]){ break; } document.patientinfo.defectsize[y].checked = true; for(var z=0; z<= 3; z++) if(document.patientinfo.degree[z].value == lungarray[x][2]){ break; } document.patientinfo.degree[z].checked = true; for(var a=0; a<=2; a++) if(document.patientinfo.segmental[a].value == lungarray[x][3]){ break; } document.patientinfo.segmental[a].checked = true; for(var b=0; b<=2; b++) if(document.patientinfo.matching[b].value == lungarray[x][4]){ break; } document.patientinfo.matching[b].checked = true; for(var c=0; c<=3; c++) if(document.patientinfo.cxropacity[c].value == lungarray[x][5]){ break; } document.patientinfo.cxropacity[c].checked = true;}");


win.eval("function setn (){if(lungarray[0][0] == '01') document.patientinfo.lapical.value = 'A'; else document.patientinfo.lapical.value = 'N'; if(lungarray[1][0] == '01') document.patientinfo.rapical.value = 'A'; else document.patientinfo.rapical.value = 'N'; if(lungarray[2][0] == '01') document.patientinfo.lposterior.value = 'A'; else document.patientinfo.lposterior.value = 'N'; if(lungarray[3][0] == '01') document.patientinfo.rposterior.value = 'A'; else document.patientinfo.rposterior.value = 'N'; if(lungarray[4][0] == '01') document.patientinfo.lanterior.value = 'A'; else document.patientinfo.lanterior.value = 'N'; if(lungarray[5][0] == '01') document.patientinfo.ranterior.value = 'A'; else document.patientinfo.ranterior.value = 'N'; if(lungarray[6][0] == '01') document.patientinfo.llateral.value = 'A'; else document.patientinfo.llateral.value = 'N'; if(lungarray[7][0] == '01') document.patientinfo.rlateral.value = 'A'; else document.patientinfo.rlateral.value = 'N'; if(lungarray[8][0] == '01') document.patientinfo.lmedial.value = 'A'; else document.patientinfo.lmedial.value = 'N'; if(lungarray[9][0] == '01') document.patientinfo.rmedial.value = 'A'; else document.patientinfo.rmedial.value = 'N';");

win.eval("if(lungarray[10][0] == '01') document.patientinfo.lsuperior.value = 'A'; else document.patientinfo.lsuperior.value = 'N'; if(lungarray[11][0] == '01') document.patientinfo.rsuperior.value = 'A'; else document.patientinfo.rsuperior.value = 'N'; if(lungarray[12][0] == '01') document.patientinfo.lanteriorbasal.value = 'A'; else document.patientinfo.lanteriorbasal.value = 'N'; if(lungarray[13][0] == '01') document.patientinfo.ranteriorbasal.value = 'A'; else document.patientinfo.ranteriorbasal.value = 'N'; if(lungarray[14][0] == '01') document.patientinfo.llateralbasal.value = 'A'; else document.patientinfo.llateralbasal.value = 'N'; if(lungarray[15][0] == '01') document.patientinfo.rlateralbasal.value = 'A'; else document.patientinfo.rlateralbasal.value = 'N'; if(lungarray[16][0] == '01') document.patientinfo.lposteriorbasal.value = 'A'; else document.patientinfo.lposteriorbasal.value = 'N'; if (lungarray[17][0] == '01') document.patientinfo.rposteriorbasal.value = 'A'; else document.patientinfo.rposteriorbasal.value = 'N';}");

win.eval("function summary_final (){if((lungarray[currentlung][0] == '01') && (lungarray[currentlung][1] == '0' || lungarray[currentlung][2] == '0' || lungarray[currentlung][3] == '0' || lungarray[currentlung][4] == '0')){window.alert('You have indicated an abnormal lung section but have failed to enter all data completely.  Please do so now, making sure that the Defect Size, Degree, Segmental, and Matching data have been entered with non-normal values.'); document.patientinfo.lung[currentlung].checked = true;} if((lungarray[currentlung][1] != '0' && lungarray[currentlung][2] != '0' && lungarray[currentlung][3] != '0' && lungarray[currentlung][4] != '0') || lungarray[currentlung][0] == '10'){submitmessage = '<FONT SIZE=5><B>Summary of Entered Data</FONT></B>'; submitmessage += button_maker(); submitmessage += 'NAME: '.bold() + document.patientid.patientname.value +'&nbsp; &nbsp;' + 'ID: '.bold() + document.patientid.patientidnumber.value + '<HR>' for(lungcount=0; lungcount<=17; lungcount++) submitmessage += summary_data(); var summarywindow = window.open('','summarywindow', 'width=600,height=520,screenX=250,screenY=130'); summarywindow.mainwindow = self; summarywindow.document.write(submitmessage); summarywindow.document.close();}}");

win.eval("function summary_data (){var templung; templung = document.patientinfo.lung[lungcount].value; if(lungarray[lungcount][0] == '10') templung += ':  ' +  ' Normal'.fontcolor('green') +  '<BR>'; if(lungarray[lungcount][0] == '01'){if(lungarray[lungcount][2] =='1') templung += ':  ' + ' Slight Defect'.fontcolor('red'); if(lungarray[lungcount][2] == '2') templung += ':  ' + ' Moderate Defect'.fontcolor('red'); if(lungarray[lungcount][2] == '3') templung += ':  ' + ' Severe Defect'.fontcolor('red'); if(lungarray[lungcount][1] == '1') templung += ' (< 25%, '.fontcolor('red'); if(lungarray[lungcount][1] == '2') templung += ' (25 - 75%, '.fontcolor('red'); if(lungarray[lungcount][1] == '3') templung += ' (76 - 100%, '.fontcolor('red'); if(lungarray[lungcount][3] == '1') templung += ' Segmental, '.fontcolor('red'); if(lungarray[lungcount][3] == '2') templung += ' Non Segmental, '.fontcolor('red'); if(lungarray[lungcount][4] == '1') templung += ' Matched )'.fontcolor('red'); if(lungarray[lungcount][4] == '2') templung += ' Mismatched )'.fontcolor('red'); if(lungarray[lungcount][5] == '1') templung += '    CXR: None <BR>'.fontcolor('red'); if(lungarray[lungcount][5] == '2') templung += '    CXR < Q <BR>'.fontcolor('red'); if(lungarray[lungcount][5] == '3') templung += '    CXR = Q <BR>'.fontcolor('red'); if (lungarray[lungcount][5] == '4') templung += '    CXR > Q <BR>'.fontcolor('red');} return templung;}");

win.eval("function button_maker (){return '<TITLE>Summary of Entered Data</TITLE><BODY onblur=");
win.eval("'window.focus();'");
win.eval("onmove='moveTo(250,130);'>");
win.eval("</BODY><FORM NAME='nodaform'><INPUT TYPE='button' NAME='back' VALUE='Modify Entry' onClick='self.close();'> <INPUT TYPE='button' NAME='interp' VALUE='Submit for Interpretation' onClick='mainwindow.document.vqprodata.submit(); self.close();'></FORM><P>';}");
}
}

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:msmolyak
ID: 1228708
I think in J++ you can create an applet using some sort of Wizard and the fill in the content. I would try a much simpler version where there is only one eval() call to do something like alert() in JS.

I am not sure why it complains about lang (it should see the java.lang package). Regarding netscape.javascript package you should tell J++ where this package is. You do it by setting (or adding to ) a classpath. The netscape's package is in C:\Program Files\Netscape\Communicator\Program\Java\Classes\java40.jar (on my machine that is). Add this complete file name (whatever it is on your machine) to the classpath (there should be an option for that in J++).
0
 

Author Comment

by:jtman
ID: 1228709
In my JS resource guide (JavaScript, the Definitive Guide), it mentions to set CLASSPATH also.  I located my java40.jar file at the same location as yours, but I can't figure out where to place the set CLASSPATH command.  Should it be placed within the Java Class or at the top of the entire source?
0
 

Author Comment

by:jtman
ID: 1228710
In my JS resource guide (JavaScript, the Definitive Guide), it mentions to set CLASSPATH also.  I located my java40.jar file at the same location as yours, but I can't figure out where to place the set CLASSPATH command.  Should it be placed within the Java Class or at the top of the entire source?
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1228711
If you are using JDK and compiling from the command line you should set an environment variable CLASSPATH. In J++ however there should be a project option which allows you to set classpath. Check the J++ help.

Also you can go to the command line and try using jvc command (I think this is the right name). Check the syntax. Usually you can specfy CLASSPATH right on the command line (if you do not want to create an env. variable). Classpath has nothing to do with the source code.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now