I have a list of strings in the list assetList. How can I send the list in the http response in java servlet ? I am very new to java.
-
Why a HTTP Servlet Response? – Buhake Sindi Sep 23 '14 at 13:41
-
I am doing a GET request and I need to send response. – prashanta Sep 23 '14 at 13:45
-
1You are not providing us any information. What does your code do and what is expected as a response to the client? And what format do you want the response to "appear" to the client? All the technical information to better help your question is appreciated. – Buhake Sindi Sep 23 '14 at 13:47
-
1You can define a contract - format of data that you want to receive and willing to produce in your web method. You can create your own format or just use already existing ones: JSON, XML, etc. - there are tons of libraries that will help you with this. – Alex Sep 23 '14 at 13:48
-
It queries to db and gets a list of names. I have to sent that list in response. I have no idea what is better format to send the list in response. May be plain text.We can send as json as well. – prashanta Sep 23 '14 at 13:49
4 Answers
Call this method after converting your list to a string:
private void writeResponse(HttpServletResponse response, String responseString) {
try {
PrintWriter out = response.getWriter();
out.println(responseString);
out.flush();
response.flushBuffer();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
To convert the list of strings to a string, see: Best way to convert an ArrayList to a string
A list of object is an object. So is the same as adding a object in a response (serialization) and deserializing on the other side.
OutputStream out = response.getOutputStream();
oos = new ObjectOutputStream(out);
oos.writeObject(yourSerializableObject);
More info:

- 1
- 1

- 4,351
- 3
- 37
- 49
If you are free to chose the format of the response, and the response is primarily intended to be processed by a client application, then use JSON. Turn the list of strings into a JSON array (of strings) and send that.
I'd recommend JSON because:
- You are best off with a standard format / serializatio scheme than a non-standard (i.e. custom) one.
- JSON is easy to generate and parse, in a wide variety of programming languages.
- JSON is text based and (relatively) human readable.
There are (of course) lots of alternatives, including language specific ones (Java object serialization), alternatives that are more compact, faster to encode, decode, and so on.
But JSON is a good de-facto choice for typical web-based application protocols.

- 698,415
- 94
- 811
- 1,216
i would suggest you to read more about servlets, JSP and ManagedBeans.
for the beggining its nice to now how these things works, but later you may upgrade and using JSF for Java Web Applications.
back to your question:
the usual way is using Java "Managed" Beans for that!
lets say you send a request to the servlet, the response should be a list of persons:
you create a Bean named Person.java
with id, name, tel, ...etc with getter and setter methods.
then you would make a Controller Class like PersonManager.java
this object may have a method for getting a list of Persons or an emprty list
in your servlet you init these Datas and puting it in the REQUEST Scope for your response
here is an example how to do this in a Servlet:
public class YourServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
Person p = new Person();
p.setName("Mohamad ...");
p.set....
ArrayList phones = new ArrayList();
PhoneNumber ph = new PhoneNumber();
ph.set...;
ph.set...;
al.add(ph);
ph = new PhoneNumber();
ph.set...;
ph.set...;
al.add(ph);
a.setPhoneNumbers(al);
req.setAttribute("person", p);
RequestDispatcher rd = req.getRequestDispatcher("yourResult.jsp");
rd.forward(req, res);
}
}
in your JSP you can then retrieve the results and loop over the list or what ever you would like to do with it!

- 2,486
- 2
- 19
- 30