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



The information in this article applies to:

  • Microsoft SDK for Java 4.0
  • Microsoft Visual J++ 6.0

This article was previously published under Q318735

SUMMARY

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.

MORE INFORMATION

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 Class1.java.
  2. Copy the following code to Class1.java:
    import com.ms.wfc.html.*;
    import com.ms.wfc.core.*;
    import com.ms.com.*;
    import com.ms.wfc.html.om.*;
    import com.ms.wfc.html.om.shdocvw.*;
    
    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
    			Dispatch.call(script, "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="java:com.ms.wfc.html.DhModule" 
            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.java
    cabarc n Project1.cab Class1.class
    makecert -sk MyKeyName -n "CN=MyTestKey" Project1.cer
    cert2spc Project1.cer Project1.spc
    signcode -j javasign.dll -jp LOW -spc Project1.spc -k MyKeyName Project1.cab 
    					
  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 Project1.cab file must be in the same directory with the <OBJECT> tag as specified earlier.

Modification Type:MinorLast Reviewed:6/29/2004
Keywords:kbhowto KB318735