• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to update an array list inside a servlet?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey fellas, I have a bean called student with three private fields (name, grades and ID).  Inside the servlet I get the three parameters through request.getParameters (from a HTML page)  I put these parameters inside the student bean Student x = new Student (userName, userID, userGrade);  how I have to this serialized bean into an arraylist so the user can keep entering data and the array list can keep growing. how would i go about this?
 
Author and ninkuma
Marshal
Posts: 66789
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store it in the session.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,

First I would like to make sure if my understanding is right. I interpret that you want to preserve all the user details in some data structure. If I am wrong, please correct me.

Solution:


 
Sheriff
Posts: 21424
94
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maaz Ahmed wrote:If I am wrong, please correct me.


Well, here are some issues I found quite quickly:
You can't declare a List like that. You mean List<String>. You also don't need to create a new one if you're going to read its value from the session a bit later on.
That will only return true if the session contains an attribute that is not the String literal "". If it's an empty String that has been created in a different way, that still returns true. I don't really get what you're trying to do here. If you want to test for absence of the attribute, use null instead of an empty String.
That line will not compile, because HttpSession.getAttribute returns Object. You will need a cast to List<String>.

A quick improvement of your code:
 
Rancher
Posts: 3601
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:You mean List<String>.



List<StudentBean> I would have thought (or whatever this student bean class is called)?
 
Rob Spoor
Sheriff
Posts: 21424
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, I don't know why I used String.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!