checking if user is login or not

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • game2d
    New Member
    • Apr 2013
    • 59

    #1

    checking if user is login or not

    I have a index page of my website. it will change if user is login or not. I know how to do just by using jsp. but this is not the right way bc I am using java code in html file.

    so here is ex of doing it just by jsp
    index.jsp
    Code:
    <html>
    <body>
    	<%
    		if (session.getAttribute("username") != null) {
    			out.print("
                                <div>
                                         user | option | logout
                                  </div>      
                                ");
    		} else {
    			out.print("
                             <div>
                                  login | forgotpassword | create account
                              </div>");
    		}
    	%>
    </body>
    </html>

    so I want to do the same thing only doing it by the right way(jsp/servlets). I tried look up but cant find any thing.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Where is the problem?
    You know how to do the login page as a a servlet (see your immediate previous post).
    When the user goes through the login page successfully, you just put an attribute into his session, like isLoggedIn=true .
    On every other page, you will check the session for this attribute, and if true, continue on the current page, else redirect to the login page.

    Instead of putting in a isLoggedIn boolean, I used a user-object. If the user-object was not in the session, then I redirected to the login page. This check was done in a base servlet where all other servlets and JSPs were extended from.

    Comment

    • game2d
      New Member
      • Apr 2013
      • 59

      #3
      i c. but wont i run into same problem? ex:

      Code:
      <%
              if (session.getAttribute("username") != null) {
                 isLoggedIn = true;
              }
              else{
                 isLoggedIn = false;
              }
      
      
      
             if(isLoggedIn){
              ....
             }
             else{
             ....
             }
      %>

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Above code only has a local variable isLoggedIn.
        That's not what I mean.
        You will put the isLoggedIn in your session (with session.setAttr ibute(). You will do that in your result page of your login-page when you have verified the password successfully.

        Second, you will not test for isLoggedIn in JSP. As I mentioned above, you will do these checks in a base-servlet.
        There you have the checking code only once. When you derive other pages from this baseServlet (MyActionSevlet extends BaseServlet), the checking will be done for all other pages automatically.
        The check will be done as the first thing when the client request arrives. You progam the check in that way: if session.getAttr ibute("isLogged In") == true, you continue with the action in your derived class, else you redirect to a login page. This action will do all the business stuff and housekeeping (model), and to create a resulting HTML page for the client you call a JSP page (view) at the end.

        I must empahsize on this: just use MVC strictly. (Model-View-Controller). Do not do any business logic (like DB-access) in your JSP. Do it in the servlet. On the other side, do not do any GUI creation (like HTML tables) in your servlet, do it in your JSP.

        Comment

        • game2d
          New Member
          • Apr 2013
          • 59

          #5
          if its not too much to ask can you give a example? Just so I can understand it better.

          Comment

          Working...