
I’m playing with Google App Engine!
In case you’ve been living under a rock, Google App Engine is basically free hosting for Java and Python apps. Your app is hosted on an appspot.com subdomain, and you can pay for extra resources if your app starts to get really popular.
I’ll be using it to play around with Java web development (since my webhost doesn’t support Java). I may play around with Python eventually, but for now, I’ll be leveraging my Java experience to do my bidding.
Interested in playing with Google App Engine, too? I’ll show you my attack plan for how to get started. ^_^
Some Background Info
To tell you the truth, I’m no stranger to Google App Engine. I’m no expert, but I’ve been playing around in it for about a year, and I’ve developed two or three apps for work. I’ve been meaning to make some personal apps with it, but sometimes working as a full-time software engineer means that you have less of an itch to do software development on the side. But now I do, so let’s see what fun stuff I come up with. ^_^
In general, this is what I’ve learned about App Engine:
- Free hosting is always awesome.
- It’s very easy to get a web app up and running.
- Authentication (Google Accounts) and data storage (Datastore) are taken care of for you.
- Java servlets, JSP, and the Datastore are enough for small apps.
- For bigger apps, there are framework options, but I don’t know if there’s a clear winner.
The apps that I’ve made so far are small and have a clear, almost single line of purpose. For these apps, I didn’t feel the need to use a framework. I’ve noticed that raw servlets and JSP get the job done quite nicely. However, when it comes time for me to make something bigger, I’ll need to decide on a good framework.
There are some options to choose from. I’ll list a few here. I haven’t really looked at any of them yet, and I don’t plan on to (at least not right now), but for your convenience:
- Grails. Spring + Groovy = Grails. This is probably the best choice for big apps. I haven’t used Grails or Groovy before, but I’ve used Spring, and I must say that Spring is pretty powerful. And big. Spring can swallow you whole if you’re not careful, so you really shouldn’t use it for tiny apps, where the overhead just gets in the way. I assume Grails is the same way. However, for more complex apps, the stuff that Spring offers (e.g., dependency injection) is invaluable.
- Gaelyk. Gaelyk was specifically made for App Engine. Like Grails, it utilizes Groovy, but unlike Grails, it’s very lightweight. If I had to pick a framework to use right now, I’d probably pick Gaelyk over Grails, at least as I transition from small apps to not-so-small apps.
- Play. The Play framework is another lightweight framework. It’s not made specifically for App Engine, but there’s a way for you to plug it in.
There are more frameworks if you look hard enough, but those were the ones I thought showed the most potential. Google has a page called Will It Play in App Engine? that lists some more frameworks, libraries, and utilities and whether or not they’re compatible with Google App Engine.
Getting Started
If you want to get started with Google App Engine, here’s what you do:
- Sign up for Google App Engine. For some reason, you’ll need a cellphone to sign up. Also, you can’t use a mobile number that’s already been used to sign up a Google App Engine account.
- Download Eclipse and the Google Eclipse plugin.
- Launch Eclipse and create a new Web Application Project.
- Make sure “Use Google App Engine” is checked.
- You can give Google Web Toolkit a try if you want, but I don’t recommend it. If you hate JavaScript but want to use it, then use GWT. Otherwise, you’re better off using jQuery for your JS needs.
- When you’re ready to upload your app, click the Google App Engine button in Eclipse, enter in your login info and application ID, and you’re good to go!
A Few Tips and Pointers
If you’re working on a small or simple app, don’t bother wasting time finding and installing some framework. Java’s got all the tools you need. Roll your own poor man’s MVC framework with these steps:
- Use JSP pages as your “view.” That’s simple enough, right? Keep your JSP pages free of heavy code, and just give it the role of displaying content to your users.
- Use servlets as your “controller.” When requests come in, have your servlets handle the processing, then forward on to the JSP view.
- Make some Java classes as your “model.” Make up whatever data structures you’ll need for your app. These will get stored in Google’s datastore.
- Tie your servlets to your JSP with web.xml. Once you do this step, your poor man’s MVC is complete!
This simple strategy has helped me write clean code for my apps, and it’s also allowed me to adopt a REST-based strategy for my URLs. For example, let’s say you wanted to create a simple app that lets users view data and create data. This app might consist of two primary URLs:
- /users/view
- /users/create
Following the above strategy, your web.xml would look something like this:
<!--?xml version="1.0" encoding="utf-8"?--> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>View</servlet-name> <servlet-class>myapp.ViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>View</servlet-name> <url-pattern>/users/view</url-pattern> </servlet-mapping> <servlet> <servlet-name>Create</servlet-name> <servlet-class>myapp.CreateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Create</servlet-name> <url-pattern>/users/create</url-pattern> </servlet-mapping> </web-app>
And your CreateServlet, for instance, may look like this:
public class CreateServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Do processing stuff
// Store data as request/session attributes for the JSP page to consume
// Forward to the JSP page
RequestDispatcher dispatcher = request.getRequestDispatcher("/views/users/create.jsp");
dispatcher.forward(request, response);
}
}
That’s all I have for now. I’ll write some more once I dig more into Google App Engine.