How to run a shell from JSP?
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 PROCEDUR开发者_开发知识库E 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.
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 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:
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 the lower_frame. Syntax:
% BufferedReader bfreader = new BufferedReader(new FileReader("/ltora/oracle64/j2ee/LtTest/LtTest/reports/MNOS_LETT_"+mrno+"."+IPaddr)); String data = new String(""); while((data = (String)bfreader.readLine())!=null { String temp=data.replaceAll(" "," "); % tr td style="font-family: Courier New; font-size: 12pt; font-weight: bold" %= temp % /td /tr % } %
Now to call a shell that's gonna print the report, I call a
java(again.with params)
usingActiveXObject(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);
}
精彩评论