I have a couple of JSP's that I will be including on several pages via the jsp:include tag. Both of these pages depends on the user being logged in, in order for them to be visible. So my question is should the logic of whether to include these pages be in the page that is doing the include, ie: MAJOR PSUEDO CODE HERE if user logged in include page endif or should the logic be in the pages to be included. ie if user logged in <table> ..... </table> endif Where the entire content of the page is in the logic block? Thanks.
Assuming that the parent page does not require login (since I'd say that you should never get to the includes if the including page requires authentication) I'd say.... it depends. What a useless answer! Sort of like "cook it until it's done" Seriously, here's the criteria I'd use to come to the decision: If the sub-pages can be included in other places (I'm assuming that they are includes because they are modular) where login is not required, then the logged-in-or-not decision is clearly with the parent page since that's where the decision-making context lies. If these sub-pages must always be authorized, it'd be safest to put the logic in the includes since they might accidentally get included in a parent page that didn't enforce the "rules". But my own personal choice would probably be to abstract the include into custom tags so that all decision making isn't done on-page at all. (But I know not everyone is comfortable with whipping out tags). Does this help? bear
Thanks Bear. Well, I am using STRUTS logic tag to determine whether or not to display the contents of the page. Is this similar to what you are suggesting along the lines of: But my own personal choice would probably be to abstract the include into custom tags so that all decision making isn't done on-page at all. Or is your custome tag doing a bit more?
Similar, expect that 'rolling your own' can give you a higher level of abstraction. For example:
would be more like
which would do the appropriate thing depending upon context. The distinction is that from the page-point-of-view, the do-I-include-it-or-not decision is completely abstracted away. I tend to prefer the custom tag route -- but I'm at the point where I can whip out tags pretty fast. Custom tag writing is a great skill to have under your belt, but the learning curve can be a bit daunting. (Though should get easier in JSP2). Nothing at all wrong with your approach either, imho. Just depends on where you are more comfortable putting the logic. bear
Not necessarily. But building up markup in Java is always rather messy. Under JSP 1.2 it was somehwat unavoidable (though I did write up a little utility that helped). Under JSP 2.0, tag files can take a good deal of that pain away.