|
|||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Credits
Disclaimers
IntroductionI have been living and breathing modern COM for over two years now as a technical lead on the toolkit team of my companys eCommerce product. I have understood the significance of well-designed interfaces and of implementations that work well within various environments like Visual C++, Visual Basic as well as scripting. The power of interface-based programming is forever entrenched in my mind and I try to apply it to everything I am involved in as a software engineer. Over the past one year, experience has taught our industry that all business logic should be encapsulated within COM objects while ASP + Scripting should be used only as glue for these objects. Designing and developing infrastructure and business objects typically requires a higher skillset as compared to what is required to actually use the same. The preferred environment for developing these objects (at least in my organization) is Visual C++ / ATL / STL. Microsoft is also promoting Visual Basic as an alternative development environment for these objects. These objects are typically referred to as faceless because they implement a lot of logic but no user interface. The presentation tier is either a rich or thin client that has logic to present information to and gather information from the end user. Such a client then uses the faceless objects to do something meaningful with that information. The whole idea is that the presentation tier typically requires a lot of customization while the business objects do not change as frequently. The changes required for the presentation tier can also be implemented using less experienced programmers. Applying these same principles to the browser environment, it seems logical to encapsulate sophisticated client side logic within faceless, binary modules while using a scripting language to handle the presentation aspects. The options for these modules on the Windows platform are Java applets and ActiveX controls / servers. The focus of this article is on using Java applets for achieving this goal because applets are browser, platform and processor independent (for the most part). A brief history of appletsSUN Microsystems introduced Java applets with much fanfare back in 1995. Applets immediately took the web world by storm because they added the ability to display dynamic web content within browsers in what was essentially a static HTML world. During those initial days, it appeared that using Java applets was the best way to add dynamic content to web pages. Microsoft initially tried to counter SUNs offering with their ActiveX Control technology but there are two primary issues with using controls within web pages:
When Dynamic HTML finally started taking shape, things changed drastically. The Document Object Model (DOM) exposes elements within a web page as programmable components with their own set of properties and methods. Even though the implementation of dynamic HTML within the Internet Explorer and Netscape Navigator browsers is vastly different, the underlying theme of programmatically changing content using scripting code within the displayed page itself was a BIG hit. Applets suddenly started to look old and primitive. The W3Cs endorsement of Dynamic HTML finally set the tone for the new breed of sophisticated, dynamic web pages. There are several advantages to using Java applets within a browser as listed below
The disadvantages to using applets are
A quick overview of using Java appletsJava applets are included in an HTML page with the applet tag. Section 13.4 of the HTML 4.01 Specification on the W3C site documents this tag in detail. It also mentions that this tag is deprecated in favor of the <object> tag. A sample HTML page containing an applet is shown below: <html> The attributes used in the above example are explained below.
Only the code, width and height attributes are required. The <param> tag contains name value pairs that allow the applet to be configured when it is first started up. A sample JavaScript function that invokes a method on the above applet is shown below. <script language="Javascript"> Division of laborIn the Introduction to this article, I suggested an approach wherein the sophisticated browser side processing is encapsulated within faceless Java applets while the presentation is managed by JavaScript code. This approach requires bi-directional communication between Java and JavaScript. The next few paragraphs investigate the available options.
Accessing public members and functions exposed by a Java applet from
JavaScript code is straightforward as demonstrated in the
For example, document.Calculator.SetCalculatorMode(Mode);
Communication in the other direction (Java to JavaScript) is achieved
using the If Netscape Navigator 4.0+ is installed on your machine, these .class files are available in the Java40.jar file which itself is located in the <Navigator Installation Directory>\communicator\program\java\classes directory. I also found these .class files in four different .zip files located in the <Windows Installation Directory>\Java\Packages directory. These .zip files were obviously installed by Microsoft products because they contain many com.ms packages. The point is that these two classes are available with either browser and you can set your CLASSPATH environment variable to any of the above paths. An alternative approach is to extract these files from the .jar or .zip files into your applets directory using a utility like WinZip. The JSObject classA brief description of the JSObject class member functions is included for better understanding the usefulness of this class. public static JSObject getWindow (Applet applet )
public Object call ( String methodName, Object args[ ] )
public Object eval ( String s )
public Object getMember ( String name )
public Object getSlot ( int index)
public void removeMember ( String name )
public void setMember ( String name, Object value )
public void setSlot ( int index, Object value )
public String toString ()
As is clear from the examples presented above, the public methods of the JSObject class are not restricted to invoking JavaScript functions from within a JavaApplet. They also enable an applet to directly manipulate the Document Object Model (DOM) elements. Full documentation on these classes is available at http://developer.netscape.com/docs/manuals/communicator/jsref/pkg.htm The documentation also explains how data types are handled between Java and JavaScript. Significance of the MAYSCRIPT attributeEven if an applet uses the JSObject to invoke JavaScript functions or to directly access the Document Object Model, the JSObject methods will fail if the <applet> tag does not include the MAYSCRIPT attribute. This enables the page designer to determine if an applet can call out to JavaScript. Communication between faceless appletsWhen using faceless, reusable Java applets within a web page, it is possible that one applet may need to directly communicate with another. Such a call could also be channeled through an intermediate JavaScript function but it is always better to know all the available options. The AppletContext interface in the java.applet package gives an applet limited access to the applets context of execution like the browser that the applet is runing in, the web page the applet is on as well as other applets on the same page. For example, here is an HTML page that contains two applets. <html> The following code shows usage of the AppletContext object. AppletContext context = getAppletContext();
Another option is to use the IUnknown::QueryInterface in Java
Class Object is the root of the class hierarchy. Every class has
Object as a superclass. All objects, including arrays, implement the
methods of this class. The
I have not tried invoking the Installing the sample filesThe sample code for this article is packaged in the Java2JavaScript.zip file. The sample files demonstrate a chat application that has been simplified to route messages on the client side itself. In real life, the users participating in a chat are on separate machines and a message is sent to a server that then broadcasts it to all the participants. The files that comprise the sample are
The steps to install and run the sample program are
Enter some text into the two input fields and click on the corresponding Send button to see the message displayed in the chat window. Sample code explainedChat applications are a popular collaboration mechanism on the web. The two types of chat applications that I have seen are
Recently I had to implement a production quality chat application for our eCommerce product. After a lot of thought, I decided to use a hybrid approach. First and foremost, the Java applet is a faceless applet that implements the ISession interface. public interface ISession
I have modified this interface slightly from the production version
of the Chat application to include the Author parameter for both
The ChatClient.java file is the actual implementation of the ISession
interface. The functions // Get the JavaScript window that will have the various scripts that this applet will call. Since the Java applet calls out to two different JavaScript functions, I decided to enable the web developer to be able to specify the names of these functions as paramerters to the applet, while providing defaults for the same. m_strMessageHandler = getParameter("MessageHandler");
if (m_JScriptWin != null)
The TestChatClient.htm file deals with the presentation aspects of the chat. The applet is included on the page using the <applet> tag. <applet id="ChatApplet" width="1" height="1" code="ChatClient.class" codebase="." VIEWASTEXT mayscript> The parameters specify names for the two JavaScript functions that the applet invokes. Ive just indicated names other than the default to demonstrate how flexible this can be. The two forms on the page simulate two people chatting with each other. The corresponding HTML is straightforward. The actual messages themselves are displayed within the ChatMessages <DIV>. In the case of Internet Explorer, I use the Table Object Model to display each message in a separate row. Hence the ChatMessagesTable definition within the above DIV.
Finally, the Final Thoughts
This article attempts to present some (neat) techniques for
implementing your browser side logic. As I have mentioned earlier in
the article, the As for the demonstration sample that accompanies this article, I feel that allowing the presentation of the chat to be implemented in JavaScript / DHTML enables this code to be maintained by an entry level / junior programmer. Customization of the user interface is also easier using DHTML / JavaScript. Additionally, it allows for more powerful presentation techniques that seem consistent with the rest of the page contents. In the production version of this application, Ive added support for exchanging hyperlinks that open up on the participants machine, dynamic selection of colors for messages using cascading style sheets etc. Any feedback is welcome. | ||||||||||||||||||||||||||||||||||||||||||||||