Now, i have changed the logic and put everything that i was doing in htmlUtil class in mainnavigation.jsp.
I want to know which is best approach or there are some alternatives?
Now you ae putting business logic in jsp. This is not better than what you had before.
Depending on your project and how complex and flexible it should be you can adopt different strategies.
You like to dynamically change the side navigation links based on whether there is a user or not or user is an admin. Your login page don't have side navigation.
In the simplest way you can use dynamic includes or static includes to dynamically including the additional links. There could be two separate include files with links. One for regular user and another with extra links for the admin.
If you are not using custom tags, you may have to spit our the call to HtmlUtil as jsp scriplets. For ex:
[include regular user links]
<%
if(HTMLUtil.isUserAdmin(session))
{
%>
[include admin alone links]
<%
}
%>
If you use custom tags you can simply use the custom tags instead of the if statements.
ex:
[include regular user links]
<logic:equal name="User" property="role" value="admin">
[include admin alone links]
</logic:equal>
See how simple it has become. For better maintainability use
[include regular user links]
<logic:equal name="User" property="<%=Constants.ROLE%>" value="<%=Constants.ADMIN%>">
[include admin alone links]
</logic:equal>
Or if you have many pages which need to be fragmented and reused use tiles.