aspose file tools*
The moose likes Servlets and the fly likes Passing an ArrayList between servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing an ArrayList between servlets" Watch "Passing an ArrayList between servlets" New topic
Author

Passing an ArrayList between servlets

A. Ghosh
Greenhorn

Joined: Dec 12, 2008
Posts: 3
I am new to Servlet Programming.

I have a bookstore application.

I have an arraylist I want to pass from one servlet to another.

I have the ArrayList bookList in OperationServlet.

I keep adding books by a particular user this ArrayList.

I create a HTML page as an output of OperationServlet, with a hyperlink that
calls "/SelectionServlet"

SelectionServlet should display the list of the books selected by a particular user in OperationServlet.

Now I don't want to use HttpSession object to pass data.

What I do is save the username in a cookie on the client-side.

And save the ArrayList in the ServletContext.

Now in the SelectionServlet I read the result as:-

ArrayList<Float> bookList = (ArrayList <Float> ()) myContext.getAttribute("array");

It works fine if I use only one browser instance and one user to login.

Now if two different users login simultaneously and select books, then both gets added to the same ArrayList, and the selection of each users is the cumulative selection.

Can anyone please suggest a way to pass the array from one servlet to another. Please note, our entire design avoids HttpSession objects. ( I know that is bad.. but thats the way it is)
[ December 12, 2008: Message edited by: A. Ghosh ]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30776
    
157

Welcome to JavaRanch!

I have the ArrayList bookList in OperationServlet.

This part confuses me. Do you mean that bookList is an instance variable on the servlet? If so, that would mean all users share it. Which might be the case since that is the behavior you are describing.

You really should be using a session for this. The only other idea I can think of is to have a global map or user id --> list that is stored on the server and continue with your user id technique.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
A. Ghosh
Greenhorn

Joined: Dec 12, 2008
Posts: 3
Sorry for the confusion.
The ArrayList is defined in my doPost method:-

ServletContext myContext = getServletContext();

// If the context does not contain anything, create a new ArrayList
if (myContext.getAttribute("array") == null) {
myContext.setAttribute("array", new ArrayList<Float> ());
}

ArrayList<Float> bookList = (ArrayList <Float> ()) myContext.getAttribute("array");
[ December 12, 2008: Message edited by: A. Ghosh ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

If you put the item in the application context (servletContext) it will be shared across all users. That's what it's for! If that's not what you want, don;t do it.

If you want to have the item available across multiple requests, but only for a single user, that's what the session is for.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
A. Ghosh
Greenhorn

Joined: Dec 12, 2008
Posts: 3
Thanks a lot. I guess I have to use a HttpSession object then. Just one more question. Is the servletContext a server side object or client side.

I mean I read somewhere that servletContext is saved in the client's JVM. In that case my problem arises because the two instances are opened from the same JVM (computer I mean).

Will this problem not be visible if the two users login from two different machines. I just want to know this, though I agree that HttpSession is a better idea.
[ December 12, 2008: Message edited by: A. Ghosh ]
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
I mean I read somewhere that servletContext is saved in the client's JVM. In that case my problem arises because the two instances are opened from the same JVM (computer I mean).


What you read is incorrect. The context objects of a Java-based web server only exist in the server's JRE. As with most technical information available on the "web", the editorial process is very loose.
[ December 12, 2008: Message edited by: James Clark ]
 
Don't get me started about those stupid light bulbs.
 
subject: Passing an ArrayList between servlets