This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm not sure if this is normal behavior, so I'm kindof embarrassed to ask.
I have a jsp with a dropdown that gets populated from the db.
When I go directly to the jsp via typing in the address or clicking the link, the data doesn't load. The service layer never gets called. My list/dropdown is empty.
But if I use another servlet that loads the bean (and thus this list) into the session, it works fine. They're not even named the same thing, so it's not like I'm getting the other reference out of the session.
I have the loading in the list inside the controller class in the constructor -- and the controller gets instantiated inside the init() method of the servlet.
I feel like I'm missing something here.
When you do things right, people won't be sure you've done anything at all.
I know you have limitations because this is a class assignment, but for data that doesn't change, I'd read it in a context listener and store it in application scope. That way, once the web app is loaded, the data is readily available to all resources without any further headaches.
I avoid static at all costs. In a web app, the scopes are the best place to store data.
I don't know what a context listener is, but it sounds like some guy who listens for the app to load and pops all the prerequisite stuff into the scope.
This is data that changes. It's an inventory list. It gets a new object added to it every time something gets added to inventory. I actually need this same list for two screens. Once to view the inventory and once to edit a member of the inventory. I can't see the list until I "add" a member to my inventory. I think there's an order of operation thing I'm missing.
I'm a tad concerned about what's going on under the covers to obtain the list. You've used words like "static" and "constructor" that don't quite jive to me.
They don't jive because I have no idea what I'm doing. :)
I started out trying to get a List the same way I got a single object, then iterate through the list and move on with my life. When I visited the page, it threw exceptions about types and couldn't find java.util.List so I assumed I did it wrong.
So I stopped trying to put the list into the session and instead return a bean that had the list on it and just use the getter to get the list. The list is a list of "all of this kind of bean" so I thought it best to make it static. That's where I was last night and you told me static getters dont get along with the bean spec.
The list was always empty.
So I thought I wasn't ever accessing the service layer, so I tested another function, and in doing so, the list was becoming populated for my broken and sad dropdown, because I have a controller that when it gets instantiated I used it to populate that list for me, and, well, it being static, when I useBean, I get a reference to the list.... even if it's a bean I've never touched.
I understand this probably isn't good. That's why I'm here. Looking back on the last 3 weeks of struggling, I should have asked for help sooner.
The first function we created (which I modeled the rest on), we created a plain html/jsp file with a plain form and called the post on a servlet and it landed me onto the jsp I wanted. Talking about this is making me realize this is where I'm confused. I started off by putting everything inside META-INF/jsp because I thought that was good, but I couldn't access stuff right so I moved everything into the root of my app. It worked, so I thought I did good.
I don't know how to go to a plain jsp file and get the data from the database, then use that same jsp to submit a post request to the servlet. Or even if that's what I want. I feel like if I had a plain page and the user typed in a name I could retrieve that just like on the login.... but the spec requires a dropdown. And I've come so far on this that I don't want to bail when it works half the time. And I understand it only works because I hacked it to work.