Saturday, February 5, 2011

Plain old M$ Stan on the road to Google app engine programming. Part 1.

This is a complete java and google app engine noob’s guide for getting from zero to somewhere. So if you are an experienced java/gae programmer please leave now Smile.

You start with creating your app on a google engine: http://code.google.com/appengine/. After you are through this bit, as an app developer you face a choice of python or java based development for the engine. For me this was a tough one. First, I was quite excited by all those google videos on python. But after few days developing and reading, I switched to java. Main reasons for that is similarity to C# (me bad), great support for refactoring and unit tests from Eclipse, amount of libraries available for java, and speculatively, java performance over python’s. Good post to read here on StackOverflow.

When creating my first Web app for the engine as described by google here I went for “with GWT” option. I don’t plan to use GWT but I was interested in what would the project layout be etc. I should say this GWT option with compile time of ~35 seconds for this HelloWorld type of application just confirmed what I learned from the classic ASP.NET world – that “automagic” is not worth it. I prefer to stay pretty much on the client side with javascript/jquery with some simple mvc on the server pumping json to me.

And so I went with the “GWT” option unchecked for the next project. Build time? Can’t even notice!

Here is that jsp war folder, I understand this is kind of a deployment structure for the app. That lib folder is something like “bin” folder in asp.net. Appengine-web.xml, logging.properties and web.xml comparable to web.config, let’s say.

image

Next step was to scratch my head on the subject of an MVC framework. I wanted something really simple and when I saw that StringTemplate from the same guy who wrote antlr, my choice was done for a moment.

As a total java/eclipse noob I needed this to see how to add StringTemplate jars to the project. I copied them as well to the war’s lib folder as you see above.

I went with the simplest example I found, applied to servlet:

package com.sdnotimetolose.gaelinks;
 
import java.io.IOException;
import javax.servlet.http.*;
 
import org.antlr.stringtemplate.*;
import org.antlr.stringtemplate.language.*;
 
 
 
@SuppressWarnings("serial")
public class Sdnotimetolose_gaeedilinksServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/plain");
        
        StringTemplate hello = new StringTemplate("Hello, $name$", DefaultTemplateLexer.class);
        hello.setAttribute("name", "World from GET and StringTemplate");
        resp.getWriter().println(hello.toString());
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, world from POST method");
    }
}

To my happiness, that worked both locally and on the engine. Still I have to look if use of Closure templates for java would not be more natural choice for the engine.

This is it for today. Your’s Stan.

Post a Comment