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:
- logged in
- 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.