Make DHTML more Dynamic with Java Applet

In a rarely cases, DHTML is not enough only with the use of Javascript. Some specific purpose in a web page just couldn't be supplied with it. Basically, Javascript has several limitations, especially in a page that need to retrieve information related to local machine resources.

Let say - for example - how to list printer names (on web based application) that installed locally in local machine? Without ActiveX object available in Windows & Internet Explorer browser, there is no option to gain with it. So, what's so special with "Dynamic" terms in DHTML if it stuck with specific OS's or browser? Please pay attention, Java Applet is the only key for the answer (multi-platform, multi-OS & multi-browser - theoretically :).

Well, I'm not a Java expert that order you to moved to become a Java programmer since my self is not included to the statement. Anyway - on this chance - I just tried to tells that "there's many ways to Rome", there's lots of things we can do to make our web page function properly accepted, but there's only a way to list printer name installed in local machine. Just spread up your creativity, make a R&D & your done! This article dedicated to peoples who follows Method 8 Framework & founder of jZebra (Hi, Tres. Looks like I need to modified the source :).

Before it started, imagine first: "I'm a new to Java programming, how can I make an applet?" and "Okay, I made it, so how can I embedded it to the page?" or "How can Java Applet communication with Javascript?"

As sequentially within the story, I'll share the answer. First of it, provide Netbeans & JDK installation file & configure it on your machine. Then, create an Applet project from Netbeans. On the example, I named both the project, class & java code with printName.

package printname;
import javax.print.PrintService;
import java.awt.print.PrinterJob;
import java.applet.Applet;
public class printName extends Applet {

public static void main(String[] args) {
String printerName;
printerName = menuList();
// just make a call to print out its value
System.out.println(printerName);
}

public static String menuList() {
// Lookup for the available print services.
PrintService[] printServices = PrinterJob.lookupPrintServices();

// Iterates the print services to a variable
String printerName = "";
for (PrintService printService : printServices) {
String name = printService.getName();
printerName = printerName + "#" + name;
}
return printerName;
}
}


Save & run above compiled code, you'll see that the applet will return a line of string of local printer name which separated with "#" character. Build & clean the code to make the JAR file.

Now, copy/upload the JAR file to the same folder where the HTML laid. Return to HTML editor & create a param tag to embed the JAR file. Also, create a dropdown select since we're gonna list that printer name from the applet to that object.

<html>
...
<script language="JavaScript">
function addOption(selectbox,text,value) {
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

function removeAllOptions(selectbox) {
var i;
for(i=selectbox.options.length-1;i>=0;i--) {
selectbox.remove(i);
}
}

function fPrintName() {
var afPrintName = document.printName.menuList();
removeAllOptions(document.fInput.printerName);
addOption(document.fInput.printerName,'-- Pilih Printer --','');
if (afPrintName.length!=0) {
var namaPrinter = afPrintName.split('#');
n = namaPrinter.length;
for (var i=1; i<n; i++) {
addOption(document.fInput.printerName,namaPrinter[i],namaPrinter[i]);
}
}
}
</script>
<body onload="fPrintName();">
<applet name="printName" code="printname.printName.class" archive="printName.jar" width="1" height="1"></applet>
<form name="fInput">
<select name="printerName"></select>
</form></body>
...
</html>


Done, upload to the web server directory. Now, check your current printer from the control panel. There's 2 printers listed on my system.



Open your browser & locate to the html page. For the first usage, a standard permission window may appear since it need to confirm the security in the client side.




Finally, here come the result. The printer name now listed to the page.



CONCLUSION
The use of Java Applet on this case helping user to choose a selected printer name they want to print out with. It's useful to combine with jZebra since it only works with printer name that specified before. The Javascript core function located on fPrintName(). This function (first line -- document.printName.menuList()) purposed to communicate with the applet embedded in param tag, also to parse the string value returned from the applet. With 4kb of JAR executable size, this is so light-weight applet.

Labels: , , , ,


PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address to receive feed update from this blog:

Post a Comment

 

Post a Comment

Leave comments here...