Example
In this example we will create a public commenting service, similar to those that can be found on blogs and news sites. We will make two versions of this commenting service, the first one is a formed based application and the second one is based on AJAX and Comet events.
Prerequisite
For this example you will need:
You also need to download the following command line tools:
Finally, you also need to know the following topics:
Creating the project
We start with the first version of this commenting service. Open Eclipse and create a new Dynamic Web Project and name it e1. Change the name of the WebContent folder to something simple like web.
JDBC
We start with our database, we would like to have a single table in which we store all the comments and two methods, one to read comments from it and another one to store new comment.
Create the file index.jdbc and copy / paste this content to it:
comments{
@(pyp)
string pyp
long time
string(63) email
string txt
}
add : insert into comments values(?,?,?,?)
get : select time, email, txt from comments where pyp = ? order by time
Explanation: we defined a single table, called comments, this table has for columns in it named:
- pyp - this is a unique identifier of the comments channel.
- time - a time-stamp when the comment was posted.
- email - the email of the person who posted the comment.
- txt - the email of the person who posted the comment.
We also created an index on the pyp column so retrieving comments will be faster.
Use the JDBC Code Generator to generate the code and follow the instructions in the generated readme.html file.
Page
We would like to have two URLs, one for reading comments and one to post a new comment.
Comment{
string date
string from
string txt
}
list read / string pyp
void add / string pyp ? string txt
Explanation: we defined a structured, called Comment, and two actions:
- read - read comments from a specific channel (pyp).
- add - post a new comment to a specific channel (pyp).
Use the Page Code Generator to generate the code and follow the instructions in the generated readme.html file.
Login
We will use a great login service called dolog.in, download the login filter and follow the instructions in the readme.html file.
web.xml
Running the example
If you did everything right till now you should have a project that looks like this:
- Example - src - ajax - AjaxFilter.java - Common.java - Get.java - GetImp.java - Send.java - SendImpl.java - comet - Comet.java - jdbc - Pool.java - Transaction.java - web - META_INF - WEB_INF - lib - h2-1.2.147.jar - json.jar - web.xml - ajax.js - comet.js - index.css - index.html - index.js - temp.js - index.adl - index.jdl
If this is how your project looks like then you are ready to launch it. Open the server view and create a new server, choose Tomcat 6. Double click on the server and switch to the module tab. Click on the Add Web Module button and add your project. Make sure to give it the ROOT (/) context path (otherwise it won't work). Make sure you made the proper changes to the server.xml file as instructed in the comet readme page.
Launch this example and start posting messages on the wall, you can try posting messages from two different browsers (Firefox and Chrome) and see the real time updates.
Conclusion
This example demonstrate how powerful these tools are:
- Fast development - just take a look again at the size of the code !!!.
- Efficiency - the generated code is design to be very efficient.
- Fewer bugs - the generated code handle all the communication, let you focus only on the logic part of the application, as a result fewer bugs arise during development.