How do I end a session in JSP?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: How do I end a session in JSP?

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    39

    How do I end a session in JSP?

    I am using a session variable to pass values from one form to another as follows:
    session.setAttribute(“username”)
    session.getAttribute(“Username”)

    However, when the user logs out, I want the session to end. If they press "Back" on their browser after logging out, they should not be placed back in the previous file as though they are logged in.

    Can anyone please suggest an answer?
    Last edited by Maria Modeste; 10-06-2005 at 01:48 PM.

  2. #2
    Join Date
    Mar 2004
    Posts
    78
    Just invalidate the session while logging out.

    HttpSession.invalidate();

    this will invalidate the session and getSession(false) will tell you if there is valid session. )

  3. #3
    Join Date
    Sep 2005
    Posts
    39
    Thanks but I really don't know how to use the method. I got this message when I tried it:
    Can't make static reference to method void invalidate() in interface javax.servlet.http.HttpSession. HttpSession.invalidate();

    the code I used is as follows:

    <% HttpSession.invalidate();%>
    <a href="home.jsp">Log Out</a>

  4. #4
    Join Date
    Mar 2004
    Posts
    78
    Ok. Then I will tell you some details.

    <%session.ivalidate();%>

    If I would have been you I would invalidate the session when the user enters the application.

    When you use a servlet or JSP(it still servlet) you have access to an object called request. Actually it is javax.servlet.http.HttpServletRequest. By this object all the servlets, including JSP communicate - they simply forward it to one antoher. This request holds all the HTTP CGI parameters sent by the web browser or added by any servlet or JSP(it is servlet) . It also holds a session object - HttpSession. You get the session by:

    request.getSession(boolean). If the paramter is true then if there is no session a new one is created. if false - you get null if session is not created.

    So in you doGet/doPost you receive request and response. In a JSP you have the predefinite parameters session, request and etc.

    When talking about session tracking or just site navigation you can put a parameter in the session that will be changed whenever you go to a specific page. For example if the user clicks exit you set its value to -1. if he is on page 2 - set it to value of 2. When navigating you will always know what is he watching and you can control him ). If the parameter is not set or the session expires the parameter will not exist in the session - this means that your apllication has just been entered by the user.

    ${empty sessionScope['paramterName']} tells you if the paramter exist

    ${sessionScope['paramterName']} gives you the parameter value

    If there is any other confusion - ask. I will give you ICQ# if needed.

    Let the JSP problem free be with you )

    Mr. PLS
    Last edited by NewUniverse; 10-11-2005 at 03:10 AM.

  5. #5
    Join Date
    Sep 2005
    Posts
    39
    Thanks Mr. PLS. I'll work on it today. If I have any problems, I'll get back to you.

  6. #6
    Join Date
    Mar 2004
    Posts
    78
    Is there any good result ?

    What's up ?

    PLS

  7. #7
    Join Date
    Sep 2005
    Posts
    39
    I have been working on the rest of the website for the past few days. Please excuse me for not responding sooner. I am downloading ICQ as I write. These are the steps that I have taken:

    1) I am invalidating the session on the logon page, am I right? With a session.invalidate();

    2) Then on that same page, do I do a request.getSession(variableName); When I tried this I got the following error message:
    Incompatible type for method. Can't convert java.lang.String to boolean.

    When I left the method empty and used it as follows:
    <%=request.getSession()%>
    I got the following error message:
    getAttribute: Session already invalidated

    My browser crashed with the first two steps, but I still would like to ensure that I have the other steps correct.

    3) However I went on to do a session = pageContext.getSession();on the other page Should I have done a session.getAttribute(variableName);instead?

    Mr. PLS what line of code really creates the session here? Kindly advice me if I have the steps correct and what may be causing my code to crash on the first page.

    Regards,
    Maria
    Last edited by Maria Modeste; 10-14-2005 at 10:47 PM. Reason: Grammatical errors

  8. #8
    Join Date
    Mar 2004
    Posts
    78
    So, before I start I will say.
    Please have a look at the java docs.
    I beleive that it will be much easier for you.

    When you want to create a session you should use this:
    <%request.getSession(true);%>
    when you set the input prameter to true if the session do not exist or it is invlaidated a brand new session is created.

    When you want to read a session varible use:
    a/ <%request.getSession(true).getAttribute("attributeName");%>
    b/ using the EL: ${sessionScope["attributeName"]}

    When you want to add a session variable:
    a/ <%request.getSession(true).setAttribute("attributeName", attributeValue)%>

    b/ using JSTL:
    <c:set name="attributeName" value="attributeValue" scope="session"/>

    I think that this will be enough for you.
    ICQ# 297-185-630 I suppose that you will need me a little bit more.

    Blessings...
    Mr.PLS

Similar Threads

  1. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 10:40 PM
  2. Re: App Object (fixes)
    By Rob Teixeira in forum .NET
    Replies: 129
    Last Post: 06-06-2002, 05:23 AM
  3. App Object
    By Rob Teixeira in forum .NET
    Replies: 15
    Last Post: 05-31-2002, 03:30 PM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  5. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center