• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning an array from servlet responce

 
varungoyal goyal
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't forget to cast the returned value of getAttribute
 
varungoyal goyal
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u demonstarte it by ...smal scriplet ...thanx
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic