aspose file tools*
The moose likes JSP and the fly likes Where should the logic be? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Where should the logic be?" Watch "Where should the logic be?" New topic
Author

Where should the logic be?

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.


GenRocket - A Test Data Generation Platform
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Man, I like your approach much better. Of couse, I have 0 experience writing custom tags. I should probably look into that. Thanks for the info Bear.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

No prob. Anything to promote better living through abstraction!

bear
Scott Maclary
Ranch Hand

Joined: May 11, 2005
Posts: 34
Bear,

I know the post is old...but I have a question. You wrote:

... 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 like abstracting things ...but then where is the jsp to include this page? Do you actually write out the html/jsp from within your custom tags using out.print statements? That doesn't seem right...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Originally posted by Scott Maclary:
but then where is the jsp to include this page?


Theere is no JSP code involved. The custom tag code would include the appropriate file(s) using the RequestDisptacher.include() method.
Scott Maclary
Ranch Hand

Joined: May 11, 2005
Posts: 34
So is it considered bad practice to include HTML in your custom tag?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where should the logic be?
 
Similar Threads
redirecting from jsp
how do i find the referring page in struts
javascript called within JSP
Failed to display login link once user has logged out
Returning A List Of Variables From A Folder Of Documents And Returning Them Into A New Document