How to use Smack for a web application

am trying to develop a XMPP chat client (web application) which can connect to Gtalk. I am using jsp, servlets and Smack API.

When I ran the code in java class the whole application works smoothly.

But the moment I use jsp servlets I am having an issue regarding presence of the users.

Following is the code in servlet. I get the credentials from the jsp page to the servlet.

            String userName=request.getParameter("username");
    String password=request.getParameter("password");

    // turn on the enhanced debugger
    XMPPConnection.DEBUG_ENABLED = true;

    // Start the connection with the Gtalk server
    ConnectionConfiguration config = new ConnectionConfiguration(
            "", 5222, "");
    connection = new XMPPConnection(config);

    try {
    } catch (XMPPException e) {
    try {
        connection.login(userName, password);
    } catch (XMPPException e) {

    boolean status=connection.isAuthenticated();

        System.out.println("You are logged to gtalk..retreving roster list");
        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();
        for (RosterEntry r : entries) {
             String name = r.getName();
             String user = r.getUser();
             int count1 = 0;
             int count2 = 0;
             Presence presence = roster.getPresence(user);


             if (presence.getType() == Presence.Type.available) {
                     System.out.println(user + "is online");
                } else {
                     System.out.println(name+user + "is offline");



The code System.out.println(presence.toXML()) is returning unavailable for all users even when the user is available. The same code in java class returns correct presence.

So I think Smack API is used only for desktop. Has anyone tried using smack with JSP and servlets? Is there a workaround for this?