tags:

views:

260

answers:

4

Hey guys, my question is self explanatory. I want to run a shell,thats calling a procedure, from a jsp using a button. The procedure is


CREATE OR REPLACE PROCEDURE DEMO_PRC (dist IN variable,mrno IN variable,
yr IN variable,flags OUT number)
IS
begin
flags:=0;
// CODE THAT GENERATES A UTIL.....
flags:=1;
end;
/

And the shell is:


sqlplus demo_user/123456@demo
var a NUMBER(4);
exec DEMO_PRC($1,$2,,$3,:a);
print a;

Named it dist.sh and it is run in command line as:

dist.sh 3 1937 10

But i want to run it from a jsp when i click a button.But i dunno the syntax to run shell(.sh) fom jsp.

Any help will be appreciated.

Thank you

+1  A: 

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.

T.J. Crowder
+1  A: 

Have a look here http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec

Anand
+1  A: 

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);
}
BalusC
besides, instead of using a shell script, I think he could do the SQL operations he wants directly from Java.
Valentin Rocher
Not all JDBC drivers supports all kinds of DDL statements.
BalusC
A: 
anand