Microsoft KB Archive/318735

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

Article ID: 318735

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Software Development Kit for Java 4.0
 * Microsoft Visual J++ 6.0 Standard Edition

-



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.  Create a text file named Class1.java.  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 &quot;HelloWorld&quot; through IDispatch Dispatch.call(script, &quot;HelloWorld&quot;); }       catch (ComFailException cfe) { cfe.printStackTrace; }   } } The following sample is the HTML code that loads the Java code-behind through the  tag. This sample also contains the script method HelloWorld, which is called in the Java code-behind module in the onDocumentLoad method.

 Create a text file that is called Page1.htm.  Copy the following HTML code to Page1.htm.  

Q318735 - How To Call HTML Script Code in a Java Code-Behind HTML Module (You see an 'alert' dialog appear with the text &quot;Hello World&quot;)

<OBJECT classid=&quot;java:com.ms.wfc.html.DhModule&quot; height=0 width=0 ... VIEWASTEXT> <PARAM NAME=__CODECLASS VALUE=Class1> <PARAM NAME=CABBASE VALUE=Project1.CAB> </OBJECT>

<SCRIPT> function HelloWorld { alert(&quot;Hello World&quot;); } </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.

</li>  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 &quot;CN=MyTestKey&quot; Project1.cer cert2spc Project1.cer Project1.spc signcode -j javasign.dll -jp LOW -spc Project1.spc -k MyKeyName Project1.cab </li> 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.</li></ol>

Keywords: kbhowto KB318735

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.