Say I have a JSP that has a form that gets submitted, a controller servlet that handles the request and forwards it on to another servlet to handle the logic. This servlet can read from the database (or probably use a class to handle the database logic more likely), but it would also populate a bean that would be used by the next JSP to use in order for its display. If I understand what I have been reading about MVC architecture is that the last bean used for the display should be a page bean. Doesn't a page been only have scope within the page? How could I populate it in the servlet and pass that to the last JSP as a page been. Wouldn't I have to put it in the session to really make it available to the last JSP page? Thanks for the help- I'm really trying to understand this design pattern! Brian
I'm slightly puzzled why you have two servlets in this design. I'd have one or more JSPs as pure "view" elements, a servlet as a controller and regular java classes and beans to populate and represent the data. The reasoning by suggesting a page bean is that the bean itself is responsible for populating its internal state from the database or whatever, but that the data it holds only represents one page's worth of information - the result of one query for example. This bean would make no sense on another page, or even on another invocation of the same page, so it has page scope. Does that help?
One servlet makes sense. If the servlet instantiates the class/bean, then the fields get instantiates, wouldn't using that bean in the JSP page with a page scope create a new instance of the bean so you wouldn't have your data? That's where I was wondering if you would have to put the bean in the session and then use session scope in your JSP page so that you have the same bean and thus the same information. Does that make sense? Or I am confusing the concept of the design? thanks Brian
Brain, Which scope we want to use depends on how long you want the data to be persistant. If the user wants a search result, by entering some search criteria and invokes a servlet, I think it is good idea to pass the resultBean in 'request' scope like request.setAttribute("results",resultBean); On the other hand it makes more sense for a LoginServlet to keep the logged user info persistant throughout that particular user's session. So the LoginServlet may code session.putValue("userView",userViewBean); regds maha anna
Joined: Nov 02, 2000
How would a page bean then be used for display? If it is instantiated when it is used on the page, how would it have any information then since it was just instantiated? Sorry I'm so confused on this. Brian
Joined: Jan 07, 1999
Imagine a very simple bean which represents a canned query, say "SELECT name, status FROM data". Whenever one of these beans is created it runs the query and populates its data, which may then be used by a JSP using standard "beans" methods. So including one in a page causes it to be instantiated, which in turn causes it to load its data. But you don't wan't to keep that data longer than a page, because it will probably have changed by the next time the same page is displayed.