I'm making a cd store web app where my index is in jsp. Within this index page I have another jsp page included that is only to contain the categories of music genres which are stored on my database. What I want to do is be able to use the jsp:useBean and have the bean read from the database and return the categories perhaps in a resultset so they can be printed out onto the page. My problem is that I'm unsure as to how to go about this.
in my included JSP page: <% page import="mypackage.categoryBean" %> <jsp:useBean id="cats" class="mypackage.categoryBean" scope="application" /> As for the rest of this file, would I retrieve the result set by using a getter method? for instance: ResultSet r = (ResultSet) cats.getCategories();
As for my categoryBean.java file: I know that the bean can only have a contructor with no arguments, and that each property must have a method to get and set the value. Am I able to use JDBC to connect and query my database for the category titles within one of these methods? Or is there a simpler way or doing this?
I wouldn't use this approach at all. I know it was recommended in another topic that you posted but I think it was not the best advice.
You stated that your app follows an MVC patern, yet this approach is a blatant violation of that premise where you are not only circumventing the use of a controller by trying to perform operations within a JSP (whether or not the processing is done in a bean or not is moot), you intend to pass a result set directly to a JSP page. Not anything near MVC.
Here's the approach I would take:
1) Your index.jsp redirects to the servlet controller for a 'fetch categories' action.
2) The controller calls the model code to retrieve the list of categories.
3) The list of categoreies (not a result set) is forwarded from the controller to a JSP used to show them.
Why would you violate the MVC pattern that the rest of your app is using?
Quite honestly, I'm just trying to get this little milestone out of the way. I was considering doing what you recommended for a while but I need my list of categories to be retrieved if there are any changes. For example, I want admins to be able to login and delete or create new categories with the option to make them live on the website immediately after they apply the changes. Would I be able to do this with your approach? I know it's a violation of the MVC pattern I'm following but like I said before, I'm just trying to get over this little milestone and move on to more important features.
You know what they say about the road to Hades? "Just this once..."
There is no reason that any web app needs to violate the MVC pattern to do what it needs to do. So yes, you can do the types of things that you want to do within this pattern.
It's your app, so do what you feel is best. But I feel that its always best to stick to good design practices even if it's just for that one little sticky milestone one is trying to get by.
If for nothing else than to be consistent. The only thing worse than a poor architecture, is a mix of architectures.
Otherwise, you end up with a mess. [ March 22, 2005: Message edited by: Bear Bibeault ]
Joined: Jan 16, 2005
I agree with what your saying and I want to maintain a solid design pattern. So is making a page that will call a loading servlet the only way? I was also informed that I could make my index a controller servlet which could call the model to do the loading and then forward to a view. Is that acceptable as well?
1. The index.jsp gets the call, redirects it to a ControllerServlet, which redirects it to a view.jsp. or 2. The ControllerServlet gets the call, and redirects it to a view.jsp.
The second one seems less complex to me. The ControllerServlet consults the model, of course.
If you absolutely don't want to create a servlet (maybe because your web hotell supports jsp, but not servlets), you could construct the controller as a jsp file containing only jsp scriptlet code. Be aware of mixing html with too much scriptlet code, it will most likely result in a mess.
I was also informed that I could make my index a controller servlet which could call the model to do the loading and then forward to a view. Is that acceptable as well?
Absolutely. Not all containers support this, but if yours does, simplifying things by making your "start page" the controller itself (avoiding the redirect) would be a good thing. [ March 22, 2005: Message edited by: Bear Bibeault ]