My question is self explanatory. I want to run a shell, that's 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's 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 don't know the syntax to run shell(.sh) from JSP.
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: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:
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 whichextends HttpServlet
which listens on anurl-pattern
of/run
and has thedoPost()
roughly implemented as follows:You can execute arbitrary processes via the
Runtime
class: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 the desired result.
Here's how:
To call the procedure that generates the util, I first call a jsp(using window.open with parameters, target lower_frame). There is use Callable Statement and call the proc. with the params sent. The syntax is:
Then using BufferedReader, I call the util and produce it on the lower_frame. Syntax:
Now to call a shell that's gonna print the report, I call a
java(again.with params)
usingActiveXObject(say x)
and the function:Here URL contains the java. In the java, I did:
Here "strShellLine" contains the shell URL......and that's all there is to it.