JSP pages using SMACK api

I just started using the SMACK API for creating JSP pages that will connect to the openfire server , get presence information, send notifications, etc

If you are trying to do something similar … post here

I’ll keep on updating this thread with the JSP code that i write!!

<%@page import= "java.io.IOException,

org.jivesoftware.smack.ChatManager,

org.jivesoftware.smack.ConnectionConfiguration,

org.jivesoftware.smack.ConnectionListener,

org.jivesoftware.smack.MessageListener,

org.jivesoftware.smack.XMPPConnection,

org.jivesoftware.smack.XMPPException,

org.jivesoftware.smack.packet.Message,

org.jivesoftware.smack.packet.Presence"

%>

<%!

ConnectionConfiguration connConfig = new ConnectionConfiguration(“talk.google.com”, 5222, “gmail.com”);

XMPPConnection connection = new XMPPConnection(connConfig);

public void jspInit()

{

}

public void jspDestroy()

{

connection.disconnect();

System.out.println("Never gets executed ");

}

%>

<%

System.out.println(“Starting IM client”);

try

{

connection.connect();

System.out.println("Connected to " + connection.getHost());

}

catch (XMPPException ex)

{

//ex.printStackTrace();

System.out.println("Failed to connect to " + connection.getHost());

}

try

{

connection.login(“username”, “password”);

System.out.println("Logged in as " + connection.getUser());

Presence presence = new Presence(Presence.Type.available);

connection.sendPacket(presence);

}

catch (XMPPException ex)

{

//ex.printStackTrace();

System.out.println("Failed to log in as " + connection.getUser());

}

%>

The above code is a JSP file that connects to GoogleTalk

just modify the username and password

The problem is that it still remains connected even when i disconnect it within jspDestroy() method

Can somebody help ??

I would guess the destroy method is not the right place to disconnect. Research when this is called, and the alternatives.

The jspDestroy() method is called just before the servlet gets destroyed… Placing the code here will definitely nt work.

But I dunno where should i place the code for disconnecting from the server

Any suggestions ???

I haven’t got any experience with JSPs, but I have heard about a SessionListener, maybe that will guide you in a good direction.

it just doesnt seem to work but i have a hint … i’ll figure it out

the next piece of code is on its way

I haven’t actually tried running it but if you checkout the latest smack source there is a sample jsp app that checks the roster.

svn co http://svn.igniterealtime.org/svn/repos/smack/trunk smack

The attached file Connect.jsp allows connection / disconnection from any jabber server. I tested it with Googletalk and OpenFire.

(One of the reasons why i m using JSP to do this is because i m developing a jabber client for Second Life as well;) which only allows http and xml-rpc )

Next tasks to do :-

  • Getting Presence info

  • Sending Messages
    Connect.jsp (1957 Bytes)

Hello jezz, I’m workin in my proyect( a jabber client with jsp and vxml), but I have a terrible problem when I request the a JSP for login to the service tomcat show error, tomcat say that can’t compile the document because for example a class “cannot be resolve” this ocuur with all the class of smack api. Really I don’t know how to config my web application for tomcat can compile the JSP, I try to use the directive tag but i get other error, please if you can help me respon here or to my email.First I am develop the application as a normal web with html wich use a browser, after I will use vxml, but I am in the first step yet.

thanks

Att: Heider

hello Heider …

Becoz the problem is occuring with all the classes of smack library,Firstly i wud like to know whether the files smack.jar and smackx.jar are included in the classpath or no …??

I prefer to keep these files directly in the " jdk1.6.0_03\jre\lib\ext " directory

IF you need sample JSP programs created using smack api… let me know

Thankx.

@Jezz

Hello jezz, thank for you sugges. And yes, I would like take or see some examples of JSP wich use smack API, because I need develop my proyect soon, also I would like see some examples of web application with JSP wihch explain how to config the file (WEB-INF, META-INF,and taglibrary and file tld, HOW THIS WORK?, because I don’t understand so well how to config the *tld file for use taglib).

thank you and sorry for the several question.!!

Att: Heider

Hello again!!

the jar file smack and smackx, I hadn’t included in the classpath. I included this file in the " jdk1.6.0_05\jre\lib\ext " directory but the error continue, I am using the jdk 1.6.0_05. I try to includ the jar file in a lib into the WEB-INF but the error continue. I listen any suggestion THANK AGAIN.

I attach a file with the error produce for tomcat.
error tomcat.txt (3971 Bytes)

I had attached a file called Connect.jsp previously in this post

You can download that file and try it…!! It will connect to OpenFire server running locally on ur machine

Enter the port number , and ur username and password and use the following syntax in the browser:

http://localhost:7373/Connect.jsp?user=username&pass=password&cmd=connect

http://localhost:7373/Connect.jsp?user=username&pass=password&cmd=disconnect

I have some more files… will send it to you when i get to work tommrow

Jezz !! thank you for your suggest the application now is working, your last recommendation was OK. Here I recommend the following suggertion if other people need help:

If you are using tomcat make this:

  • Copy the smack jar file to the next directory: CATALINA-HOME/common/lib. (CATALINA-HOME is for example c:\file programm…)

  • If the openfire server use ports greater than the tomcat apache server change the port of tomcat for other more greater than the openfire server. For example if tomcat use 8080 port y openfire use 9090 change the port of tomcat to for example 9095…

I made this and test the Connet.jsp file attach for jezz how he say.

Thank again and I hope that this help for other people.

Att: Heider

Hello Jezz. I have another quesion. How I can contol the sendig and recieving messages into the chat?.

The user connect to application and send a messages, but how he can recieving the messages that the other user send within the JSp page od course, I have listen that for web application was better use applets for chat because jsp or servlest don’t allow refresh very well the or something like that, but the samck api allow create a thread to maintenace the conversation with the chat class, but How I can embeded the conversation into the page ? how include a box wihch allow show the messages the arrive from the other user.

thank and sorry the long explaneation for make the question

hi heider,

I think you should develop an applet which is basically a chat window where the incoming messages will be displayed.

To receive incoming messages you will have to add a MessageListener or a PacketListener.

refer the documentation for details

Hi All,

I’m in the process of doing something similar (adding chat functionality within a web app), and I’m running into some trouble. I haven’t touched Java (or programming for that matter) in over 4 years, so I’m scraping away some cobwebs here. Regardless, I need help and I don’t find the “documentation” very helpful. I’m finding the listeners very complicated and I’m not sure where to instantiate them. I’ve gotten to the point where I make a connection to my Openfire server in a jsp, instantiate a self-developed “Chat” object that I maintain on the session (meant to store connection info, messages, etc.), and currently display the connection’s roster of users including their status (online vs. offline).

Now I’m at the point where I want to start messaging, but I am really getting into a mess. I first started using Chat objects, but it looks like my only option to receive, store, display and reply to messages is to use a PacketCollector. I don’t like this because it’s synchronous and I need this app I’m developing to displya messages as they come in (asynchronous). However, what I’m not understanding is exactly how to send a message to a user on my roster, have this message show up in this user’s jsp message viewing instance, then have this user reply back to the first user - the result would be that both users would have the complete conversation show up in the web pages in question, and also allow them to continue to add more messages, end the conversation and start a new one. Please help! I’m getting incredibly frustrated.

Ok. First I respon you what I consider that you have more problems, for recieve the messages in asyncrous way you should use pollmessages function which is in the MUC class this is true if your are work into a chatroom, I recomend you that work with servlets because I don`t sure that it can maintenece the connection object trought the differents jsp pages, but maybe it would be possible with implicit object of jsp. Other suggets is that you should use HTML frames for built a webcity where you can see several feauture for example roster, send message, and a windows where you can see the messages. Too you will need use javascritp for some function.

Att: Heider

Hi, maybe the better way to do that this days is using AJAX. With that, you could use one of this three techniques:

  • AJAX polling with Packet.pollResult()

  • AJAX long polling with Packet.nextResult(int timeout)

  • AJAX streaming with Packet.nextResult()