Visual Java is a network-centric programming language, and many tasks that are difficult to accomplish with ASP scripts can be easily implemented in Java. At the same time, the best way to extend ASP applications is to join COM components. So, can you use Java to develop components for ASP? This paper illustrates the specific process of developing COM components in Visual J + + environment through examples.
I. Overview
Since the advent of Java, various development tools, development environment has been emerging. These environments and tools are targeted at different levels of users and have their own advantages. So what are the advantages of using Visual J + + as a Java development platform? Roughly speaking, these advantages include:
A familiar development environment. Many of Microsoft's development tools are very similar in style and usage. Users who are familiar with Visual Basic and Visual C + + development environments can quickly learn to use Visual J + +.
Collaboration with other development environments. For example, Visual J + + can collaborate seamlessly with Visual InterDev, and all HTML, ASP, and JavaScript code can be written in the same environment.
Debugger. One of the most notable advantages of using the integrated development environment (IDE) is that you can use the built-in debugger. As with all other Microsoft development environments, the debugger features of Visual J + + are also very powerful.
Of course, the IDE for Visual J + + also has many other advantages, such as color display of keywords, automatic formatting of code, application of the structure global view, and so on.
Specifically, for those developers who are itching outside the Java gate and don't know where to start, we think that Visual J + + is a very good starting point. However, we know that sun and Microsoft have been disputing the "extension" of Java by the latter. Whether or not we encapsulate Java classes into COM objects, Java programs are a good way to extend ASP applications. However, given that Microsoft private extensions are not likely to be supported by all Java platforms or environments, you should carefully consider using Microsoft's Java Extensions in Visual J + + programming.
Next we will use Visual J + + to create a component that can be used in the ASP environment. This component contains only one method, its argument is a string URL, and the function is to extract and return the contents of the HTML document specified by the URL.
This step-by-step article describes the entire component development process. This includes how to create a project in Visual J + +, enter the necessary Java code, and explain how these Java code works, and how to apply this component within an ASP script.
Note: This article will use Visual J + + 6.0, but you can also use the earlier version of Visual J + +, and when necessary we will point out the differences between the different versions.
Second, create Visual J + + Project
Starts Visual J + +, and the following dialog box appears:
"Figure 1"
In this dialog box, select the new project, enter the name of the project Fetchurl and the appropriate working directory, then select the COM DLL icon, and finally click the "Open" button.
Visual J + + will automatically create a Java file for the new project, named Class1.java. In the Project Explorer pane, double-click the project name, press the right button, and then rename Class1.java to Fetchurl.java. Double-click the Fetchurl.java file name to see the file contents. Code that is automatically joined by Visual J + + in Fetchurl.java is the skeleton code necessary to create an ASP component, as follows:
/**
* This class are designed to being packaged with a COM DLL output format.
* The class has no standard entry points, other than the constructor.
* Public methods is exposed as methods on the default COM interface.
* @com. Register (clsid=600455a0-b534-11d3-a434-0080ad38c188,
* typelib=600455a1-b534-11d3-a434-0080ad38c188)
*/
public class Class1
{
Todo:add additional methods and code here
/**
* note:to Add auto-registration Code, refer to the documentation
* On the following method
* public static void onCOMRegister (Boolean unregister) {}
*/
}
The first thing to do is change the name of the class by changing the line "public class Class1" to "public class Fetchurl". Java requires that each class's name be exactly the same as its corresponding Java filename (case sensitive).
Notice the COM directive in the above code, which starts with "@com. Register" (within a comment). This instruction tells the Visual J + + compiler to wrap the current Java class as the corresponding COM DLL file. Wrapping the compiled code in a DLL allows us to use it directly in an ASP script.
Next, we're going to modify the auto-generated initial code to give it the features that were presented earlier. Complete code engineering See the link at the end of this article, where we'll explain the meaning of the code you entered in turn.
Import java.io.*;
Import java.net.*;
These two lines of code import the Java package to use for this component. The Java API consists of a series of packages that provide code that can be used directly, similar to libraries in C + +. Using the Import keyword in a Java program is somewhat similar to using the #include command in a C + + program.
To see which classes are defined by the imported package, you can extend the corresponding directory within the class outline pane. Fetchurl uses the classes defined in the Java.io package to read data from an open network connection, using classes provided by the java.net package to establish a connection to the remote server and request documents.
public string Fetch (string strurl)
Fetchurl contains a unique method fetch, whose argument is a string that points to the URL of the HTML document that requires extraction.
String strinputline = new string ();
String strresponse = new string ();
Try
{
URL objurl = new URL (strurl);
Try
{
URLConnection objconnection = Objurl.openconnection ();
The variable strinputline will be used as a buffer for the program to read data from the remote server, and the variable strresponse will hold the entire document of the target page, and the Fetch method returns the Strresponse variable at the end. In these lines of code, the program creates a URL object and attempts to establish a connection with that object. Both lines of code are encapsulated within a try...catch block to handle any exceptions that might occur. Java uses "Exceptions" to handle exceptions (usually errors) that are encountered by any program. For example, if the URL parameter passed to the Fetch method is malformed, or if the program runs without establishing a connection to the specified server, an exception is thrown, and the program returns the exception information to the application that invokes the Fetch method. The exception handling code sees the corresponding catch keyword.
BufferedReader objinreader = new BufferedReader (
New InputStreamReader (
Objconnection.getinputstream ()));
This line of code is a bit complicated, so don't worry before you fully understand what it means. In this line of code, the program leverages the advantages offered by a fully object-oriented programming language like Java. First, it creates an object of the InputStream class with the Objconnection object, which is actually a stream that can read data from the server, and then passes the object of the InputStream class to InputStreamReader, The latter will be responsible for reading data from the InputStream object, and finally, the object of the BufferedReader class, Objinreader, is created with the object of this InputStreamReader class. The program will use this Objinreader object to complete the actual data read operation and save the result as a string.
while ((Strinputline = Objinreader.readline ())!= null)
Strresponse + = Strinputline + "
";
Objinreader.close ();
In this part of the code, the program uses a while loop to read all the data until the input is empty (null). Objinreader is an object of the BufferedReader class that buffers the read characters and returns the entire block to the strinputline variable. Then, add this strinputline and a "" to the end of the strresponse. "" is an escape character in Java that represents a new line of text. After reading all the contents of the target document, the program calls the close () method of the Objinreader object to explicitly turn off the input stream.
This fetchurl project can be compiled after all the code has been entered. Visual J + + will generate not only the usual class files from the Java source file, but also a DLL file. This DLL file encapsulates the Java class file, which allows us to access the functionality provided by Java class files from any COM environment, including ASP.
If you use Visual J + + 6, compiling the. java file into a. class file and the corresponding. dll file is simple, just select Build from the Build menu. If the machine that tests FetchURL.dll and the compilation is the same computer, no additional work is required, and if not on the same machine, you also need to register it with the regsvr32 FetchURL.dll command on the test machine, just like the registration of other COM objects.