Archive for the 'Software' Category

Create a Ridiculously Simple Status Image in a Java Servlet

Thursday, February 19th, 2009

We were recently working on a project at work that required we submit some data by appending a dynamic image tag with JavaScript. The data was transmitted in a GET request through a query string defined in the src attribute. We wanted to return a 1×1 image that could be tiled as a background image to indicate the status of the request. With such a simple image requirement, I wanted to find a more elegant solution than streaming an existing image from a file. My experience dealing with images in Java is extremely limited, and a quick Google search didn’t turn up a concise example, so this is mostly just here for reference (mine and others’).

BufferedImage buffer = new BufferedImage(IMG_SIZE, IMG_SIZE, BufferedImage.TYPE_INT_RGB);
Graphics g = buffer.createGraphics();
g.setColor(new Color(Integer.parseInt(success ? SUCCESS_COLOR : FAILURE_COLOR, 16)));
g.fillRect(0,0,IMG_SIZE,IMG_SIZE);

resp.setContentType("image/png");

OutputStream os = resp.getOutputStream();
ImageIO.write(buffer, "png", os);
os.close();

IMG_SIZE in this case is 1 and assumes a square image. You’ll notice the parseInt method call specifies a radix of 16, so that allows SUCCESS_COLOR and FAILURE_COLOR to be defined as hex strings, which to a web developer are second nature.

Think Big

Monday, December 1st, 2008

After having recently audited a web application for proper character encoding support, I have one piece of advice for web developers in this area and that is: you should always support UTF-8 encoding across the board right from the start.

If you just take a few steps at the beginning of a project to enable Unicode encoding, you won’t ever have to worry about international character support, your application is “future-proofed.” When the time comes for your application to go global, it’s ready to go.

For example, if you use PHP you can set the encoding with the following statement in your php.ini file:
default_charset = "utf-8"

Or with Java set the following jvm setting:
-Dfile.encoding=UTF-8

In your Apache config file, set:
AddCharset UTF-8 .utf8
AddDefaultCharset UTF-8

You also need to set the database encoding to match up. In MySQL you can do this by setting the following directive in your my.cnf file:
default-character-set=utf8

It is important to note that you also need to specify the encoding for the connection. For a JDBC connection you can do this by adding the following parameters to the database URL:
useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8

Finally, if you are going to be sending emails from an application, you will want to specify the encoding in the Mime type as well. Using JavaMail, this can be done while setting the text of a MimeBodyPart:
mimeBodyPart.setText(htmlContent, MimeUtility.getDefaultJavaCharset(), "html");

If you make a concerted effort to implement the same encoding across the board, it will certainly help in the long run, as you won’t ever have to deal with character encoding issues.

For more information and specifics about UTF-8 encoding, visit UTF-8: The Secret of Character Encoding

Multiplayer Email 2.0

Thursday, October 16th, 2008

Email Center Pro

For almost 2 years, I’ve been working on a product called Email Center Pro – anyone who reads this blog probably already knows that. It’s an email management solution for small to medium sized businesses that makes it super easy for a group of people to manage hundreds, even thousands of emails a day.

This is a big week, because we just launched version 2.0 of the software to our user base. The new version has a completely rewritten JavaScript client and makes use of an entirely new RESTful web services API. It’s got a lot of powerful new search features and introduces the dashboard which will soon allow users to customize their home page with analytics tools. 2.0 is a big step forward but it also lays the groundwork for the exciting things Email Center Pro will do in the very near future.

For anyone who has a web site, manages more than one email address, or has at least one other person involved with their business, I would whole-heartedly recommend this product. Sure, I may be biased, but I have seen first-hand the real gains in efficiency that it provides for an organization.

JavaScript iPhone Lock Slider with jQuery

Monday, May 19th, 2008

I was thinking the other day about the amount of form spam that most web sites get (and again just now as I moderated a ton of blog comments), and I was reminded of a proof of concept I saw a while back. It required the user to utilize drag and drop functionality to submit a form, thus verifying human interaction. Then I thought of the iPhone’s unlock function and how it would make a cool CAPTCHA.

I’ve written a handy JavaScript class for including these types of sliders into a web site- stick around after the break for more details and cool configuration options.

(more…)