• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting ArrayList to the session

 
Brooke Maddox
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble understanding how my ArrayList works. I have a method that reads from a text file and puts the contents into an ArrayList<Movie> the Class returns the ArrayList. I have called the method in my Servlet and set the returned ArrayList to another ArrayList<Movie>. I have then set this ArrayList to the session, and used c:forEach to retrieve it in my JSP. What I have as of right now works, my problem is that it is returning the entire ArrayList to my JSP. Do I need to get the contents of the ArrayList and set them to my object in the servlet and then set the object to the session?
GetMovie():


Servlet:

JSP:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand what you are asking.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that all looks fine to me. However you haven't shown the code which puts the ArrayList reference into the session, so I can't comment on that. But otherwise, yeah, you get the list from the session and you do <c:forEach> on it to get each of its elements.
 
Brooke Maddox
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need the jsp page to only display movie.title and then use the movie's id # to get more information about the movie. So my text file has something like
1 Say Anything Drama 5.95 10
2 Water for Elephants Drama 14.95 10
3 Duplicity Comedy 8.95 10

I would like my JSP page to display a list of the movies such as
Say Anything
Water For Elephants
Duplicity
The user should be able to choose a movie from the list and upon hitting the submit button see the rest of the details about the movie they choose. For example if the user choose Say Anything from the list
the next view would display a table
Movie ID: 2
Title: Water for Elephants
Genre: Drama
Price: 5.95
Qty: 10

As of right now my the c:for each statement in myfirst jsp page is displaying the entire contents of the ArrayList such as:
1 Say Anything Drama 5.95 10
2 Water for Elephants Drama 14.95 10
3 Duplicity Comedy 8.95 10

How can I get it to display only the movie Titles.
I noticed my servlet code wasn't added
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, now I understand the question. That's what we call "Garbage In, Garbage Out" in the computer biz. In other words, if you don't want all of that stuff to be output as the title, then don't put it all into the title in the first place. That would involve parsing the string into its component parts, something which you aren't doing in your Movie class. Here's the line you need to fix:



So instead of using the entire line of text for all five attributes of the Movie class, you'll need some code which breaks "line" into its parts and uses those parts as the parameters to the Movie constructor.
 
Ayan mallick
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: string split can come handy in such situation. use delimiter.


Regards

Ayan
 
Brooke Maddox
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I thought I figured out how to parse the string into its component parts but then something happened and now I keep getting this Exception. Not sure what I changed that caused this but I can't figure out what it is.

I keep getting java.lang.ArrayIndexOutOfBoundsException: 1
Here is my new while loop

 
Ayan mallick
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


just check your array length.it is less than 5 that's why you are getting this exception.may be you could print the array elements for testing then create movie object.
could you please show your text file content it would be easier for us to detect.


Regards

Ayan
 
Brooke Maddox
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the contents of my text file
1 Say Anything Drama 5.95 10
2 Water for Elephants Drama 6.95 6
3 Duplicity Comedy 8.95 10
4 Bad Teacher Comedy 6.95 5
5 Finding Nemo Family 10.95 9

 
Ayan mallick
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let's say movie constructor(serialno, name, category, rating, price)
so you need to get five fragment out of every single line in order to create every Movie instance.
so instead [\t] use delim as [\\,] because i am getting some problem with [\t] if any word having 't' in it it's not escaping that also.you might have a look into here.
1,Say Anything,Drama, 5.95,10

now you could use it as you were doing.


Regards

Ayan
 
Brooke Maddox
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out, thanks for your help. My text file had a line break between lines.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic