J2J - Java to JavaScript integration ver. 1.5

What is this for ?

    It is a development tool lets you integrate Java classes and JavaScript within your HTML pages. The main idea behind this product is how to call methods of Java classes right from JavaScript functions.
    Let us start from the some example.

    1. Suppose we have got some form on our HTML page and we need to initialize fields from the database. What can we do? The standard solution is to use some CGI script (or Java servlet). But in this case we need to render again the whole HTML file on the server. J2J lets you use results of any calculation from the server-side Java directly within your JavaScript.

<html>
Our form:<br>
<form>
<input type="text" name="textField"><br>
<input type="button" value="Click" onClick="updateField();">
</form>
<script>
function updateField()
{
    document.forms[0].textField.value=java.dbConnection.getData('some_SQL_expr');
}
</script>
</html>

So when user clicks on the button we can call method getData from the class dbConnection and use results in our JavaScript application. And now only textField value will be updated, we do not need to refresh the whole page in the browser.
    And it is not JSP. It is still a plain HTML and you do not need compile your page into Java servlet.

2. DOM.

    Suppose you are going to use browser's DOM and need to update some part of your page.

<html>
<p> current paragraph text </p>

<script>
function changeParagraph()
{
  pp=document.all.tags("p");
  pp(0).innerText=java.myClass.myFunc('give me new text, please');
}
</script>
</html>

So you can replace text within the paragraph with the result of some Java call. By the same manner for example you can update only one row in the huge html-table:

<script language="JavaScript">
function changeCol()
{
document.all.quote.rows[0].cells[1].innerText=java.Stock.getQuote('ACME');
}
</script>

<table id="quote" border=1>
<tr><td>ACME</td><td>123</td></tr>
<tr><td>Another company</td><td>34</td></tr>
</table>

    That is a main idea. Directly mark within your JavaScript some calls for Java objects and keep the rest of your page unchanged. All the necessary stuff for the integration will be generated on the fly by J2J.

How does it work ?

    J2J is a Java servlet. All your HTML pages should be passed through this servlet. J2J will generate on the fly hidden frame with some Java applet. This bridge applet will be deployed for Java server-side communications. Applet size is less than 2 Kb. Applet uses HTTP connection with the same J2J servlet for calling Java objects. J2J replaces all Java calls (they are detected by the prefix 'java.') with the appropriate JavaScript construction and keep the rest of the compiled page unchanged.
    So, for any Java call within your JavaScript code use the following construction:

    java.object_name.function_name(list_of_parameters)

   Where:
    java. - is a standard prefix
    object_name - is a name of variable keeping some java class instance (see below how to create/define such variables). It can be class name for example.
    function_name - is a name of the function from java class
    list_of_parameters - is a list of parameters for this call (can be empty). See below how to pass parameters to Java. Each parameter is obviously some JavaScript expression.

    You can use also precompiled pages - in other words compile your HTML pages before any usage.

How to use this component?

You need two elements for your server: J2J servlet and bridge applet. See bottom of this page for the downloading.

All your html pages where you need to use this integration tool should be preprocessed by J2J servlet. You can achieve at least that by two ways:

a) just pass your page as a parameter for J2J servlet

So instead of hyperlink like:

<a href="mypage.html">My page</a>

you can use:

<a href="http://myhost/servlet/J2J?mypage.html" target="_top">My page</a>  etc.

J2J servlet will generate on the fly all Java-to-JavaScript calls and output the page (pages).

b) define some kind of mapping. For example you can order the invocation for this servlet invocation for the each file with the extension .j2j

Servlet parameters

You need to set some global parameters for J2J servlet. In your web.xml file you have to describe:
1) path to J2J servlet. Parameter name is servlet
It is some like http://your_host:port/servlet/J2J

2) root directory for your HTML pages. Parameter name is root. By default it is an empty string.
It is some like /home/webpages  (or c:\webpages). This value will be added to any file name you are using as a parameter for J2J. E.g. if your root directory is /home/webpages and you are using http://host/servlet/J2J?myfile.htm the real file name will be /home/webpages/myfile.htm

3) full path for the file where you define your Java objects. Parameter name is objects.
Just a name of the file located somewhere on your server.

4) codebase for java applet. Parameter name is codebase.
It is some like http://your_host and define location for applet's class.

E.g. (web.xml file):

    <servlet>
    <servlet-name>J2J</servlet-name>
    <servlet-class>com.jsos.j2j.J2J</servlet-class>
    <init-param>
    <param-name>servlet</param-name>
    <param-value>http://your_host/servlet/J2J</param-value>
    </init-param>
    <init-param>
    <param-name>root</param-name>
    <param-value>path_to_the_root_directory</param-value>
    </init-param>
    <init-param>
    <param-name>objects</param-name>
    <param-value>path_to_your_objects_file</param-value>
    </init-param>
    <init-param>
    <param-name>codebaset</param-name>
    <param-value>http://your_host</param-value>
    </init-param>
    </servlet>

 

Java objects

1. Java objects (class instances) serviced by J2J servlet will be created during the servlet loading (initialization). You must define these objects in the some text file and set path to this file in the parameter objects.

It is a text file, each line describes one element (empty lines, lines starts with # or // are ignored). Element description has got the following form:

object_name=new class_name(list_of_parameters);

In other words it is similar to java operator new when you call class constructor. Object_name is some identificator you can use later in your JavaScript. E.g.:

// database pool
A=new dbPool(5);

// hashtable
B=new java.util.Hashtable();

# just some object
c=new myPackage.myClass(true);

and later in your JavaScript you can use some like A.connect() where connect() is a function from the class dbPool or B.put('key',value)

Note:

1) your classes should be mentioned in the CLASSPATH
2) you can use class name as an object name (if there is no name conflict because objects name should be unique): dbPool=new dbPool();

2. J2J supports a some set of predefined objects. Through this standard library you can for example create/delete object instances in the runtime, check object existence etc.
 

Standard library (predefined objects)

Object name is j2j. So server-side instance with the name of j2j will be created automatically. This class has got the following functions:

public boolean create(String object_name,String class_name,list_of_constructors_parameters);
public void delete(String object_name);
public boolean exists(String object_name);
public String id();
public int random(int n1, int n2);

public String rewriteURL(document,String id_name);
public String rewriteURL(document,String id_name,String id_value);

create: lets you create some object instance in the runtime.
Parameters:
    - new object name
    - class name
    - list of parameters (can be empty, in case you are using default constructor).
Returns:
    - true or false depends on creation results

E.g.:
<script language="JavaScript">
    <!-- create myHash as a new Hashtable -->
    java.j2j.create("myHash","java.util.Hashtable");

    <!-- use myHash -->
    java.myHash.put('key1','shop1');
</script>

delete: lets you delete any object instance
Parameter:
    - object name

E.g.:
<script language="JavaScript">
    <!-- delete  myHash -->
   java.j2j.delete("myHash");
</script>

exists: check object instance presence
Parameter:
    - object name
Returns:
    true or false

E.g.:
<script language="JavaScript">
    if  (java.j2j.exists("myHash")=='false')
   {
        <!-- create myHash as a new Hashtable -->
        java.j2j.create(myHash,"java.util.Hashtable");

        <!-- use myHash -->
        java.myHash.put('key1','shop1');
    }
</script>

id: returns unique identificator.You can use this for example for session's support.
random: returns random value from the given area

E.g.:
<script language="JavaScript">
    var sessionId=java.j2j.id();

    <!-- random value from 1 to 10 -->
    var randValue=java.j2j.random(1,10);
</script>

rewriteURL: lets you support sessions. You can replace any link within your page like
<a href="some_url">Your link</a>
with
<a href="some_url?id_name=id_value">Your link</a>
(the same will be done also for any forms action). You can set only a name for the new parameter (value will be generated) or the whole pair: name and value.

E.g.:


<script language="JavaScript">

 <!-- add sessionId=some_value to the link -->
  java.j2j.rewriteURL(document,'sessionId');

<!-- add id=12345 to the link -->
java.j2j.rewriteURL(document,'id',12345);

</script> <

Parameters for Java functions

In all cases when we are looking class constructor (in the global script) or class method (in the html page) we need to make some conclusion about parameter types.
If the current parameter represents integer we assume this type is int
If the current parameter represents float we assume this type is float.
If the current parameter is true or false we assume this type in boolean
In all another cases we assume this type is String.

For example:

<!-- we assume A(int,float,String,boolean) -->
java.myClass.myFunc(2, 5.6, a123, true);

for making sure you pass some parameter as a String you can use " or '
<!-- pass value always as a  String -->
java.myClass.myFunc("256");

If some string value is enclosed into " or ', it will be passed to java without that.
E.g.

<!-- within myFunc parameter type is a String and value is a 222 , not a "222" -->
java.myClass.myFunc("222");

Funcntion results

If java function has got type void then empty string will be returned to JavaScript. Otherwise will be returned string representation of Java objects (if it is not an array). If your Java function returns array or Vector or Enumeration this result will be presented as an array in JavaScript. E.g. if you have some function performQuery(some_sql_string) in the class dbpool you can invoke SQL request through JDBC, convert ResultSet into array (or Vector for example) and return this array to JavaScript:

res=java.dbpool.performQuery("select * from emp");
first=res[0];
second=res[1];

J2J console

You can run the same servlet directly from the browser and create/delete objects. The appropriate command is:

http://your_host/servlet/J2J?your_password

or, if you are using mapping for extention j2j:

http://your_host/your_file.j2j?your_password
 

You can set this password as an initial parameter for servlet. The name of this parameter is admin.

By default this parameter value is welcome, so  J2J?welcome should runs administrator's page.

Through this page you can delete objects and create new objects. For doing last action just set object name, class name and parameters string (if you need to run non-default constructor).
 

What is new in 1.5:
    - updated codebase

What is new in 1.4:
    - performance issue
    - bugs fixed

What is new in 1.3:
    - performance issue (major upgrade)
    - free version supports up to 5 Java object

What is new in 1.25:
    - Vectors and Enumerations support added

What is new in 1.24:
    - URL rewriting added

What is new in 1.23:
    - arrays support added

What is new in 1.22:
    - console added

What is new in 1.21:
    - new functions for standard library

What is new in 1.2:
    - standard library
    - some bugs fixed
    - performance issue
 

For downloading:  The current distributive contains J2J servlet and bridge applet (you need both of course).

            J2J     bridge
 

© Coldbeans      Comments?