It's not a secret anymore!*
The moose likes GWT and the fly likes GWT in Action: GWT and my actual Javascript code (JSNI) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » GWT
Bookmark "GWT in Action: GWT and my actual Javascript code (JSNI)" Watch "GWT in Action: GWT and my actual Javascript code (JSNI)" New topic
Author

GWT in Action: GWT and my actual Javascript code (JSNI)

Filippo Vitale
Greenhorn

Joined: May 06, 2006
Posts: 12
I�m interested in the JavaScript Native Interface to use directly Javascript code in Java. Looking at the index of the book (http://www.manning.com/hanson/excerpt_index.pdf) I found out that there are probably 10-15 pages covering this topic.

From your experience is it practical to integrate Javascript code within the Java one? Do you think is well covered this topic in the book?

Can I ask you if in your experience using a the java-to-javascript compiler you really stop thinking about the underlying Javascript?


Thank you guys
Filippo


_ _ _____________________________ _ _ <br /> <br /> * SCJP 5.0
Robert Hanson
author
Ranch Hand

Joined: May 11, 2007
Posts: 34
Hi Filippo.

From your experience is it practical to integrate Javascript code within the Java one?


Yes. Check out the GWT Widget Library. It contains wrapper classes for Scriptaculous, JsGraphics, JsCalendar, etc. These are just JSNI wrappers around JS code.

These are really great JS libraries. Using JSNI allows you to take advantage of that and not have to reinvent the wheel.

Do you think is well covered this topic in the book?


Yes. JSNI sounds complicated, but it is pretty simple. In the book we cover the special JSNI syntax, and show you how to use it.

Can I ask you if in your experience using a the java-to-javascript compiler you really stop thinking about the underlying Javascript?


There is a compiler?

It took me about two weeks to get used to it, and now I don't even think about it. I just write my code in Java, compile, and deploy. I don't look at the generated code at all.


Robert Hanson
Co-Author GWT in Action
Adam Tacy
author
Ranch Hand

Joined: Jun 02, 2007
Posts: 37
Hi Filippo,

Yes, it's certainly practical to integrate JavaScript within your GWT code.

Take a look at the Google Search and Google Video Search applications that comes with the book's main example (http://dashboard.manning-sandbox.com and then select the Create Menu). These example use JSNI to communicate from GWT to Google JavaScript libraries.

Whilst it is possible, you do have to think of the reasons why you would.

The main reasons for using JSNI are two-fold:

1) You need to use a JavaScript library that you cannot rewrite in GWT (e.g. you don't have the license; you don't have the desire to,...).

2) You need to do something that is not currently provided by GWT.

I don't think it is sensible to advocate putting large amounts of JavaScript within an application except for those two reasons. To do so, you would:

* not benefit from the static analysis of GWT compiler (so your code would be larger than necessary - GWT compiler very aggressively removes unused Java code),
* not leverage off the browser agnostic approaches within GWT (so code would either be larger with included code for browser sniffing/browser versions, or not work in one/more browsers),
* not be able to take advantage of the typing model of Java (so potential for errors / tougher debugging creeps in)

The principles of JSNI are fairly straight forward - it effectively hijacks JNI syntax for methods and then introduces some syntax to represent Java objects in your JavaScript code.

The chapter in the book talks through the key components of JSNI covering writing JavaScript in your code; talking to JavaScript libraries; getting JavaScript libraries to talk back to GWT applications; passing objects over the boundaries; and implementing an API to your application through JSNI. This is then backed up by complete implementations in the example application.

Your final question is also interesting! There is a danger when writing a GWT application to forget the actual client side Java code will be implementated in JavaScript. If you look on the GWT forum there are often questions about accessing a user's hard drive; directly accessing a database; client side logging (e.g. Log4J file output) - stuff that is not too unusual to think of in a Java program, but not necessarily possible in JavaScript. That said, as long as you remember your code will eventually become JavaScript, it is a comforting development experience.

Hope the answer to your questions are in there - naybe I should start writing shorter answers!!

//Adam
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: GWT in Action: GWT and my actual Javascript code (JSNI)