Win a copy of Mastering Non-Functional Requirements this week in the Design forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Global variable through the Servlets  RSS feed

 
Isaac Ferguson
Ranch Hand
Posts: 1056
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I would like to add a value to a list :



Where is the best place to put this code:



At the moment it is located in the Servlet itself, but it doesnt work properly obviously. And the value of the LinkedList stored on it is lost every time a call the servlet and a new object is created.

Any idea, please?

Regards, Isaac

 
Dave Tolls
Ranch Hand
Posts: 2650
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of this list?
 
Isaac Ferguson
Ranch Hand
Posts: 1056
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose is that every time an user from a combox in the view, choose a value it does into an array which is persisted in the session...
 
Isaac Ferguson
Ranch Hand
Posts: 1056
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant : It goes
 
Dave Tolls
Ranch Hand
Posts: 2650
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's for the session then add it to the session using the setAttribute and getAttribute calls.
 
Isaac Ferguson
Ranch Hand
Posts: 1056
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is like this :

 
Tim Moores
Saloon Keeper
Posts: 3648
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't create a new list no matter what - you should first check whether one already exists in the session, and if there is one, use that.

I don't understand what you're trying to achieve by also storing it as a context attribute, so I can't comment on that.
(Update: I see you removed that part from the code now.)

One more thing: Your use of a "processRequest" method leads me to believe that you're calling this method for both GET and POST - since it changes state, this code shouln't be run upon a GET request - so you should name the method "doPost" instead.
 
Isaac Ferguson
Ranch Hand
Posts: 1056
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dave Tolls
Ranch Hand
Posts: 2650
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Isaac Ferguson
Ranch Hand
Posts: 1056
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes


In fact what I mean is where should I create it the first time, so else the variable "listProductNotLogged" just doesnt exists. When the user from the .jsp, sends his option it should not be created again in the servlet.
 
Isaac Ferguson
Ranch Hand
Posts: 1056
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now it works. I made it like this:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!