Previous     Contents     Index     DocHome     Next     
iPlanet Web Server, Enterprise Edition Programmer's Guide to Servlets



Appendix B   Converting SSJS Applications


This appendix contains information about converting Server-Side JavaScript applications to JSPs. It has the following sections:



Differences Between JavaScript and Java

Before you can convert SSJS applications to JSPs, you must understand the differences between JavaScript and Java. JavaScript and Java are similar in some ways but fundamentally different in others.

JavaScript lacks Java's static typing and strong type checking. JavaScript supports a runtime system based on a small number of data types representing numeric, Boolean, and string values. Java has a compile-time system of classes built by declarations.

JavaScript also supports functions without any special declarative requirements. Functions can be properties of objects, executing as loosely typed methods. In Java, methods are defined in and belong to classes, and they are strongly typed.

JavaScript is a very free-form language compared to Java. In Java, you must declare all variables, classes, and methods. You must declare methods as public, private, or protected. Variables, parameters, and method return types are explicitly typed.

Java is a class-based programming language designed for fast execution and type safety. Type safety means, for instance, that you can't cast a Java integer into an object reference or access private memory by corrupting Java bytecodes. Java's class-based model means that programs consist exclusively of classes and their methods. Java's class inheritance and strong typing generally require tightly coupled object hierarchies. These requirements make Java programming more complex than JavaScript authoring.


Table B-1    JavaScript and Java compared

JavaScript

Java

Interpreted (not compiled) by client.  

Compiled bytecodes downloaded from server, executed on client.  

A scripting language that supports objects.  

An object-oriented language.  

No distinction between types of objects. Inheritance is through the prototype mechanism, and properties and functions can be added to any object dynamically.  

Objects are divided into classes and instances with all inheritance through the class hierarchy. Classes and instances cannot have properties or methods added dynamically.  

Functions may be inside classes but do not have to be.  

Every method must be inside a class.  

Code integrated with, and embedded in, HTML.  

Code distinct from HTML, although JSPs can contain HTML tags.  

Loosely cast: variable and function types not declared (dynamic typing).  

Tightly cast: variable and method types must be declared (static typing).  

Cannot automatically write to hard disk.  

Cannot automatically write to hard disk.  

Semicolons at the ends of statements are optional.  

All statements must end with a semicolon (;).  



JavaScript to Java Class Conversions



The objects used in an SSJS application must be converted to classes in Java. Table B-2 can help you convert these objects.


Table B-2    JavaScript and Java basic classes

SSJS Class

Java Class

Comments

Client  

Session  

A Java session is not as automatic as an SSJS client.  

Project  

Context  

 

Request  

Request  

 

Server  

System or Context  

Methods are split between two Java classes.  

SSJS includes a special library to handle database connectivity. Since JSPs are Java, all database connectivity is handled through JDBC. JDBC database drivers are available directly from the database vendors. It is a good idea to use pure Java (type 4) drivers whenever they are available. Table B-3 lists the database connectivity classes.


Table B-3    JavaScript and Java database classes

SSJS Class

Java Class

Comments

Connection  

Connection  

 

Cursor  

ResultSet  

There is no Cursor object; you can use methods in the ResultSet class to move the cursor.  

DbPool  

PooledConnection  

This class is in the JDBC optional package.  

Stproc  

CallableStatement  

 

ResultSet  

ResultSet  

 



Conversion Steps



When converting applications, follow these steps:

  1. Go over the structure of the application to see if there are helper classes that provide functionality between pages. Convert these helper classes first so that they are ready to use in your pages. (The hangman example contains a helper class, called hangman.js in the original JavaScript application and called JavaHangManUtil.java in the JSP application shipped with iPlanet Web Server 6.0.) Write helpers as .java files and compile them into .class files before using them.

  2. Define the methods within classes. The methods (functions in JavaScript) must declare what they return and who can access them. For example, in JavaScript you can write:

       function InitAnswer(str) { function_code }

    In Java, the same method must be declared as follows:

       public static String InitAnswer(String str) { method_code }

    This method passes in a String and returns a String. The public keyword means that any other class can call this method. The static keyword means that the virtual machine can run this method without creating an object of the class containing the method.

  3. Convert the client object of the application to a session bean. In Server-Side JavaScript, data is easier to use in the client object. In a Java Session object, the programmer must get data from the session and then put it back in if there are changes. This work can be done in a wrapper bean. The bean handling functionality of JSPs is very strong. You can specify the scope to be session and it is saved in the session automatically.

  4. Convert the HTML files to JSP files. This mainly consists of finding the <SERVER> and </SERVER> tags and replacing them with the <% and %> JSP tags. Then go through and change the syntax to correct JSP or Java syntax.

  5. It is sometimes necessary to add special lines to the file. For example, the line

       <%@ page import="HangManUtil,HangBean" %>

    imports helper classes, and the line

       <jsp:useBean id="client" scope="session" class="HangBean"/>

    uses a session bean.



Example Conversion

The hangman example, which was a Server-Side JavaScript example in the 4.1 version of iPlanet Web Server, have been converted to a JavaServer Pages example in the 6.0 version. You can find the converted example under the following directory:

server_root/plugins/servlets/examples/legacy/jsp.10/hangman


Previous     Contents     Index     DocHome     Next     
Copyright © 2001 Sun Microsystems, Inc. Some preexisting portions Copyright © 2001 Netscape Communications Corp. All rights reserved.

Last Updated May 02, 2001