I wanted to talk about the power and platform that Phobos offers and how easy it is to use. Let's take an example of wanting to accomplish a simple task like reaching out across the Net in a script and retrieving a web page and sending it back to the browser.
The first thing we have to do is figure out a way to reach out across the Net. If we were programming in say asp, we would need to use a com object or dll control as vbscript does not have the power to do that. Phobos does have a library function to accomplish this task, but lets assume that it didn't. What do we do? Hhhhmmmm . . . well every class in Java is available to us, or any Java class that is installed into your Phobos installation for that matter. So in order to solve our problem, I did a quick Google search for a Java example with the terms "Get URL Java" and landed on the following URL: http://www.devdaily.com/java/edu/pj/pj010011/index.shtml
Now this example will give us a step by step procedure for doing what we want to do in Java, but we are writing in Javascript. All we have to do is rewrite the example in Javascript. It is quite easy. Here is the code for a Phobos script to do the same thing:
********************************************************************
//Set status and content type
response.setStatus(200);
response.setContentType("text/html");
// Get writer
writer = response.getWriter();
// Create the URL object
var u = java.net.URL("http://192.168.0.76/");
// Open an input stream from the url.
var is = u.openStream(); // throws an IOException
// Convert the InputStream to a buffered DataInputStream. //
// Buffering the stream makes the reading faster; the //
// readLine() method of the DataInputStream makes the reading //
// easier.
var dis = java.io.DataInputStream(java.io.BufferedInputStream(is));
// Now just read each record of the input stream, and print it out.
while ((s = dis.readLine()) != null) {
writer.println(s);
}
//Close the InputStream
is.close();
// Flush to the browser
writer.flush();
*************************************************************************
See how easy that is! Using Java classes just like they are native to Javascript. Now, we should do some error trapping and other checking, but the code above illustrates how incredibly easy it is to use Java classes.
Another great benefit of using Phobos and Java for that matter is the Java Virtual Machine. It is so powerful, you sometimes forget what it is doing for you. For example, I developed a stub application on a 32 bit workstation and tested and ran it. I then deployed it to a 64 bit dual processor Windows server running the Sun Glassfish Application Server and of course it ran flawlessly. I also deployed to a Linux server running Glassfish and it ran the same. Portability and power available to every developer!
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment