I am having issues with presence when I try to use smack inside servlets. I get the credentials from the login form via doPost method..I can able to successfully authenticate as well as connection.getRoster() also works.Next I want to show only users who are online so when I get the presence of user,presence object stores default value "unavailable" for all users even when they are available!!

The whole chat app works without flaw in a normal java class without any change..
String userName = request.getParameter("username");
    String password = request.getParameter("password");
        HttpSession session=request.getSession();
        session.setAttribute("username", userName);
    SmackAPIGtalkServlet gtalk = new SmackAPIGtalkServlet();
    ConnectionConfiguration config = new ConnectionConfiguration(
            "talk.google.com", 5222, "gmail.com");
    connection = new XMPPConnection(config);
    try {
    } catch (XMPPException e) {
    try {
        connection.login(userName, password);
    } catch (XMPPException e) {
    boolean status = connection.isAuthenticated();
    if (status == true) {
public void displayOnlineBuddyList() {
    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();
    int count1 = 0;
    int count2 = 0;
    for (RosterEntry r : entries) {
        Presence presence = roster.getPresence(r.getUser());
        if (presence.getType() == Presence.Type.unavailable) {
            // System.out.println(user + "is offline");
        } else {
            System.out.println(name+user + "is online");
    roster.addRosterListener(new RosterListener() {
        // Ignored events public void entriesAdded(Collection<String>
        // addresses) {}
        public void entriesDeleted(Collection<String> addresses) {
        public void entriesUpdated(Collection<String> addresses) {
        public void presenceChanged(Presence presence) {
            System.out.println("Presence changed: " + presence.getFrom()
                    + " " + presence);
        public void entriesAdded(Collection<String> arg0) {
            // TODO Auto-generated method stub
I have created connection for each request Therefore registering listeners against that connection will be pointless since it will pass out of scope as soon as you leave the method, and eventually get garbage collected. Also XMPP is completely asynchronous by nature whereas servlet requests are synchronous. So the code that works in a standalone app does not work in this J2EE.

I am stuck with this and not able to get the code working with servlets..Can anyone help me out??