views:

29

answers:

2

This is an example of a type of problem I have been having.

When a user comes to the home page of my app there are two possibilities, he/she is:

  1. logged in
  2. not logged in

If the user is logged in I want to display the username in the top right and a link which says "logout". If the user is not logged in I want to display a link which says "login or signup".

Previously I would have called some code from the JSP which would return an appropriate piece of HTML that I would stick into my page. But this is probably not the best thing to do, although it is simple.

Now I believe I should send the initial request for the homepage to a servlet (instead of jsp), which will instantiate some object of type "HomePage" which has subclasses "LoggedInHomePage" and "NotLoggedInHomePage" depending on whether a user id is available in the session variable. I will have to determine the correct constructor with an if or case statement.

So far it seems neater. But at this point it get's messy again, because I now need to know whether or not I have userId being supplied in my "Request object" for which I need to use some if statements, or I use a different jsp template which implies having two almost identical jsps sitting on the server for every page.

Neither seems elegant. Help most appreciated. If you have a neat solution to this problem please just ignore my description and lay that out, I don't mind throwing away whatever ideas I have presented here.

+1  A: 

One possible solution:

Add a method prototype to HomePage. Call it getNavigationArea() or getNavigationLinks() (say). This method will return say a set of Link objects that can be rendered using suitable HTML.

Both LoggedInHomePage and NotLoggedInHomePage will override this method. The implementation of the former will supply a Link instance that says "logout" while the latter will supply an instance that says "sign up/register".

The JSP will render the navigation area without knowing what its contents are. This way you will only have one JSP page irrespective of user status. Also you can add other type of users, say RegularUser, AdminUser etc. with different navigation options without having to add new JSP pages.

Manoj Govindan
Thanks the one change I will make is that I will return a list of Link objects, since the issue is in one case I have a single link (signup) and in the other case I have two links (username, logout). Thanks.
Ankur
+3  A: 

I would simply create a single home page, with a login component whose responsibility is to display the current status and to perform the necessary authentication when logging in.

This is a 'has a' relationship rather than a 'is a'

Mitch Wheat
Yeah I agree that it's "has a" not "is a" .. will make things attributes rather than subclasses.
Ankur
Is this similar to having a header / footer / left / right area that is a jsp in itself and then making it handle the login logic. All the pages are just composed with this login JSP ?
Icarus
This is the best choice of those provided, I think. Simple and elegant.
EricBoersma