How to pass array in the url of the servlet

Hi Friends,

How can i pass an array in the URL that calls a servlet. For instance,I have this in my servlet rite now:

MyClass obj =new MyClass();

String[] args ={"-opt1","-opt2","file1","file2","file3"};

obj.main(args);

I want that the "args" should come in the URL call,because some php application would be calling this, for instance:

http://localhost:8080/MyProject/MyServlet?args=somearray

MyClass obj =new MyClass();

String[] arg = request.getParameter("args");

obj.main(arg);

Please help..I would really appreciate it.

Thanks

[906 byte] By [java80a] at [2007-11-27 10:31:53]
# 1

You break the array down into individual String values and add to the url using the same parameter name:

http://localhost:8080/MyProject/MyServlet?args=opt1&args=opt2&args=file1&args=file2&args=file3

Then in the called Servlet yoyuretrieve the array using

public java.lang.String[] getParameterValues(java.lang.String name)

likeString[] args = request.getParameterValues("args");

tolmanka at 2007-7-28 18:12:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Sorry,needed to edit this,I am getting some problem,please help if you can:

I call the java class from the servlet as shown above,and it executes fine but only for the first time...after that the tomcat automatically shuts down .....can someone explain me why?

How can i get the execution control back to the servlet ..for example:

System.out.println("Before execution..");

MyClass obj = new MyClass();

String[] arg = request.getParameter("args");

obj.main(arg);

System.out.println("After execution..");

prints only "before execution..."..so can someone please tell me how can i bring the control back to the servlet...

Thanks

Message was edited by:

java80

java80a at 2007-7-28 18:12:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Why are you trying to run a Java application inside of a Servlet?

tolmanka at 2007-7-28 18:12:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

There is some open source application,that we are trying to use and its all in Plain J2SE and runs on command line....

To make it run from a webserver,I made a servlet that can call that java class and pass arguments to it and it works the first time, but for some reason,the execution is not coming back from the java class and Tomcat or JVM crashes.....

I can see there is a System.exit(0); code at the end of that java class,do you think thats causing the system to a halt?

Please help....

java80a at 2007-7-28 18:12:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

If it is open source you should be able to get the source code. Then you can find the entry point and convert it to run as a thread instead of as a Java Application.

The alternative is to use the Runtime.exec method to run it in a seperate JVM.

tolmanka at 2007-7-28 18:12:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Yeah,thats what i was thinking to use the Runtime.exec and fork it and run it under a separate JVM...i wil try that now....

Thanks a lot tolmank...

java80a at 2007-7-28 18:12:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Hi,

When I try to call it using Runtime.exec I get this:

I am getting this error:

Process theProcess = Runtime.getRuntime().exec("java xyz -c -o one.wav two.wav three.wav");

java.lang.NoClassDefFoundError: xyz

My "xyz" class is in the same folder where this servlet class file is....so can someone tell me how to set up the classpath to get that "xyz" file in the path....i thought "java" would execute from the same folder and it would pick up the file as it lies in the same folder...

Any help?

java80a at 2007-7-28 18:12:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Got it resolved,by passing the classpath along i.e -cp ...

Thanks a lot.

java80a at 2007-7-28 18:12:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...