Categories
Computing Java

Generating Short Alphanumeric Identifiers in Java

I will make it short and sweet, for a change: suppose you have a large number that you want to distribute to users, and you want to instead to give them a shorter, easier to share string of characters.  220p9 instead of 3453453. Neat, no?

I grappled with this, being a moron, for way too long – looking into Apache Commons Id. The issue is that I also needed to use this ID with a database, and that means that there could be issues of concurrent requests for identifiers and other messy situations. I also did not need something overly fancy, just alphanumeric identifiers. The problem was that I was too stressed to think, if I may be excused. Anyway, the solution is just silly:

Let the database generate the usual integer unique identifiers that grow with time. Whenever you need to use a short, alphanumeric identifier, simply use base conversion, to base 36, on that number. How easy is that? The conversion, in Java is done using the Long or Integer objects’ toString(number, radix) methods. To convert back use the valueOf() method.

Pretty bad of me.

Share
Categories
Commercials Computing Java WebSphere

Building an IM Bot with the Smack API. Avoid setting Roster Permissions

So I built what is an instant messaging bot using the Smack API and the Openfire XMPP server. Doing it is pretty simple of you read the documentation and Smack’s developer notes. The bot relies on a queue-like object that Smack gives you called a PacketCollector. All you need to do is log in to the server, and let the packet collector wait for incoming messages. Clearly, you need to respond to the messages and use thread tools to do the waiting and all, but in general, the effort is relatively straightforward.

Share
Categories
General

Jetty 6 welcome file: how to use a servlet to handle your welcome file

I ‘ported’ my Java web application from Tomcat to Jetty. Like any web application, I defined a ‘welcome file’ in the application’s web.xml file. The welcome-file directive specifies the URL to be loaded by default when someone requests the root of your application. The welcome file may be just a file or a JSP, e.g. index.html, or it may be a name that is mapped to a servlet’s url (through the element in web.xml).

In Tomcat, specifying something like

Code listing of web.xml file

will work just as you would expect. But in Jetty, it will not. Unless you provide a workaround. What the somewhat angrily written post says is this: If you map a servlet to the URL index.htm, a file called index.htm must still exist. You *must* have a static, even empty, file called index.htm to have Jetty pass the request to your servlet that is mapped to that URL. Sorta cludgy, glad I found the answer quickly. Hope this helps others.

Share
Share