> There was a suggestion of using the setAttribute() method of the
> ServletContext with the name of the user and when the user logs off remove
> this attribute. If a user tries to log on, system first checks in the
> context as to whether he is already logged on.
>
> This will work fine for me in all but one situation for me. If the user
> closes the browser rather
> than logging out, the value will be still there in the Servlet Context.
> There was a suggestion about using the destroy method of the applet to
> remove this attribute. But i cannot do this also , since destroy will be
> invoked when i click different links as one applet is giving way to
> another. What do i do in this case?

Do not use the destroy method of the Applet. What you can do is that you can
put the session object in the sevlet context and then check if there is a
session object
in the context corresponding to the username. You can then check whether the
session
represented by the session object is valid() or not and accordingly manage
the login.

>
> There was also a suggestion to implement session tmeout. Does this mean
> that i have set the setMaxInactiveInterval method of the session object to
> a particular time interval. Even if i set that , how does it remove the
> above said attribute from the context after the expiry of the session.

I think you should set the session timeout value or let the default value be
there.
I think what you are saying about the attribute staying in the context for
eternity is correct..
 The workaround for this may be as follows.

Create a class which implements the HttpSessionBindingListener interface. On
creation of this object
pass it the user name and referance to the ServletContext and put it in the
session by inserting the following line where the user logs in..

session.setAttribute("Cleaner", new CleanUp(name, getServletContext()));
// the name should be same as the key used for storing the attribute in the
// context.

When the session expires, the valueUnbound(..) method  is called
and the implementation of this  method removes the
stored attribute from the context. Something like the code given below

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


class CleanUp implements HttpSessionBindingListener
{
    public CleanUp(String name, ServletContext context)
    {
        this.name = name;
        this.context=context;
    }

    public void valueBound(HttpSessionBindingEvent evt)
    {
        // nothing to do here
    }

    public void valueUnbound(HttpSessionBindingEvent evt)
    {
        // remove the session attribute from the context
        context.removeAttribute(name);
    }
    private String name;
    private ServletContext context;
}

I have tried this on tomcat and it works !!

regds,
Gokul

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to