Friday, June 12, 2009

The Myth of the Genius Programmer

This is a video recorded at Google I/O 2009. It's very informative and enlightening :) Basically the presenters were talking about being ready to accept failures and criticisms and also admit mistakes. Failing at different things is good but failing at the same thing over and over again means that you are not learning.

Thursday, June 11, 2009

A simple example with StringTemplate

I have always wanted to write this article but often find myself preoccupied with many other things. Please spend 20 minutes of your day to read this article. It is a food for thought in my opinion and the main audience are Java developers developing web applications using Eclipse together with Apache Tomcat.

I would like to introduce a very powerful and convenient tool to generate HTML pages using StringTemplate. This tutorial assumes that the readers are well versed with writing servlets, configuring web server and Java programming. The main intention and focus of this article is to expose ourselves to the power of this template engine using Apache Tomcat web server; not Jetty, not Resin, nor JBOSS. I hope you find this blog entry useful although I will not go in depth with the nitty gritty of the details.

A short history of StringTemplate: this tool is written by Terence Parr, my former Computer Science professor at University of San Francisco. It is a useful tool to generate HTML pages dynamically and enforces a distinct and clear separation between view and model in the MVC model.

Tools that I use: Eclipse 3.4.0, Apache Tomcat 6.0 and of course the two important jar files that you need to download from the StrngTemplate official website. They are antlr-2.7.7.jar and stringtemplate.jar. Unpack the compressed file and copy the two jar files into WebContent/WEB-INF/lib folder of your project in the Eclipse IDE.

DISCLAIMER: the codes shown below are adapted from Terence Parr's sample implementation at his website.

Now let us get into the meaty part of this article by introducing the servlet dispatcher. Imagine the job of a postman sending letters to different destinations. However in our context, my servlet dispatcher is actually dispatching the users' requests to the appropriate servlets. I will name this class FrontendServletDispatcher and a sample of implementation is as below:As you may notice in the implementation of FrontendServletDispatcher, there is a class called InvalidPage that subclasses Page. I wrote that class as a way to prevent users from going into illegal servlet paths. At least the users are informed that the path they are trying to go is not available. Details of other classes that subclass Page are followed after the demonstration of web.xml. This is a file you cannot ignore when you are developing web application with Tomcat Apache.After that, I create a folder called "templates" in the com.pages package. This is where all the st files are located. The next thing to focus is the Page class and the subclasses (LandingPage, LoginPage and InvalidPage). The code shown below demonstrates to you the implementation of the super class Page and the introduction of StringTemplate and StringTemplateGroup.Page class gets an instance of "templateLayout" from the folder "template". "templateLayout" serves as a skeletal or a building block of dynamic content in a web page. I include all four necessary st files for our reference in this discussion. The first st file is called "templateLayout.st", followed by "landingPage.st", "loginPage.st" and "invalidPage.st"Last but not least, here are the implementations of LandingPage, LoginPage and InvalidPage classes.For a start, you may try changing the string of the titles. You do not actually need to restart the server as the changes will take effect immediately, somewhat similar to what we know as hot-deploy. I hope you have fun trying this out and please feel free to drop any comments that further improves this article :)

Tuesday, June 9, 2009

localhost:8080 and not localhost:8080/projectname

Typical scenario:
[1] You are developing a dynamic web application using Eclipse in Java with project name, say "ServletTutorial".
[2] You have configured your web.xml in the WEB-INF folder.
[3] Everything looks fine when you are testing your web application within Eclipse and with the URL localhost:8080/ServletTutorial

You feel happy BUT ... you are not that happy and not completely satisfied and you want to set the root URL of your web application at localhost:8080

Soon after that, you tried to look at the web.xml, context.xml and server.xml and perhaps, not only looking at those files but also editing them hoping to get your web application running at localhost:8080 instead of localhost:8080/ServletTutorial

You feel frustrated and do more online research. Googling, Binging and Yahooing with the keyword string "do not show java project name in localhost:8080" does not yield any relevant results. Even worse, you type in "localhost:8080 does not show java project name" and the search engines thought that you are experiencing some issue with Tomcat or with localhost:8080 and suggest you links about showing the project name. Frustration is mounting and your web application is still running at localhost:8080/ServletTutorial but not at localhost:8080

You then bang at the wall or the table or try to suffocate yourself with the pillow if you are doing programming lying on the bed.

SOLUTION: After spending quite some time tinkering with Tomcat Apache and doing lots of reading by myself, I have finally come out with a simple solution. As simple as it may be, many IT Operations Engineers may have other much better suggestions and probably disagree with this solution.

My solution works anyway, otherwise it is not a solution, isn't it?

This is what I did eventually:
[1] Export your project as ROOT.war
[2] Keep in mind that it is case-sensitive ROOT.war
[3] Copy the ROOT.war file into the webapps folder of your Tomcat directory
[4] Tomcat will unpack the war file by itself

You will now able to run your web application at localhost:8080 and you are smiling again :)

Friday, June 5, 2009

2 to the power of N

Here is a one-liner statement to check if a given integer is of 2 to the power of N.

public static boolean twoPowerN(int x){
return ((x & -x) == x) ? true : false;
}
You may try the method using this test case:
System.out.println(twoPowerN((int) Math.pow(2, 8)));

Basically it returns true for all integers 2powN, for example 2^100 or 2^7 or 2^33. A false will be returned for values other than 2powN. Well, that's the purpose of this method anyway (to check if a method is 2 to the power of N), right? :)

Have a good day everyone!

Solving Palindrome Using Recursion

This is a follow-up article about solving palindrome using recursion. While we solved this problem previously using iteration (link to previous article), I am demonstrating the way of solving it in a recursive manner.

public static void isPalindromeRecursive(String text){
text = text.toLowerCase().replaceAll("[\\W\\_*]", "");
if (helperFunction(text, 0, text.length()-1)) {
System.out.println("Is a Palindrome");
} else {
System.out.println("Is not a Palindrome");
}
}

private static boolean helperFunction(String input, int leftIndex, int rightIndex){
if (leftIndex < rightIndex) {
if (!(input.charAt(leftIndex) == input.charAt(rightIndex))) {
return false;
}
leftIndex++;
rightIndex--;
helperFunction(input, leftIndex, rightIndex);
}
return true;
}

Monday, June 1, 2009

The Wii Killer?