Creating sessions in servlets using Smack


I have login code in one servlet:LoginGtalkServlet…

 XMPPConnection connection;

 //@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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


    //ProxyInfo proxyInfo = new     
    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();

And my GetRosterServlet has the code for retreving the roster list

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Roster roster = connection.getRoster();
Collection entries = roster.getEntries();
for (RosterEntry r : entries) {
String user = r.getUser();
String name = r.getName();
System.out.println(name + user);
roster.addRosterListener(new RosterListener() {
// Ignored events public void entriesAdded(Collection
// addresses) {}
public void entriesDeleted(Collection 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) {



Now the isssue is my GetRosterServlet is not informed that I haved logged into Gtalk..i.e LoginGtalkServlet is not communicating with GetRosterServlet and hence connection.getRoster() is throwing Null pointer exception...

How do I let the roster servlet know that the user is logged into gtalk and hence get the friends list

I read about Session class in Smack API documentation.

I wanted to know if I can use HTTPSession get the logged info and  pass or should I use Session class of smack for this??

An example would be good

Allow Facebook a moment to deliver presence information after you have established the connection.

So I put a short wait time after succesfully connected before I load my buddy list.


//do what you want to do before sleeping

Thread.currentThread().sleep(2000);//sleep for 2000 ms

//do what you want to do after sleeptig


catch(Exception ie){

//If this thread was intrrupted by nother thread


Hope this helps