• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

returning an array from servlet responce

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And don't forget to cast the returned value of getAttribute
 
varungoyal goyal
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can u demonstarte it by ...smal scriplet ...thanx
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

===============================================================
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic