Saving requests from JSP - how?
I'm a big fan of storing application logic in a servlet and keep JSPs as simple as possible. One of the reasons for this is that any good web designer should be able to expand their HTML knowledge to create multiple JSTL tags to do simple iteration, access beans, etc. We also keep more complex / ajax / js components behind a tag library (similar to displayTag, but for our own components).
In most cases, everything works fine. The servlet executes any SQL query, and stores the results in beans for JSP access. If we have a problem where the records we want to get are specified in the project.
The most striking example is the home page - it must be attractive and effective. It doesn't have to be uniform like the rest of the site. There are many one-off or "special cases" here where we want to query for a specific product record or whatever.
What the designer really wants is a way to get the product bean by the product id so that he can access the properties as usual. We obviously don't want to query all products, and I don't want to query in the presentation.
I am sure that I am asking the impossible here and that I must say something. My question is what?
EDIT
Am I wrong in thinking that all application logic must be complete prior to the JSP call? I was under the impression that it was considered best practice to do all the requests / computations / processing in a servlet and then pass (fairly) dumb beans in a (very) dumb JSP.
There are several ways in which the actual complexity of the request can be encapsulated in another class (custom tag or bean) and the JSP can invoke it. This keeps the JSP simple (goal 1), but the JSP still "triggers" the request - quite at the end of the process.
- I did it completely wrong and it's good for it.
- This is a general rule of thumb, but it's okay in this case.
- May I run into problems?
EDIT - Example
I hope this example helps:
The home page is not a "template" like categories / search pages - it is designed specifically to work with a good marketing image and a few specific product images. However, it has information about these two products that need to be retrieved dynamically (so the name and the important price) remain in sync with the db.
The servlet cannot know what products they will be, because if a developer wants to change / remove / add more, he will only need to edit the JSP (and possibly XML as one of the suggested answers).
source to share
If I understand correctly, you have logic in the JSP that wants a certain product, but at the moment you don't want to query from the DB, and it's too late for the servlet to know about it.
(Note, while I respect your will to maintain separation of concerns, the fact that this is a problem clearly shows that your structure has too much logic in the presentation layer ... but since we probably can't fix that .. . moving).
My recommendation is that your designer creates an XML config file that contains all the special cases the frontend needs, and your servlet can read that and then pass the dumb beans back to the JSP.
OR ... you split things up into multiple requests using XMLHTTPRequest and return a servlet for each individual request and then assemble the page on the client.
source to share
It looks like you need to improve the separation between display and database code. You should have separate classes that just interact with the database and don't know anything about the display.
Then you just create a method that will look up the product by id and return its bean so that the display can pull the attributes it needs.
source to share
You need to create a custom bean that will serve your front end requests. In fact, it is probably more like multiple beans to get data for you, in line with what you are talking about here.
There is no problem doing this from a design standpoint; it's just that a particular home page design has more heterogeneous requirements than the rest of your site. Make sure your designer knows that he needs to communicate his needs well to the development team in order to create a BO for your home page (or whatever) and everything should be great.
source to share
You are not mistaken in thinking that all application logic must be complete before rendering the JSP.
If you need to fetch more files to display in your JSP, it will be a different request to the server and a different loop. If you are looking for interactive loading you can use AJAX.
In a single page lifecycle, I have a hard time understanding why you have to make database calls from JSP. Was there a page previously posted with all the required form variables to help you find the data in the Servlet / Helper classes?
If you could give an example of a case it would be helpful.
[Edit] . Look at your example, yes, your designer (or site admin) should set this information as config and not part of the JSP. Or you can have a small application / admin page to maintain information in the database so that it can be changed along the way. When you show your homepage, read the configs and load the relevant data.
source to share