How to run a shell from JSP? How to run a shell from JSP? shell shell

How to run a shell from JSP?


You can execute arbitrary processes via the Runtime class:

Runtime.getRuntime().exec("...a string here specifying the command to run...");

There are several overloads depending on how much control you need, etc. You'll need to be sure that the user account your JSPs use has access to everything necessary for the command you're running.


Solved the problem.Took me some time, 6 hrs. to be exact, but got thedesired result.

Here's how:

  1. To call the procedure that generates the util, I first call a jsp(usingwindow.open with parameters, target lower_frame). There is use CallableStatement and call the proc. with the params sent. The syntax is:

    Callable Statement cs = null;cs = mk.con.prepareCall( "{call DPD_PRINT_LETTER(?,?,?,?)}" );cs.setString......//4 params.cs.execute();

    Then using BufferedReader, I call the util and produce it on thelower_frame. Syntax:

     %BufferedReader bfreader = new BufferedReader(newFileReader("/ltora/oracle64/j2ee/LtTest/LtTest/reports/MNOS_LETT_"+mrno+"."+IPaddr));String data = new String("");while((data = (String)bfreader.readLine())!=null{           String temp=data.replaceAll(" ","&nbsp");% tr      td style="font-family: Courier New; font-size: 12pt; font-weight:bold"           %= temp %      /td      /tr  % } % 
  2. Now to call a shell that's gonna print the report, I call ajava(again.with params) using ActiveXObject(say x) and the function:

    x.open('GET',url,true);

    Here URL contains the java. In the java, I did:

    Process child = null;Runtime anyRuntime = Runtime.getRuntime();child = anyRuntime.exec(strShellLine);

    Here "strShellLine" contains the shell URL......and that's all there is to it.


There is a misconception here. JSP is a view technology which provides a template to write HTML/CSS/JS in and provides the ability to interact with backend Java code in flavor of taglibs and EL during the generating of the response. It can however be done with normal Java code. You could write that in a JSP file, but it's considered bad practice. Java code belongs in real Java classes. Your problem is actually to be split in 2 parts.

First the question how to execute the desired task using Java code. As answered several times before you need Runtime#exec() for this. However, be sure that you've read and understood all the 4 pages of this article! Here's a kickoff example:

public class YourShellClass {    public Result execute() {        Runtime runtime = Runtime.getRuntime();        // ... implement        return result;    }}

Now left behind the question how to invoke it using a HTML form which is served by a JSP page. Let's assume that the HTML form look like this:

<form action="run" method="post">    <input type="submit" value="run"></form>

This form will send a POST request to http://example.com/somecontext/run. To exectue Java code whenever this request arrives at your server, just create a class which extends HttpServlet which listens on an url-pattern of /run and has the doPost() roughly implemented as follows:

protected void doPost(HttpServletRequest request, HttpServletResposne response) throws ServletException, IOException {    YourShellClass yourShellClass = new YourShellClass();    Result result = yourShellClass.execute();    // ... do something with it?    // Then display some result page?    request.getRequestDispatcher("result.jsp").forward(request, response);}