wood burning stoves 2.0*
The moose likes Servlets and the fly likes returning an array from servlet responce Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "returning an array from servlet responce" Watch "returning an array from servlet responce" New topic
Author

returning an array from servlet responce

varungoyal goyal
Ranch Hand

Joined: May 31, 2006
Posts: 37
how can i return an array from the http servlet response.basically i am making a method wich takes a string as a parameter and hits the servlet URL wich(servlet) returns the array ...how can i do that ..pz tell
bujjib babu
Greenhorn

Joined: May 31, 2006
Posts: 3
Hi Varun,

You can get it easily by using request.setAttribute("Array",value) in the servlet program. and accept that request in another jsp page by using request.getAttribute("Array")..

Ok Bye
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

And don't forget to cast the returned value of getAttribute


[My Blog]
All roads lead to JavaRanch
varungoyal goyal
Ranch Hand

Joined: May 31, 2006
Posts: 37
can u demonstarte it by ...smal scriplet ...thanx
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
i am making a method wich takes a string as a parameter and hits the servlet URL wich(servlet) returns the array

I think the other responders are answering the wrong question.

By "hit the servlet URL" I suspect he means create a URLConnection to the servlet and send the string, then read the servlet response as an array of Strings.
In that case, look at the java.net.URLConnection and HttpURLConnection classes. You might find using the Jakarta Commons HttpClient toolkit to be useful.
Bill
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
You just need to read the object from input stream and type cast it to whatever object has been returned from the servlet.

You need to do something like this.
========================================================
URL url = new URL("servlet_address");
URLConnection con = url.openConnection();

con.setDoInput(true);
con.setDoOutput(true);

con.setUseCaches(false);
con.setDefaultUseCaches(false);

OutputStream out = con.getOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject("what_ever_object_you_want_to_send");

InputStream in = con.getInputStream();
ObjectInputStream objIn = new ObjectInputStream(in);
Object result = objIn.readObject();
objOut.close();
objIn.close();

// result is the object returned by servlet
// typecast it to whatever object has been returned and use it

===============================================================
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Of course, returning an array as a serialised Java object is only good for Java clients... if there's any possibility that a client might be something else (some other thin desktop client, or even a PHP or Perl script), you would have to find some language-neutral way to return an array (which could be either a new binary format, or some string format [e.g. comma- or space-delimited] if it's a string array you're returning).


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: returning an array from servlet responce