Results 1 to 4 of 4
  1. #1
    Jagdeesh123 is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Question problem in session invalidation.

    i have written code to logout the user session in my project.
    but its not if i click back option, the session is still alive. please suggest me.


    my code is Java code:


    public void logout()

    {

    session = request.getSession();

    session.invalidate();

    RequestDispatcher rd = new RequestDispatcher("Homepage");

    rd.forward(request,response);

    }


    After logout, if I click back , the session is still alive.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    Try this below code.

    HttpSession session = req.getSession(false);
    if(session != null){
    session.invalidate();
    }
    Ramya:cool:

  3. #3
    kalpanakovuru is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by Jagdeesh123 View Post
    i have written code to logout the user session in my project.
    but its not if i click back option, the session is still alive. please suggest me.


    my code is Java code:


    public void logout()

    {

    session = request.getSession();

    session.invalidate();

    RequestDispatcher rd = new RequestDispatcher("Homepage");

    rd.forward(request,response);

    }


    After logout, if I click back , the session is still alive.
    Hi Jagdeesh,

    For this u need to check session object (which u created) is null or not if it is null then u need to redirect to login page.

    I am giving some example:

    this is in server side code ..

    HttpSession session=request.getSession(false);
    //here u are geeting the user name from session
    String strname=(String) session.getAttribute("username");
    //here u are checking the session object (the value which u store in session) if it is null u are redirecting..
    if(username==null){
    //session=request.getSession();
    RequestDispatcher rd=request.getRequestDispatcher("/Login.jsp");
    rd.forward(request, response);
    return;
    }

    HttpSession session=request.getSession(false); //here u are geeting the user name from session String strname=(String) session.getAttribute("username"); //here u are checking the session object (the value which u store in session) if it is null u are redirecting..

    if(username==null){ //
    session=request.getSession();
    RequestDispatcher rd=request.getRequestDispatcher("/Login.jsp");
    rd.forward(request, response); return; }



    Like this only u need to check in jsp also.
    It can surely redirect to login page..



    While u logout use must invalidate the session .
    write this code in logout servlet

    HttpSession session = request.getSession(false);

    if(session!=null)
    {
    session.invalidate();
    }
    RequestDispatcher rd=request.getRequestDispatcher("/Login.jsp");
    rd.forward(request, response);
    return;

    HttpSession session = request.getSession(false);
    if(session!=null) { session.invalidate(); }
    RequestDispatcher rd=request.getRequestDispatcher("/Login.jsp");
    rd.forward(request, response); return;


    It is better to use filter to check the session object otherwise u need to check the condition in every servlet it become redundancy ..


    I hope this is useful for u...

  4. #4
    anubhavranjan is offline Member
    Join Date
    Sep 2009
    Location
    Bangalore, India
    Posts
    10
    Rep Power
    0

    Default

    Hi Jagdeesh,

    Try this

    Java Code:
    HttpSession session = request.getSession(false);
    if(session!=null)
    {
    session.invalidate();
    response.sendRedirect("/Login.jsp");
    }
    Also make sure that in each of the JSP page, you are checking whether the session is new. If its new then redirect to the welcome page.
    Last edited by anubhavranjan; 12-06-2010 at 04:07 PM.

Similar Threads

  1. Replies: 1
    Last Post: 04-21-2009, 11:20 PM
  2. Problem with Session Scope while using with Java Bean
    By sulthanmytheen in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-27-2009, 10:50 AM
  3. Replies: 2
    Last Post: 12-23-2008, 06:35 PM
  4. Session Problem
    By irfan.ahmad in forum Java Servlet
    Replies: 4
    Last Post: 10-08-2008, 10:50 AM
  5. how to use session variable in my problem
    By Arif Baig in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-27-2008, 07:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •