1

I have a question pertaining to the use of the ServletInputStream and ServletOutputStream available in Java Servlets. First I'll give some much needed context:

The assignment I am working on calls on implementing Task Queues in the google app engine. I've been able to get tasks to be added to the app engine and the appropriate workers to be called. However, I am struggling to figure out how to pass an ArrayList<> of serializable objects to the worker's doPost() method. The pervailing method is apparently to use the input and outputstreams of the HTTP request and response objects, respectively, to handle this communication between servlets. I've googled extensively but haven't been able to find a clear example of how to prepare such an arraylist for transmission as an outputstream, adding it to the response of the first servlet, then retrieving it from the request in the second servlet and finally converting it back into an arraylist for use in the code of the doPost() method. So that is basically my question. Due to my inexperience with Java, it is difficult for me to figure it all out by myself and am mostly struggling to wrap my head around it.

To clarify a bit more, I'll post the doPost() method of the worker in question:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
{
    try
    {
        ArrayList<Quote> qs = /*Here the list needs to be read in.*/ null;

        EntityManager manager = EMF.get().createEntityManager();
        CarRentalModel.get().confirmQuotes(qs, manager);
    }
    catch (ReservationException e)
    {
    }       
}

Any help would be greatly appreciated.

Thank you in advance,

Kevin

2
  • 4
    You seem to be rather new to HTTP than to servlets. What exactly is the client? It sounds like as if it's not just a HTML page and that you're trying to develop a web service. Have you ever considered something more reuseable as XML or JSON as input format? With Java serialization you're restricted to Java based clients. Commented Nov 29, 2012 at 14:37
  • Well spotted, I am indeed quite new to HTTP, and it is indeed a very simple web service. However I have to stick to the given methodology, I can only implement it based on the assignment's parameters. However, I will look into your suggestions purely out of educational reasons. Commented Nov 29, 2012 at 20:05

1 Answer 1

3

It's worth to follow BalusC's advice. If you are looking for a simple and quick solution, you can do it with Java's serialization:

In your doPost() method, you can create an ObjectInputStream which reads data from the underlying servlet input stream and deserializes (makes objects out of) the data.

ServletInputStream sis = req.getInputStream();
ObjectInputStream ois = new ObjectInputStream(sis);

ArrayList<Quote> qs = (ArrayList<Quote>) ois.readObject();

You write the object on the other side analogously with an ObjectOutputStream and its writeObject() method. If this doesn't work on spot, try to .flush() or .close() your output stream after finishing your write operations to trigger sending over any remaining buffered data.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, that makes a lot more sense than what google was spewing forth. Thank you very much for the helping hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.