Introduction
In this article we will learn how to access ASP intrinsic Objects from within
COM component built in Java using Microsoft Visual J++.
Practical Application
You can access Response and Request
Objects to create presentation layer components. An example of this type of component
will be an Ad Management component which will output HTML banner code by internally
accessing the database and randomly choosing an advertiser.
Note! that we will not discuss data access from Java COM component in this article.
This article is concerned only with calling using ASP intrinsic objects from within Java
COM component.
What do I need ?
We will create this Java component using Microsoft Visual J++ 6.0. No prior Java/COM
experience is required but familiarity with Java is a plus.
Start new Project
- Open Visual J++ and click Open -> New Project.
- Select the Components folder from 'Visual J++ Projects'. Then from the options on
the right select 'COM DLL'.
- Name this Project as 'Stardeveloper' and in the location box give it the location
where you want to store the project files and the compiled component.
New Project
- You will be prompted a couple of windows about some legal issues involved with
using Microsoft extensions in your Java component. Ignore them by pressing 'OK'.
- In the Project Explorer window on the right rename 'Class1.java' to 'JavaASP.java'.
Note 'Class1.java' is the default Java file that Visual J++ opens for you.
- Now add the following code into it and then save it :
import com.ms.iis.asp.*;
import com.ms.mtx.*;
These lines should be added above the line which says 'public class Java1'. These
lines are import commands which import certain packages of Java Class files which we
will be needing in out project.
- Now rename the class 'Java1' to 'JavaASP' and then add the following code into it.
The complete code looks some thing like this :
/**
* This class is designed to be packaged with a COM DLL output format.
* The class has no standard entry points, other than the constructor.
* Public methods will be exposed as methods on the default COM interface.
* @com.register ( clsid=2519BE74-3B7C-488E-8564-8F12F53432C5, _
* typelib=4122F43C-6F76-4374-B957-3277398FC3C8 )
*/
import com.ms.iis.asp.*;
import com.ms.mtx.*;
public class JavaASP {
public boolean Hello() {
Response myResponse = AspContext.getResponse();
myResponse.write("Welcome to Stardeveloper.com");
return true;
}
}
- Now the last step. Go to Project -> Stardeveloper Properties ( at the bottom ).
From the different tabs select 'COM Classes'. Under 'Automatically generate Type Library'
check the 'JavaASP' class and hit 'OK'.
Project Properties
That's it. We are now ready to build our 'JavaASP' component which calls
Response Object of ASP and writes a response back to the client
browser.
Building the Project
We are now ready to build our Java COM component.
Go to Build -> Build. On the status bar at the bottom left you will see different
status messages appearing that the component is being registered and type library is
being created. After that you will see a success message saying 'Solution Update
Succeeded'. Congratulations you just built your first Java COM component which accesses
the ASP built in objects.
Registering on the Remote Server
When we build a COM component in Visual Studio, it automatically registers the
component and builds the type library. But what if you want to use your component on
some other server ? Then you will have to register your component first before using
it.
To register your component on other server use the following command at the DOS-PROMPT:
C:\regsvr32 Stardeveloper.dll
Note in the place of Stardeveloper.dll you will write the name of your DLL file.
Creating com.asp test page
Open your favorite ASP editor and create a new ASP page. Name it 'com.asp' and
copy paste the following code in to it :
<% Option Explicit
Response.Buffer = True
Response.Expires = 0
%>
<html>
<head>
<style>
body,p { font-size:8pt; font-family:Verdana; }
</style>
</head>
<body>
<p align="center" id="com"><b>
<%
Dim JavaASP
Set JavaASP = Server.CreateObject("Stardeveloper.JavaASP")
JavaASP.Hello
Set JavaASP = Nothing
%>
</b></p>
</body>
</html>
Above code simple creates a JavaASP object out of the Stardeveloper.JavaASP component.
And then we call it's only method Hello to show the use of
Response method from the component.
What is ProgID ?
The argument that you give to Server.CreateObject is the
ProgID of the component you want to use. So how do you know then what is the ProgID of
the component you create and how can you change it ? Well it is very simple. In Visual
J++ and Visual Basic, the name of your Project becomes the first of part of your
component's ProgID e.g. in this article the Project name was 'Stardeveloper' thus the
ProgID of our component started with 'Stardeveloper.JavaASP'. Then the class name
becomes the second part of the ProgID e.g. in our case class name was 'JavaASP' thus
the complete ProgID became 'Stardeveloper.JavaASP'.
Explanation
I deliberately left the explanation at the end so that you should first compile
and test the Java component and then I will explain what you need to do to call the
ASP intrinsic objects from within Java COM component.
import com.ms.iis.asp.*;
import com.ms.mtx.*;
All you have to do is to first import two very important Java packages in the
.java file. These two packages provide basic classes and interfaces to access the
methods of ASP built-in objects.
Response myResponse = AspContext.getResponse();
Request myRequest = AspContext.getRequest();
Server myServer = AspContext.getServer();
Session mySession = AspContext.getSession();
Application myApplication = AspContext.getApplication();
Then within the methods of your Java class or within the constructor you can
create the ASP objects you want by using the AspContext
class's getXXX<object_name>() methods as shown in the
code above. Now tell me it was difficult. Only with a couple of lines of Java code
you get access to the functionality provided by the ASP objects.
Tip
Only create the objects which you want. For example if you want to write response
back to the client then simply create the Response object.
It enables you to create faster and more efficient components without using too much
memory.