JSP reload after login

people! I have one simple website, which has a Home.jsp and inside Home.jsp, there is an iFrame. inside the iframe, the user can choose what he wants to do. Example login. Every other page loads inside the iframe only. The Home.jsp is the iframe holder, along with the header and footer data.

After login, which i have implemented using JSP "session" i redirect to index.html(which opens inside the iframe). In the home.jsp, there is a specific place where it shows "Welcome guest" or "Welcome user_name". The main problem i am facing right now is that after login, everything works as it should, except the "Welcome user_name" doesn't update itself. If i refresh the page, since the session is preserved, it shows it correctly. Then even the logout link works and it automatically reloads into home.jsp.

at the beginning of the home.jsp page, inside my div tags which hold the topNavBarRight i have placed the following code:

    <%if (session.getAttribute("fname") == null){
out.print("Welcome Guest");
}
else{
out.print("Welcome");
%>
<a href="profile.jsp">"<%=session.getAttribute("fname") %> </a>
<a href="logout.jsp">Logout</a>
<%
    if(session.getAttribute("login") == "true"){ //something to reload once or whatever
    session.setAttribute("login", "false"); //so that it doesnt reload again.
    }%>         

Hence, you can see why it works when i reload the page. Now where i am stuck is, how to get it to reload once automatically after index.html is loaded inside the iframe? in my login.jsp i have also saved an attribute called "login" as session.addAttribute("login", "true"); but it doesn't work..

I got two things: 1. Reload Loop using anything such as META, Javascript(window.loc), etc.(placed outside the body of if block) 2. Just doesn't reload, only manual reload...(placed where i have commented out in the code "reload once or whatever").

OH YEAH IF YOU DO SEND REDIRECT TO HOME.JSP(right now its sendRedirect('index.html')) FROM LOGIN.JSP, IT OPENS INSIDE THE IFRAME, is there a way to change the target of sendredirect to browser itself? that would solve it too. because the page that loads inside the iframe like this http://imageshack.com/a/img585/1177/tkyl.png

So what I'm saying is that the welcome guest should become welcome username. I am just unable to figure this one out.. please help! :D thanks in advance.

Answers


Your biggest problem is that in Java for String == compares pointer equivalence, so it only tells you if the two Strings are pointing to the same address in memory. To test that they contain the same value, you need to use .equals():

 if( "true".equals((String)session.getAttribute("login")) ){ 

Also session.getAttribute() returns Object, not String, so you need to type-cast to String when pulling Strings out of it.

The reason I suggest the backwards syntax "true".equals(str) rather than str.equals("true") is that if the variable str is null then str.equals("true") would throw a null pointer exception due to using the dot operator on a null instance, whereas "true".equals(str) would not throw a null pointer exception even when str is null.

As for the iframes, my suggestion would be, Don't use iframes. Learn to style DIVs with scrollbars using CSS and then populate those DIVs either with jsp:include or with Ajax.

And since index.html can't access the session since its not parsed server-side, it would be better to make it a .jsp, since you probably need to check in your index page if the user is logged in or not.


Need Your Help

How to Invoke MessageBox on background thread in VB.NET?

.net vb.net multithreading winforms messagebox

How do I call Invoke to run MessageBox.Show on my background thread please?