This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing an ArrayList between servlets

 
A. Ghosh
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
A. Ghosh
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
A. Ghosh
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic