Win a copy of Kubernetes in Action this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 66352
155
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: 21187
87
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:
 
Ranch Foreman
Posts: 3141
37
  • 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: 21187
87
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!