You are currently offline, waiting for your internet to reconnect

How To Call HTML Script Code in a Java Code-Behind HTML Module

This article was previously published under Q318735
The Microsoft Java code-behind Hypertext Markup Language (HTML) module permits you to load a Java code module from an HTML page and make a change to Microsoft Internet Explorer Dynamic HTML (DHTML) through the Java DHTML classes. You can also directly make a change to the Microsoft Internet Explorer Object Model from this code-behind module. The sample code in this article uses the Microsoft virtual machine (Microsoft VM) Component Object Model (COM) capabilities and shows you how to query for the required COM interfaces that you can use to call Internet Explorer script code directly from the Java code module.
This sample is self-contained and can be built with the Microsoft Java software development kit (SDK) tools, but is based on the Visual J++ 6.0 code-behind HTML project type. Alternatively, you can copy the onDocumentLoad method that follows to a Visual J++ generated project and use Visual J++ to build, to run, and to debug this code.
  1. Create a text file named
  2. Copy the following code to
    import*;import*;import*;import*;import*;public class Class1 extends DhDocument {	protected void onDocumentLoad(Object sender, Event e) 	{		IServiceProvider obj = (IServiceProvider)getWindowPeer();		IWebBrowserApp pBrowserApp[] = new IWebBrowserApp[1];		obj.QueryService(IWebBrowserApp.iid, IWebBrowserApp.iid, pBrowserApp);				IWebBrowser2 browser = (IWebBrowser2)pBrowserApp[0];		IHTMLDocument2 rawDoc = (IHTMLDocument2)browser.getDocument();		Object script = rawDoc.getScript();		try {			// Call the HTML script method "HelloWorld" through IDispatch, "HelloWorld");		}		catch (ComFailException cfe) {			cfe.printStackTrace();		}	}}					
    The following sample is the HTML code that loads the Java code-behind through the <OBJECT> tag. This sample also contains the script method HelloWorld, which is called in the Java code-behind module in the onDocumentLoad method.

  3. Create a text file that is called Page1.htm.
  4. Copy the following HTML code to Page1.htm.
    <HTML><BODY><H2>Q318735 - How To  Call HTML Script Code in a Java Code-Behind HTML Module</H2><p>(You see an 'alert' dialog appear with the text "Hello World")<OBJECT classid=""         height=0 width=0 ... VIEWASTEXT><PARAM NAME=__CODECLASS VALUE=Class1>        <PARAM NAME=CABBASE VALUE=Project1.CAB></OBJECT><SCRIPT>function HelloWorld() {	alert("Hello World");}</SCRIPT></BODY></HTML>					

    The Java class file for the following sample must be built into a digitally signed .cab file to work properly. If you do not run this code from a digitally signed .cab file you receive Java security exceptions as the code performs trusted operations that are not permitted by standard applet security.

  5. Use the following command lines to build the digitally signed .cab file that is used by the <OBJECT> tag in the Page1.html file:
    jvc /nomessage /x- Class1.javacabarc n Class1.classmakecert -sk MyKeyName -n "CN=MyTestKey" Project1.cercert2spc Project1.cer Project1.spcsigncode -j javasign.dll -jp LOW -spc Project1.spc -k MyKeyName 					
  6. After the class file is built, load the Page1.htm in Internet Explorer to test the application.Notice that the Page1.htm file and the file must be in the same directory with the <OBJECT> tag as specified earlier.

Article ID: 318735 - Last Review: 06/29/2004 22:48:00 - Revision: 2.1

  • Microsoft Software Development Kit for Java 4.0
  • Microsoft Visual J++ 6.0 Standard Edition
  • kbhowto KB318735