A cheap trick to connect a Windows command line app to a web page for data entry and displaying results using IE.

I built a java class to process some text in a specific way, and, eventually, someone else will integrate my class into a web app they’re building. But in the meantime, we need a demo. So, I want to have a simple web page having a textarea and Submit button. When Submit is pressed, the content of the textarea is fed to the java class via StdIn. The text is processed and the results spit out via StdOut, which the web page loads and displays.

Now, of course, the ability to do this represents a potentially huge security hole, so, of course, it requires IE!

Here is the body of my HTML page. I took the initial < off so it doesn’t actually build a form here:

body>
form>
textarea cols="40" rows="5" id="document">
input type="button" value="Submit" onclick="submitIt();">
/form>
div id="processed">
/body>

A few things to note: The textarea has an id of “document”. We’ll reference this textarea by that id in the javascript (see below). Second, the button will call the javascript function submitIt.


/*
function submitIt() {
//document.getElementById("plop").innerHTML = "THIS WILL GET REPLACED";
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("C:\\PathToABatFile.bat");
var x = document.getElementById("document").value;
oExec.StdIn.Writeline(x + "\n");
var input = "";
var error = "";
var line = "";
var start = false;
while (!oExec.StdOut.AtEndOfStream) {
line = oExec.StdOut.ReadLine();
if (!start) {
if (line.indexOf("##Start##") > -1) {
start = true;
}
} else {
input += line;
}
}
document.getElementById("processed").innerHTML = input;
return false;
}
*/

The javascript calls an ActiveXObject which is then used to execute our .BAT file. This .BAT file, in turn, calls our jar file, passing it a few parameters it needs. As you’ll see, I use Writeline to push the value of the textarea (remember, it’s called “document”) to StdIn.

In my java class, I have a void main method that accepts some parameters. Then it builds a string from StdIn.


StringBuilder document = new StringBuilder();
//********Take Input from StdIn:
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null && s.length() != 0) {
document.append(s);
}
} catch (IOException e) {
e.printStackTrace();
}

When processing is done, I simply output formatted HTML using System.out.println.


outStringBuilder.append("Some Text: " + "Some Other Text" + "
");

A stylesheet embedded in the html page provides style information for the returned content. Note the use of ‘##Start##’. My class uses other libraries that output to Stdout at the beginning of execution. So, this just gives me a way to ignore everything up until I hit this marker. Everything after that is appended to input, and then pushed to the DOM.

…and that is it… Good luck!

Advertisements

About jeffmershon

Director of Program Management at SiriusXM.
This entry was posted in Software and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s