Java Forum / General / August 2005
Java inside Javascript
AbhyS - 24 Jul 2005 11:00 GMT Hi Techs
Can I invoke a java function which I have created in my own package, inside JavaScript?
I am using the JS inside JSP.
THANKS in ADVANCE
Andrew Thompson - 24 Jul 2005 11:55 GMT > Can I invoke a java function which I have created in my own package, > inside JavaScript? Yes. And no.
> I am using the JS inside JSP. JS can interact with a client-side *applet*, which can then transmit information back to the server, to be used by the classes inside WEB-INF and your JSP's.
JS can not interact with the *Server* *side* JSP classes unless the end user makes another 'page send'.
 Signature Andrew Thompson physci.org 1point1c.org javasaver.com lensescapes.com athompson.info Now Interactive! Joystick Controls Fry's Left Ear.
Raymond DeCampo - 24 Jul 2005 17:39 GMT >>Can I invoke a java function which I have created in my own package, >>inside JavaScript? [quoted text clipped - 9 lines] > JS can not interact with the *Server* *side* JSP classes unless > the end user makes another 'page send'. Not to confuse things, (the OP should probably only focus on Andrew's answer), but isn't it (at least theoretically) possible to use JSP with JavaScript as the script language (as opposed to Java)? Has anybody ever done this? Can you share your experience?
Thanks, Ray
 Signature XML is the programmer's duct tape.
Andrew Thompson - 24 Jul 2005 18:02 GMT > ..isn't it (at least theoretically) possible to use JSP with > JavaScript as the script language (as opposed to Java)? Not as far as I have heard.
The closest that came to that was that a 'MicrosoftJavaScript'* could be run directly off a Microsoft server (of some description). Since my interest was close to zero, I did not investigate further.
* With all the 'compliance to standards' that implies.
 Signature Andrew Thompson physci.org 1point1c.org javasaver.com lensescapes.com athompson.info Now Interactive! Joystick Controls Fry's Left Ear.
Raymond DeCampo - 25 Jul 2005 01:38 GMT >>..isn't it (at least theoretically) possible to use JSP with >>JavaScript as the script language (as opposed to Java)? [quoted text clipped - 7 lines] > > * With all the 'compliance to standards' that implies. A quick check of the specification (JSP 2.0) and I see that Java is the only required scripting language. There is a reference to JavaScript in the section on objects, but it is not clear what the context is.
Ray
 Signature XML is the programmer's duct tape.
Hal Rosser - 25 Jul 2005 02:14 GMT > >>Can I invoke a java function which I have created in my own package, > >>inside JavaScript? [quoted text clipped - 17 lines] > Thanks, > Ray I think the 'Server-Side' Javascript you're referring to is "Active Server Pages" , which can use VBScript or Javascript.(JScript). The Op should just think about <where> and when scripts will be executed. The java on a JSP runs on the server. The JSP can have Javascript (client-side) mixed in just like a normal html page. The client-side JavaScript will be executed on the user's browser. So it would be possible to include the value of a Java expression mixed into the javascript code, but not vice-versa.
Andrew Thompson - 25 Jul 2005 06:53 GMT > I think the 'Server-Side' Javascript you're referring to is "Active Server > Pages" , which can use VBScript or Javascript.(JScript). Ahh. yes. That sounds like actual information, as opposed to me 'vaguely waving my hands about'.
Yes, the use of the term JScript even had me wonderring just how far MS had changed the standard JS to suit ASP, and why a developer for ASP would not simply 'blow the expense' and use VB. (As far as I understand, VB should be able to do anything JScript can do.)
Note to the OP. We are having a merry little side discussion here.. Thus is little to do with your oringinal question. You need to focus on the question of 'where does it execute'.
 Signature Andrew Thompson physci.org 1point1c.org javasaver.com lensescapes.com athompson.info Not Affiliated With Futurama Brass Knuckle Co.
Raymond DeCampo - 25 Jul 2005 13:34 GMT >>I think the 'Server-Side' Javascript you're referring to is "Active Server >>Pages" , which can use VBScript or Javascript.(JScript). [quoted text clipped - 7 lines] > expense' and use VB. (As far as I understand, VB should > be able to do anything JScript can do.) By the same token, JScript can do anything VBScript can do and is the same language used in Internet Explorer to implement JavaScript (by which I mean that JavaScript in Internet Explorer is interpreted by the JScript scripting engine). So there may be a familiarity factor at play, especially for the front-end developer moving into the back end.
> Note to the OP. We are having a merry little side discussion here.. > Thus is little to do with your oringinal question. You need > to focus on the question of 'where does it execute'. Absolutely.
Ray
 Signature XML is the programmer's duct tape.
Viv - 25 Jul 2005 13:55 GMT Hi Abhy, You can always invoke the java function in a javascript, I tried the same and solved many complex things using java function in a javascript. One example is
var v18to25 = new Array(<%=((SAOutputBean)OutBean).getEliminetedProducts(Literals.QUESTION_ID,Literals.ANSWER_18TO25HP) %>);
AbhyS - 27 Jul 2005 17:38 GMT > Hi Abhy, > You can always invoke the java function in a javascript, I tried the [quoted text clipped - 4 lines] > Array(<%=((SAOutputBean)OutBean).getEliminetedProducts(Literals.QUESTION_ID,Literals.ANSWER_18TO25HP) > %>); It is done at the time of loading the page. I have to call java functions dynamically inside the page. We use the built in Java functions like indexOf(), I need to use my function also like that.
Andrew Thompson - 27 Jul 2005 17:56 GMT > ..I have to call java functions dynamically inside the page. Applet <-> JS = 'LiveScript'
<http://www.rgagnon.com/howto.html> Look under 'Javascript interaction'
HTH
 Signature Andrew Thompson physci.org 1point1c.org javasaver.com lensescapes.com athompson.info Featuring Gratuitous Alien Nudity
Raymond DeCampo - 25 Jul 2005 13:30 GMT >>>>Can I invoke a java function which I have created in my own package, >>>>inside JavaScript? [quoted text clipped - 27 lines] > So it would be possible to include the value of a Java expression mixed into > the javascript code, but not vice-versa. No, I was thinking of JSP pages where one uses the language attribute of the page directive to specify a language other than Java. In recent specifications however, "java" is the only allowed value for the language attribute. So either JSP has changed to no longer allow alternate languages or the whole thing was a product of my imagination.
I haven't been able to locate early versions of the servlet specification (the servlet and JSP specifications used to be one) to verify or disprove this claim.
There is some evidence that such a change was made. For example, the existence of the language attribute on the page directive. Why have an attribute with only one allowable value? Also, there are references to JavaScript as the scripting language in section JSP.1.8.1 of the 2.0 specification:
An element type indicates the name and type of such variables although details on the name of the variable may depend on the Scripting Language. The scripting language may also affect how different features of the object are exposed. For example, in the JavaBeans specification, properties are exposed via getter and setter methods, while these properties are available directly as variables in the JavaScriptâ„¢ programming language.
Then there is the fact that much of the specification is written without assuming Java is the scripting language when it is the only possible one.
Ray
 Signature XML is the programmer's duct tape.
Monique Y. Mudama - 28 Jul 2005 21:26 GMT > Then there is the fact that much of the specification is written > without assuming Java is the scripting language when it is the only > possible one. Couldn't this simply be a matter of paving the way for a possible future capability of supporting other syntaxes, rather than proof that it once supported JavaScript?
(I have no idea either way ..)
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
Raymond DeCampo - 28 Jul 2005 22:36 GMT >>Then there is the fact that much of the specification is written >>without assuming Java is the scripting language when it is the only [quoted text clipped - 5 lines] > > (I have no idea either way ..) It could be, but I find that that kind of foresightedness is rare. And that doesn't explain the occurrence of "JavaScript" in the specification that I quoted.
I admit that I hardly have conclusive evidence. I wish I could find the early servlet specifications somewhere, then we could know for sure. (Not that it really matters.)
Ray
 Signature XML is the programmer's duct tape.
Raymond DeCampo - 08 Aug 2005 19:46 GMT >>> Then there is the fact that much of the specification is written >>> without assuming Java is the scripting language when it is the only [quoted text clipped - 15 lines] > > Ray The J2EE 1.4 tutorial has some references to the scripting language being something other than Java, although no examples are mentioned:
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPAdvanced3.html#wp74291
This seems to be in conflict with my reading of the specification, which indicated that Java is the only option.
Ray
 Signature XML is the programmer's duct tape.
Andrew Thompson - 08 Aug 2005 20:00 GMT >>>> Then there is the fact that much of the specification is written >>>> without assuming Java is the scripting language when it is the only [quoted text clipped - 3 lines] >>> future capability of supporting other syntaxes, rather than proof that >>> it once supported JavaScript? ...
>> It could be, but I find that that kind of foresightedness is rare. And >> that doesn't explain the occurrence of "JavaScript" in the specification >> that I quoted.
> The J2EE 1.4 tutorial has some references to the scripting language > being something other than Java, although no examples are mentioned: > > http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPAdvanced3.html#wp74291 Wow! You are just not gonna' give this up ( I guess ;).
I heard mention of the RhinoScript[2] engine being included in Mustang[1], couldn't figure would they would do such a thing at the time..
[ Spooky music ] ..Maybe you're right.
[1] <http://java.sun.com/developer/technicalArticles/J2SE/Desktop/Mustang_build39.htm l#Language> [2] <http://jcp.org/en/jsr/detail?id=223>
 Signature Andrew Thompson physci.org 1point1c.org javasaver.com lensescapes.com athompson.info Hey TiVo! Suggest *This*!
Raymond DeCampo - 08 Aug 2005 20:46 GMT >>>>>Then there is the fact that much of the specification is written >>>>>without assuming Java is the scripting language when it is the only [quoted text clipped - 16 lines] > > Wow! You are just not gonna' give this up ( I guess ;). I just happened to come across that page in my research efforts today. I couldn't resist posting it. :-)
Ray
 Signature XML is the programmer's duct tape.
Chris Smith - 27 Jul 2005 18:06 GMT > > I am using the JS inside JSP. > [quoted text clipped - 4 lines] > JS can not interact with the *Server* *side* JSP classes unless > the end user makes another 'page send'. This isn't precisely true. What is true is that JavaScript cannot directly call code that's running on the server, and that there will always be some kind of a network connection involved if you need to run code on the server.
It is, however, possible to use the non-standard but widely implemented JavaScript XmlHttpRequest class to interact with the server via a network connect without actually reloading any pages. You'll have to write your own code to marshall the parameters to your procedure call into an HTTP request, and parse it back out again on the server. If you happen to be using JSF (JavaServer Faces), then I've written some code that can handle this using JSON, by mapping calls to backing bean methods and properties. The code is there, but hardly well-documented. Let me know you're interested, and I'll package the code on my web site and post a link to it.
If you have to roll your own here, then be aware that this is actually a somewhat difficult problem, since the two languages (Java and JavaScript) have drastically different type systems and both can define arbitrary objects that are difficult to map to anything on the other side. You'd be better off simplifying many of the issues by sticking to only String parameters and return values, etc... at least initially.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Hal Rosser - 25 Jul 2005 05:35 GMT > Hi Techs > > Can I invoke a java function which I have created in my own package, > inside JavaScript? > > I am using the JS inside JSP. Java executes on the server and JavaScript executes in the user's browser. So the answer is no. Java can write the html and the javascript - and send it all to the browser, where the Javascript can then be executed. Once it gets to the browser, Tomcat has already done its job.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|