• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to update an array list inside a servlet?

 
Joe Jabroni
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65667
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store it in the session.
 
Maaz Ahmed
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:


 
Rob Spoor
Sheriff
Posts: 20831
68
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:
 
Dave Tolls
Ranch Hand
Posts: 2373
25
  • 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: 20831
68
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.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!