Hi guys. I have a question about using request.setAttribute and request.getAttribute. Say for example I have an online book store with different categories (drama,sci-fi,etc). If the user clicks on a particular category, the servlet will fetch all the records that match the category_name, will create a bean with all the book properties and put this bean in a vector. In other words, a vector full of beans. the vector will be placed in the request in this way: request.setAttribute("list",vectorResults) Now, a list with all the books under that category will be displayed to the user, with a link to a specific book, example:
Now, displayProduct.jsp shows in detail all the book details. It should be pretty straightforward because I have all the information in my vector, which is full of beans. But the problem is when I try to access my vector in displayProduct.jsp I get that the vector is Null. Why? is it because I put it in the request? I should be able to look in the vector for the itemId and display it in the jsp page, no need to involve database again. what is wrong??? thanks
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Joined: Jun 13, 2002
Once the list is displayed to the user, the earlier request and response are lost. The link to DisplayProduct.jsp is a fresh request. You have following options, 1) Either make a fresh call to the database for the selected item from the list 2)Put the vector in session (This look good, but might not be as good a solution as it looks. Specially if your vector is huge. Also you need to take care of the "attribute name" you use in the session.setAttribute() so that there are no clashes. 3) You could dump all the details of the item(if they are not a lot) on the html. Use java script to popup of the details instead of a jsp. 4)You could have a very scalable solution with EJB's if you need one. hope this helps
Joined: Nov 27, 2001
what about putting the list in the servlet context. I don't know what the consequences of this are, but it's working smoothly. Besides, I don't expect to have thousands of books available.
Why make it specific to a web application at all? Design and build a separate 'book list handler thingy' and make use of it in the web app. If you design it using the application context, you will only be able to use it in servlet containers. If you design it separately then you can use it in stand-alone apps or whatever. Dave
Joined: Nov 04, 2001
hi agree to both the suggested solutions but if i try to address the question "as it is" then, i'd ask "how u r directing the request to the displayProduct.jsp page?" if u use response.sendRedirect() u will obviously get Vector NULL. if u r using forward() or include() then i would have to have a look at the problem more seriously... sendRedirect() purges the OLD request object and makes a new request. it keeps the response object as it is as it is redirecting the response.. regards maulin
As soon as the servlet filters all the products from the database that match a specific category, I use forward to listProducts.jsp. This page simply makes use of the vector full of beans to display all the books available with their specific link, as I posted in the beginning.
but as seshu mentioned, it is indeed creating a new request when the user wants to see more details about the book and clicks on one of the links available. So I guess this is the reason why I get a null vector. After doing some research for a while, there is also a possibility of storing this vector in the session... hhhmmmm... but I just don't know what design is the best, putting it in a session or putting it in the servlet context... . Or probably neither of these approaches are the best!
Design and build a separate 'book list handler thingy' and make use of it in the web app. If you design it using the application context, you will only be able to use it in servlet containers. If you design it separately then you can use it in stand-alone apps or whatever.
This is even more interesting, but can you explain to me a bit more in detail?? thanks
Joined: Mar 22, 2002
but I just don't know what design is the best, putting it in a session or putting it in the servlet context...
Better to put it in an HttpSession object, so that each user will have his own "shopping cart" of books. Putting the data in the ServletContext while servicing concurrent requests from different clients is asking for trouble.