Jeff Brown
Rafael M. Muñoz
Microsoft Corporation
August 3, 1998
The following article was originally published in the Site Builder Magazine (now known as MSDN Online Voices) "Web Men Talking" column.
Contents
Registration not required - Using JavaReg
Relative positioning perchance? - Using CSS positioning
Tale of the image map - Server-side versus client-side image maps
Refresh-O-Matic - Refreshing database queries
Resourceful GIFs - Using GIFs as a resource to the Java Package Manager
The Web Men are back with more bad coffee jokes -- and more helpful information about Java classes and the Java Package Manager. Lest you worry about their caffeine preoccupations, the guys also sweeten up various solutions for relative positioning, database queries, and image maps. Grab a cuppa. And if someone comes up with a language called "Pastry," we'll do our best to pun that, too.
Dear Web Men:
I've started writing server components in Java (VJ++ 6.0 is godlike for this, best IDE I've ever used!). I have a question: Is there any way of referring to standard Java objects without "JavaReg"ing them first?
For example:
dim ourStack set ourStack = Server.createObject ("java.util.Stack") ourStack.push ("First against the stack now the revelation has come....")
If I reg a class that returns a java.util.Stack object, even if I haven't reged the java.util.Stack class itself, I can still refer to it. Oh, and another question: How can I tell what things have been "JavaReg"ed? I can't seem to find any way.
Thanks for any help you can give.
Blair 'Myrryr' Nilsson
The Web Men reply:
So Starbucks is now serving JavaReg along with their Mochas and Frappucinos? (Thats coffee talk, folks.) But JavaReg isnt a new kind of coffee; its a utility that exposes your Java classes as COM components in the Microsoft Windows Registry. As you have already seen, Blair, executing JavaReg is not necessary to reference a class -- but in your example above, we are a little wary about the lack of a Java moniker.
The key to effectively running the Microsoft Visual Basic function CreateObject in your example is ensuring that the Microsoft Virtual Machine (VM) can find the class. To achieve this, you need to use a Java Moniker and correctly position your classes in the Classpath. The use of a Java Moniker assumes that our object doesnt have to access the ASP Built-in Objects or participate in transactions . Clear as mud, huh?
All right, the details: When we refer to the Microsoft VM, we are speaking of the Java VM, which is a virtual processor emulated in software. Java VMs are distributed with the more advanced Web browsers, such as the Microsoft Internet Explorer and Netscape Navigator. The Java VM reads what is referred to as Java bytecode, interpreted instructions that execute the native instructions appropriate for the local operating system. Refer to the Microsoft technologies for Java site for information about Microsoft and Java.
Distributed COM (DCOM) introduced what is referred to as an extensible moniker namespace, which allows us to pass a string to MkParseDisplayName in the OLE32.DLL. This function then takes the string, such as java:some.class (where some.class is the full package classname), loads the Microsoft VM, and provides the class moniker to bind to the object.
The Classpath is the location on your system where the Java compiler can access your classes or packages. By default, the Classpath for the Microsoft Java compiler is operating system\java\classes. There are various ways to set the Classpath, and you can find more information on this in CLASSPATH Environment Variable on MSDN Online.
Lets look at an example that would work on our own Java classes built with Microsoft Visual J++ 6.0 Tech Preview 2 . We can build myJavaClass.java and copy myJavaClass.class into our Classpath, which for our example will reside in \windows\java\classes. An easy way to build class files into the Classpath from within the IDE is to set the "Output Directory" in the Project Properties Compile tab to be "c:\windows\java\classes". This will instruct the integrated development environment (IDE) to output your class file into the trusted Classpath when you do a project build.
Here is a simple Java class file generated by creating a new project of type "Empty Project" in the "Visual J++ Projects" main folder in the "New Project" dialog box. In the new project, select Add Class from the Project Menu, and add a new Class. Name the java file/class myJavaClass.java in the Add Item dialog. Now, create a public method so that you have the following Java class:
public class myJavaClass { public String somePublicMethod() { return "Hello from somePublicMethod"; } }
Set the project properties as described above to create a class file in the Classpath and build the project.
Then, using a Java moniker, you can write the following Visual Basic code:
Dim myJavaObject as Object set myJavaObject = Server.GetObject("java:myJavaClass") Print myJavaObject.somePublicMethod
The Java VM will now look in your Classpath to find your class and create your object, and you won't need to run JavaReg.
Now, should you want your object accessing the ASP Built-in Objects or participating in transactions, you will want to have your class registered -- thereby requiring JavaReg. Another reason to run JavaReg on your class is to use your class as a COM object within another language. Also, if you plan to execute your classes remotely (on another computer), using DCOM, you will want to have your classes registered.
In response to your last question: How can you tell what is registered? You can use OleView to find any registered Java classes. Anything that has MSJAVA.DLL registered as the InprocServer32 launching application will be considered a Java/COM object. With later versions of JavaReg, you might also notice registered objects having an InprocServer32 JavaClass key.
Other resources on this topic:
Dear Web Men:
I have a question regarding Cascading Style Sheets. I know that you can position text from the upper-left corner of the browser. I was wondering if you can position text from the corner of a specific graphic. If this is possible, could you please let me know how to accomplish it? Thank you.
Shane Trotter
The Web Men reply:
This is definitely possible, Shane. You can set the Cascading Style Sheet (CSS) position attribute for the text to use relative positioning. With relative positioning, you use the left and top attributes for the element to position it relative to the preceding element in the page.
Below is a graphic followed by some text that is positioned relative to the graphic
using this HTML:
<IMG SRC="/msdn-online/shared/graphics-home/Chrome-illust.jpg"> <DIV STYLE="position:relative; top:-15px; left:60px"> Hi, I'm the positioned text</DIV>
You can see that an inline style is used to set the positioning for the DIV element relative to the image. It is positioned 15 pixels toward the top and 60 pixels to the left of where it would normally be positioned in the flow of the page. It will always retain this relative position to the image, regardless of whether the page is resized, scrolled, and so on.
The first thing you mentioned in your question -- positioning text from the upper-left corner of the browser -- is done through absolute positioning. With absolute positioning, the top and left position of the element are relative to the parent element, regardless of the position of any other elements on the page. Elements can contain other elements (i.e., be parents of other elements), as you probably know. But if an element is not contained by another element, its default parent is the <BODY> tag.
To find out more about pinpointing an element's position, see the DHTML, HTML & CSS section of the Web Workshop.
Dear Web Men:
I want to do mouseovers on an image with an underlying layer of coordinates. How would you describe this layer of coordinates to me? It seems like the old image map, but I don’t want to keep going back to the servers.
Thanks,
Jim Davis
The Web Men reply:
Once upon a time, there were server-side image maps. Browsers were young and still growing. Then came the browser wars. History changed. Client-side image maps were created. Speed, efficiency, and robustness became benefits. Designers had more options. All were happy. Wow, that was better than Aesops Fables.
Jim, that server-side option you're refering to is rapidly being replaced. In fact, you can actually combine both server-side and client-side image mapping -- making cross-browser development real easy. Client-side image maps came about with the release of Netscape Navigator 2.0, and can be thought of as the default when both options are available. A great reference is a tutorial found at the CERN site, Image Maps Creation and Application .
A server-side image map will always return to the server with the mouse-click coordinates where it checks a .map file to find its next destination. You define a server-side image map by using the ISMAP attribute within the IMG tag. For more information on server-side image maps, refer to Using Image Maps with Internet Information Server in the Microsoft Knowledge Base.
To define an image as a client-side image map, we use the USEMAP attribute within the <IMG> tag to point to the MAP element containing the AREA element, which is located within your HTML page. Then the AREA element uses the SHAPE, COORDS, and HREF attributes to define the shape, the coordinates for the shape, and the destination URL or anchor point respectively.
All these elements and attributes make the client-side image map more robust, because you can also use attributes -- such as TITLE to bring up a tool tip per area and TARGET to point to the frame or window to which you target the contents. Refer to the Web Workshop for all the options available to the AREA element.
Do you have to calculate all the coordinates of an area by hand? You can, but there is actually a lot of software on the market that will easily do it for you. For a good list of software and the platforms on which it is available, see Software available in each Platform at the CERN site. By the way, some of it is free!
The creation of Dynamic HTML (DHTML) also leaves the use of image maps up to the imagination. Changing attributes, playing with the z-order, and even determining the mouse location -- all on the fly -- can create cool effects. In fact, a couple of great samples for your self-study can be found in the Samples area of the MSDN Online.
Dear Web Men:
I have a little ASP Web site, and it has been requested that I add one feature to the otherwise perfect site. After a user logs on and pulls up a page of database info, I want to set the page to automatically requery the database -- say, every 10 minutes -- while the user has the page open, without the user having to hit reload manually. This is due to the fact that there will be data entry people updating information at the same time. I've searched many ASP resources, books, online, scraps of paper, and have been unable to find any kind of reference that will shed any light on this. Any help will be appreciated.
Thanks!
Jessika Sutton
The Web Men reply:
Wow, we're sorry to hear you had to resort to searching for scraps of paper. But we think we have a tip that may end your search -- because the last thing we want your users to have to do is click on the Refresh/Reload button. Geez, the next thing you know, they'll have to get up from the couch and change the TV channel!
One way to achieve this auto-requery behavior is to add a <META> tag to the HEAD section of the page, and set the HTTP-EQUIV and CONTENT attributes to reload the current page after a specified amount of time. We did this in some sample code for our Show me the money! answer about advertising banners. Here's the code:
<HTML> <HEAD> <TITLE>Banner ads sample</TITLE> <META NAME="ROBOTS" CONTENT="NOINDEX"> <META HTTP-EQUIV="REFRESH" CONTENT=5> </HEAD>
In our sample, we set CONTENT equal to 5 to specify the page reload every 5 seconds, but you could set it to whatever time (in seconds) you want. If you want it to update every 10 minutes, then just set the CONTENT attribute equal to 600.
There are other ways to get this effect, too. You could do it with client script -- or if you know that your users will be using Internet Explorer as the browser, you could create a channel and specify a regular update schedule.
Dear Web Men:
Here's my situation. I'm desperately trying to install an application via the Web. I have created Distribution Units to install my Java classes, using Internet Explorer 4.0's Java Package Manager. I have successfully installed the JAVA classes (and they work!), but unfortunately I am not able to access non-class files (i.e., GIF files). How do I access these GIF files that have been cached in Internet Explore 4.0 (using Java)?
Thanks
Chris Smith
The Web Men reply:
Many thanks to Derek Smith of the Java Support team. His knowledge of Java and Distribution Units makes this sound easy.
Here's a riddle for you, Chris: What do you call an image when it's stored in the Java Package Manager (JPM) ? Answer: A resource. A resource in Java, you ask? Read on....
Your ClassLoader is the Java class that knows how to find your classes, be they in the JPM, in a CAB, on a server, or whatever. It also has a method for finding things that aren't classes: ClassLoader.getResourceAsStream(). You can use it to read pretty much any kind of data from any place where you would have stored class files.
Create the Distribution Unit using DUBuild, as usual, but add the GIF files by adding "/I *.gif" to the command line. Include any other files you need in a similar fashion. To find more information on Distribution Units and component download, refer to Microsoft Internet Component Download Overview.
To read the image file, make a call such as this:
InputStream inStream = getClass().getResourceAsStream ("/path/myImage.gif");
To create the image, read the file into a byte array and pass that array to Toolkit.createImage().
There are a couple of things to remember. That string you pass to getResourceAsStream uses Java-style forward slashes to separate directories, and that first "/" serves to make the path absolute. Also, getResourceAsStream was added to ClassLoader with the Java Development Kit (JDK) 1.1, so this won't work with a Microsoft Virtual Machine prior to Internet Explorer 4.0 or SDK 2.0 for Java .
By the way, this trick works whether you're developing the applet (and running off your hard disk), loading the applet via a regular CAB, or running with the classes installed in the JPM.
Other resources on this topic:
Jeff Brown, when not forcing family and friends to listen to Zydeco and country blues music, helps develop training courses in the Microsoft Mastering Series -- with a smile.
Rafael M. Muñoz is a part-time Adonis, and full-time support engineer for Microsoft Technical Support. He takes it very, very personally every time you flame Microsoft.
Derek Smith wrote his first program on a 110 baud teletype, and still looks forward to the day when he can get a net connection that's fast enough.
For in-depth training in Microsoft Web development tools and technologies, check out the Microsoft Mastering Series . Try out the award-winning Mastering Series online by browsing free sample chapters from Mastering Series training courses.
If you haven't found the answer, search the Knowledge Base (KB) on Microsoft Support Online . For a primer, see Jason Strayer's MSDN Online Voices feature, Knowledge Is Power: Inside the Microsoft KB.
August 3, 1998
Q: Ashley wants info on creating a WebTV dating-service site.
A: The 15 Seconds Job Classifieds Site Features contains commented downloadable code.
Q: Mitch wants to set style sheet attributes for <HR> to display a graphic instead of a line.
A: We could not get this to work. To make it easier to find or maintain graphic horizontal rules included with an <IMG> tag, use the CLASS attribute and assign them all the same class name.
Q: Phil is having problems with borderless frames for Internet Explorer and Netscape.
A: See Weve been framed! in the Web Men archives.
Q: Rob Stocks wants a server to return database query results as bar or pie charts rather than tables.
A: If you are using Active Server Pages (ASP) technology, check out the ASP Components Catalog. ServerObjects Inc.'s ASPChart is another available charting component.
Q: Eric Schultz wants to display Internet Explorer in full-screen mode.
A: Refer to the Web Men Shorts posted on December 1, 1997.
Q: Tom Leonard asks where Internet API functions for Windows are documented.
A: See the Web Workshop sections Content & Component Delivery and Networking, Protocols & Data Formats.
Q: John wants to modify the Internet Explorer logo in the upper right-hand corner of the browser.
A: The Internet Explorer Administration Kit allows full customization of the browser.
Q: Andy Wilson asks how create and manipulate HTML documents from a Visual Basic application.
A: You can display and navigate to HTML documents using the Web Browser Control, but you may want to create HTML documents just as you would open and write other text files in Visual Basic. Future versions will have more support for building DHTML applications. Refer to the Reusing Browser Technology section of the Web Workshop.