This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Database stores items in a ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Database stores items in a ArrayList" Watch "Database stores items in a ArrayList" New topic
Author

Database stores items in a ArrayList

Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
Can anyone please help me I am new to this and am having such a hard time. I have a database that I need to retrieve the names of my items and display them in a JSP page. I am using a Servlet to connect to the database. I created an Inventory Bean and I thought I could use an ArrayList in the servelt to get the items and then use this ArrayList to display them in a JSP. When I run my program the list of names in my JSP page is blank.


Here is my JSP Page


I have been working on this for days and am getting no where. Any help would be greatly appreciated. Thanks
Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 104

In your JSP you have:



While in your servlet you have put the data as a session attribute with name "items". In the for each tag the items attribute contains the source of your data. In this case you simply use ${items}. The var attribute will contain each item read. In this case it will be the type of InventoryBean. To get the value out you use the variable name "item". So I think you for each should be:



Website: Learn Java by Examples
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

In your JSP you started out with the old-fashioned, obsolete "jsp:useBean" way to access data from the session. And then in the middle you switch to the modern JSTL way. The part at the beginning is useless, since the JSTL code doesn't use it, so you should just get rid of that. However the JSTL code also won't work, because you haven't declared the JSTL namespace correctly. So you should fix that.

Also in your servlet it would be more usual to put the data in request scope, rather than in session scope, and to forward to the JSP rather than redirecting to it. Is there a reason you didn't do that?
Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
Thanks Paul,

Like I said I am very new at this and I exactly know what I am doing yet. That would be the reason why I used redirect over forward.
Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
Okay so I changed my scope from session to request, and I added the new JSTL library tag. I also changed from redirect to forward, but its still not working. One other question, should my bean be a request scope as well. I am going to need to access this array in another JSP page will that change my scope? Any other suggestions?
Thanks Brooke
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If your servlet forwards to the JSP, then both of them are in the same request. (The request is "forwarded", that's why it's said that way.) So if you have data which the servlet produced, then putting it in request scope is sufficient for the JSP to access that data. However that's the end of the request and the data can't be accessed from request scope after that.

So if you need to save that data for future requests to be used in future JSPs then yes, it should go into session scope. Data put into session scope lasts until your session expires, which is essentially forever.

Once you have made changes based on that, see if you're getting any data in the browser. What we've been talking about here doesn't have anything to do with JDBC, it's more about JSPs. But you're testing several different things all at once here, which is a tricky thing to deal with, so you have to go carefully. First it's possible that your JDBC code isn't finding any data, or maybe it's throwing an exception. The code looks pretty much okay to me but it could still have problems. And if it does work and forward to your JSP correctly, then you should at least see "Item Catalog" in your browser.

Anyway I don't want to go into all the possible things which might be wrong, you should run your latest version and see what happens. Post the code if you're still having problems and of course let us know what those problems are.
Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
I think that my connection to my Database is good, because I tried just printing off all the rows in my table and that worked.
I do see the Item Catalog in my browser it just that I am not getting any of the objects displayed in my


Here is my updated code:
Servlet


JSP:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8



Well, first of all you should call your "var" in the c:forEach by a different name -- otherwise ${items} is ambiguous and you don't know which the two possibilities the JSP compiler will choose. (And neither do I.) I would suggest var="item".

And second, you should check your spelling -- you have a typo where you try to use your JSP variable in your <option> tag.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database stores items in a ArrayList
 
Similar Threads
page gets hang on runtime
Quick Search Results page cant put multiple results into a table
exception while running code
not selecting values from database
not fetching values