tags:

views:

581

answers:

3

Hi,

I have a JSP from which I'm getting the input from the users. Now, I'd like to pass the input that get from this JSP as arguments to the main method of my java program and run the program from this JSP. Some help please!

Thanks in advance! Ravilla

+1  A: 

Bad idea. What are you trying to achieve? Even if you do that, by calling main() as any other arbitrary method, or by invoking Runtime or something, What you will achieve is a program running on server. What the client would do with that.

JSP/Servlet/Java Web programming is a request/response based stuff.

If you want to compute something using that program and planning to use the output as a response. Then why not include that particular piece as business component or something and call that in a usual way. May be in a separate thread, if its a long process, let the user interact with the application and notify user once it is done.

Adeel Ansari
A: 

you need to execute command at OS level.

 Process p = Runtime.getRuntime().exec("java -classpath "..." SomeClassContainingMain ...other arguments);  

        //you need to consume the outputs of the command if output/error is large otherwise the process is going to hang if output/error buffer is full. and create a seperate thead for it (not created here).
  log.debug("PROCESS outputstream : " + p.getInputStream() );
  log.debug("PROCESS errorstream : " + p.getErrorStream());   
 p.waitFor(); // Wait till the process is finished
Ratnesh Maurya
Bad idea. Why not include that particular component as business component or something and call that. May be in a separate thread if needed. -1
Adeel Ansari
if it is anyways needed.
Ratnesh Maurya
Yes it is bad to do that but this answer is good one
Rahul Garg
A: 

I had to program something like this a year ago and I ended up making it a webservice. This allowed my app to be called from a JSP. I think you should try this.

nairdaen