Combine and cache multiple CSS files for performance
This is a simple way to get a performance boost if you’re using PHP + Apache. I am using it to boost performance on GoToQuiz. If you are not using Apache, you can still use the PHP portion of this performance enhancement.
Some quick background: maintaining your site’s CSS in multiple files is frequently useful and sometimes necessary. But including multiple CSS files results in a performance hit when your pages load, because each one requires a separate request from the user’s browser to your server. Each request has associated overhead, which can be minimized if you send all CSS in one request.
This is how you can combine your CSS files with PHP and cache them, relying on a clever bit of .htaccess modification:
How to extract titles from web pages in Java
Let’s say you have a set of URLs and you want the web page titles associated with them. Maybe you’ve data-mined a bunch of links from HTML pages, or acquired a flat file listing URLs. How would you go about getting the corresponding page titles, and associating them with the URLs using Java?
You could use an HTML parser such as Jsoup to request the HTML document associated with each URL and parse it into a DOM document. Once obtained, you could navigate the document and select the text from the title tag, like so:
String titleText = document.select("title").first().text();
Elegant, but a lot of overhead for such a simple task. You’d be loading the whole page into memory and parsing it into a DOM structure just to extract the title. Instead, you could use the Apache HTTP Client library, which provides a robust API for requesting resources over the HTTP protocol. But it would be unnecessary in this case. Let’s keep it simple and rely only on the java standard library.
Create thumbnails and avatars in Java
Avatars–icon-sized images used to represent people online–are usually square. Consider, Facebook avatars are 50 by 50 pixel images. Most source images are not square, however. Cameras typically take pictures with a 4:3 aspect ratio. So to create an avatar from an image, you must do two things: crop it to a square and resize it to your desired icon size.
Thumbnails present a similar challenge. When displaying thumbnails in an image gallery, you typically want to enforce at least one of width|height to be the same for all images to avoid a very sloppy look. The cleanest look is achieved with square thumbnails of equal size, though the necessary cropping may not be desireable in every case.
Read more
Handy RowMapper base class for Spring JDBC
RowMappers are needed all over the place in your Spring JDBC DAO classes. One challenge that I kept running into was that when I wanted to reuse a particular RowMapper class for numerous queries, there was an ever-present threat of an underlying SQLException if certain columns were not present in the ResultSet. Obviously, a reusable RowMapper will set every field on the object it maps for, however not every ResultSet will include every field. Calling rs.getString("column_name"); will result in an exception being thrown if column_name is not present in the particular ResultSet.
So to solve this problem, I wrote this base RowMapper class:
Read more
Define global lists, sets and maps in Spring configuration
Sometimes you may need to define stand-alone collections–lists, maps and sets–in your Spring XML configuration files, so that your beans can reference them as properties. This is easy to do using the util schema. For example, let’s say you want to define a set of credit scores in your applicationContext.xml file:
<util:set id="creditScores"> <value>600</value> <value>710</value> <value>760</value> </util:set>
Now you can reference the set in your beans, like so:
<bean id="creditBean" class="com.this.is.my.CreditBean"> <property name="myScores"> <ref local="creditScores"/> </property> </bean>
Annoying: Netbeans code completion in comments
I don’t understand why Netbeans performs code completion when you are typing a comment. If I am working in a java file, typing a comment, and come to a word like “if” or “for”, Netbeans automatically inserts the code completion templates for those keywords. Shouldn’t it recognize that I am typing within a comment? Is there a fix for this issue?
EDIT: Another thing that drives me mad: I use Alt+F S to save. Constantly. I’m so trained in doing this that I usually don’t even think about it. In Netbeans, however, this often results in the letter “s” being typed, either because the File menu didn’t drop down fast enough or the S key registered twice. I’ve never had this problem in any other program.
How to load an image from a URL in java
Assuming you want to load the image from the URL into memory in order to display or manipulate it:
try { URL imageUrl = new URL("http//example/image.jpg"); InputStream in = imageUrl.openStream(); BufferedImage image = ImageIO.read(in); in.close(); } catch (IOException ioe) { //log the error }
Loading an image from a URL is as simple as that. Now you’ve got a BufferedImage.
It can be even simpler, and if all you are doing is cropping or resizing the image, you may be interested in a little java image utility I wrote. Using my ImageLoader and Image classes, the code would look like:
try { Image image = ImageLoader.fromUrl("http//example/image.jpg"); } catch (IOException ioe) { //log the error }
The Image class is a wrapper on a BufferedImage and provides some useful methods for cropping, resizing, and writing (saving) the image. Let me know in the comments below if you’ve found this helpful.
Widescreens on laptops, reducing vertical resolution
The trend in laptop screens has been to move away from a 4:3 aspect ratio and towards 16:9. Maybe it’s cheaper to produce the widescreens. Maybe it’s a marketing gimmick relating to widescreen HD video playback. But unless you’re using your laptop to play movies all the time, the loss of vertical resolution is really inconvenient.
My old 15-inch laptop, from 2005, has 800 pixels of vertical resolution. All the brand-new 15-inch models have 768 pixels. Considering you’ve got various menu bars, tool bars, status bars, and other things taking up precious vertical space, the actual main window (in your browser, word processor, etc.) has shrunk quite a bit, requiring a lot of vertical scrolling. Assuming you spend most of your laptop time not watching HD movies, this is very frustrating.
I recently purchased a 17-inch laptop, which gives me an extra 100 vertical pixels over my old machine. However, 17-inch laptops once came with 1200 vertical pixels. They then shifted to 1050 pixels, and now a mere 900. What a shame. On the bright side, I don’t think laptop manufacturers will go any lower.
There is even a Facebook group about this issue. (Then again, I suppose there is a Facebook group about every issue.)
Java member access levels – public, protected, private
Java access level cheat sheet:

*no modifier is usually refered to as package-private.
- Classes can be declared:
- public
- package-private
- Methods and member variables can be declared:
- public
- protected
- package-private
- private
It’s easy to remember the effect of public and private. One grants total access, while the other restricts access to the class itself. Both protected and package-private grant access to the package, but only protected grants access to subclasses.
Good practice dictates that you keep access as restricted as possible. This enforces encapsulation and helps forestall bugs in the code. Keep in mind, both public and protected members become part of your API forever (unless you want to break clients, which is not nice). Preserving encapsulation ensures you are not tied to a particular implementation and keeps your API clean.
More Here.
If you repost the chart above, please cite this blog. (icons from famfamfam)
Spam in WordPress Comments
This blog is only a few weeks old, and I’ve managed to attract 12 comment spam attempts. 86% of the comments so far have been spam. When I was initially configuring WordPress I selected moderated comments, so I’ve had to review each one manually. Most spams are of the variety that attempt to fool the blog owner into thinking it’s a real comment. “That was interesting. I’ll have to think about it a little more.” I suppose these are all automated.
I’ve briefly looked into spam filter plugins. I was about to enable Akismet when I learned it is a pay service. I can’t justify paying for spam filtering on a new blog with few comments. If anyone has any plugin recommendations, please share in the comments. I promise to approve all legitimate responses!
UPDATE: I’m up to 92% spam comments now. I guess the bots are starting to discover this blog.
Resize images in Java, preserving image quality
It shouldn’t be so difficult to do simple image manipulation in java. Resizing images is a frequently-encountered need, often to create thumbnails or to shrink pictures taken from digital cameras to a reasonable display size. But how to create thumbnails in java without sacrificing image quality? Standard library image manipulation is severely lacking in this area.
Luckily, talented java programmers have worked to create better solutions. I’ve thrown together an image utility, building off of the work of others, to expose a few basic image manipulation functions, namely: open (from a file, URL, InputStream or byte array), save to file, soften, resize, and resize to square. This may be useful to your project. Just read the important caveat toward the bottom of this post.
- Download: ImageUtil-1.11.zip
I make no warrantees about this utility. If you like it, a link back to this blog would be more than welcome.
Best stats plugin for WordPress: ShortStat
This is just my opinion, based on what I was looking for, which is: a very simple WordPress blog plugin for gathering and reporting stats like unique and total hits, referrers, and search keywords. I wasn’t interested in a heavy-weight stats program. I already use Google Analytics, but for this blog I wanted a plugin to put exactly the stats I’m interested in on my WordPress dashboard.
ShortStat does exactly what I want. It’s a simple and (as of this writing) actively-maintained WordPress stats plugin that provides a quick dashboard link, putting your stats a mere click away. Read more
