Overview
Web developers can use Java to make COM components which will provide additional features to their web applications. In this
tutorial we will compile a Java class and then we will register it into the system registry. Later we will access
it as COM object from our web pages.
Tools of trade
I will presume here that your operating system is Microsoft
Windows. You will need a Java compiler, you can download Java Development
Kit from Microsoft's SDK for Java
web site. It contains the javareg.exe tool which we'll be needing
later to register our java classes as COM components. Just make sure you download the latest
version.
Writing the code
Open your favorite notepad and write down the following Java code
exactly as it is :
public class ReturnText {
public String GetText() {
return "Hello World!";
}
}
You may want to copy the above code and then paste it onto your
note pad. If you examine the above code, you will notice that our
above Java class is pretty much straight forward with only one
public method GetText, which returns a String "Hello World!". Now
save this file as "ReturnText.java".
Compiling ReturnText.java
Now go to the DOS prompt and compile Java class file using
the command below :
jvc ReturnText.java
A ReturnText.class file will be created if every thing goes well.
Registering Java class
We will register ReturnText.class using javareg. JavaREG is a tool
for registering Java classes as COM components in the system Registry.
Write down the following command at the DOS prompt :
javareg /register /class:ReturnText
/progid:StarDeveloper.ReturnText
/typelib:ReturnText.tlb
After successfully registering the Java class file you will see
a dialog box telling you that your Java class has been registered.
Now copy ReturnText.class file into windows/java/trustlib/ directory
on Win 9x or winnt/java/trustlib/ directory on Win NT. This copying is
a must step, our COM object won't work without it.
Calling ReturnText component from the web page
Open your favorite notepad and write down the following code :
<%
Dim sd
Set sd = Server.CreateObject("StarDeveloper.ReturnText")
Response.Write sd.GetText
Set sd = Nothing
%>
Now save it as ReturnText.asp in your root directory of your server.
Note that you will be requiring Personal Web Server on Win 9x or IIS on
Win NT. You can place your ASP page any where you want from where you can
call it using HTTP. I am just simplifying the things out here. Now point your
browser to http://localhost/ReturnText.asp. If everything has gone on well you should see the following
output on your web page :
Hello World!
Congratulations you have just built and tested your first Java COM object.
Having done this you can now make better and useful COM components for your web site
and applications.