Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Logout Help

Options
  • 12-04-2005 3:26pm
    #1
    Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭


    Hopefully someone can help me with this. Kinda Urgent.

    My problem is I'm using Java Servlet's to log into a webpage, getting the information for login from a database. The code for logging in is:
    LoginBean loginBean = (LoginBean) request.getAttribute("loginBean") ;
    
    HttpSession session = request.getSession(true) ; // creates if does not already exist
    String state = (String) session.getAttribute("state") ;
    

    It then searches the database, looking for the login information. If it finds it:
    session.setAttribute("member", member) ;
    session.setAttribute("state", "Registered") ;
    request.setAttribute("message", WELCOME_MEMBER + member.getFirstName() ) ;
    page = messagePage ;
    

    That all works perfectly. The problem comes when I try to log out and log back in:
    request.setAttribute("memberBean", null);
    request.setAttribute("loginBean", null);
    request.setAttribute("state", null);
    HttpSession session = request.getSession() ;
    session.invalidate();
    		
    session = request.getSession(true);
    

    When I click to log back on, I get this error message:
    java.lang.NullPointerException
    	at cp3036proj.command.LoginCommand.execute(LoginCommand.java:30)
    	at cp3036proj.servlets.FrontController.doGet(FrontController.java:70)
    

    I'm using TomCat. This has completely stumped me. I'd really appreciate any help cos I'm in a rush to get this working :(


Comments

  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    What's the actual line that's causing the error ?

    Put System.out.println("Here 1"); etc. in the code, if the line number (30) doesn't correlate to an actual line of code.

    Also - if this line calls a method,what's the method signature ?


  • Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭feylya


    Line 30 seems to be:
    if (state.equals("Registered") )
    

    SoP won't work cos I don't have any way of testing this due to the jsp. In other words, I don't know how to :P

    This is using a front controller and when I go back to the controller address (/cp3036projapp/controller) instead of a page (/cp3036projapp/controller?command=Login) and then go to the Login page, it works.


  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    state has been set to null with the line :

    request.setAttribute("state", null);

    Then you're trying to compare a null string to "Registered" - that's the error

    Use :

    if (state != null && (state.equals("Registered") ))
    {
    // Do your stuff
    }


  • Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭feylya


    I tried changing it to request.setAttribute("state", ""); and that didn't work. I'll try your way now.

    I just made a work around, where after you log out, the only link available is a link back to the controller so the entire session is reset. Not ideal but it'll do if I can't solve this other ways.


  • Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭feylya


    Cheers deadl0ck, worked perfectly.

    BTW, request.removeAttribute("") works best :)


  • Advertisement
  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    No problemo :D


  • Registered Users Posts: 4,287 ✭✭✭NotMe


    feylya wrote:
    SoP won't work cos I don't have any way of testing this due to the jsp. In other words, I don't know how to :P
    By the way, System.out.println() outputs to a log file in Tomcat/logs. Very handy for debugging.


  • Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭feylya


    I did not know that!


Advertisement