Running Server-Based Applications


Once the input values are extracted and converted into their appropriate forms, the servlet is ready to run the requested application. A servlet will run two basic types of applications ”those written in Java and those written in some other language. The simplest case is when the servlet runs a Java application. The application invocation is simply incorporated into the servlet code listing. In the case of the atmosphere modeling tool, the servlet creates a USatm76 object with the appropriate input parameters.

The situation becomes somewhat more complicated if the application is legacy code written in a language such as Fortran, C, or C++. The application needs to be one that can be run from the command line because Java has the ability to execute command line instructions. Executing an application from the command line is accomplished by using instances of the Runtime and the Process classes. Runtime defines the exec () method that executes a command line instruction. The exec() method returns a Process object that is used to suspend further activity until the command being executed by the exec() method is finished. For example, to run an application named MyApplication , you could use this syntax.

 Runtime r = Runtime.getRuntime(); Process p = null; String str = "MyApplication"; try {   p = r.exec(str);   p.waitFor(); } catch (Exception e) {   System.out.println("error "+e); } 

The Runtime object is acquired by calling the static getRuntime() method. The argument to the exec() method is a String containing the command line instruction to be executed. The waitFor() method blocks further activity until the instruction finishes its execution. The exec() and waitFor() methods are placed in a try block because either of them can cause an IOException if something unexpected happens during their execution.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net