I am having an issue with creating some JSP pages. I have a few pages (Home, About Us, Contact Us) done up in JSP, and I also have the ability to login to this site using a username and password.
What I want to achieve is to allow visitors to access the About Us and Contact Us page without logging in. Yet if the visitor has logged in, I want to print a simple welcome message in About Us and Contact Us that includes his username.
I am implementing the MVC architecture, so I already have the controller and model done. The code I have for the About Us page is:
<jsp:usebean id="user" type="UserBean" scope="session"/>
...other code...
Welcome, <jsp:getproperty name="user" property="username"/>
This displays fine if the user has already logged in, however it throws an exception if I access this page directly without logging in. I can fix the problem by changing the usebean line to:
<jsp:usebean id="user" class="UserBean"/>
But this doesn't seem in line with MVC, which says that I shouldn't let JSP pages create classes.
Is there any way to conditionally use the bean only if it exists? Or is this implementation wrong, and there is an alternative?