Hi, I'm Andrew Bowers,
product manager for developer
products at Google.
Today I'm going
to give you an early look
at Java language support
on App Engine.
I'll guide you
through the development
of a simple guestbook
application,
from creation to deployment.
Let's get started.
I'll start by opening up
the Eclipse IDE.
Here I have the standard Java
perspective,
and you can see a few extra
toolbar icons at the top left.
We've extended
the environment
with a Google plugin
for Eclipse,
providing support
for Google Web Toolkit
and App Engine.
Of course,
you can also develop
using another IDE, or from
the command line if you wish.
To create a new project,
I'm going to click
on the blue "g"
in the top left.
This brings up the new
web application project wizard.
I'll enter in a project name
and a package for my project.
I can also choose whether
to use App Engine
or Google Web Toolkit
with the project.
Choosing these will configure
the starter project differently,
depending on what you pick.
For this example
I'm going to create
a simple JSP servlet,
so I'll uncheck
Google Web Toolkit
and click, "okay."
The project wizard has now
created a template project
that I can edit and run.
Taking a look
inside the project,
you'll notice that
it's a standard J2EE Type
servlet project that can
be run on any J2EE container.
There's a source directory,
which contains
our Java source files,
like you'd expect.
The App Engine and Java Virtual
Machine SDK libraries.
And finally, a web archive,
or war directory.
App Engine uses a standard Java
web archive directory
as its deployment directory.
You put the files you want
to deploy to App Engine
into this directory,
including static files,
JSPs, and your compiled
class files.
Going back
to the source directory,
you'll notice that there is
a standard Java servlet file.
This file responds
to an HTTP request,
and outputs, "Hello, world."
Let's try it out locally.
In order to do that,
I need a server.
Luckily, App Engine ships
with a development web server
that mimics
the production environment.
This enables you
to download the SDK
and immediately start developing
on your local machine.
I'll start the local server
by choosing,
"debug web application."
This starts the server
on port 8080.
Navigating to this URL,
I see, "Hello, world."
We now have the foundation
for our project.
Let's add the source files
that we need
for a guestbook application.
This application
is going to consist
of three basic source files.
First, we'll have
a guestbook.jsp file
that contains the HTML form
for creating a new entry.
That will post
to a GuestBookServlet,
which will accept
the form variables
and create
a GuestBookEntry object.
We'll then save that object
to the datastore,
and redirect back
to the JSP page.
Finally, the JSP page
will list out
all of the GuestBook entries
in the datastore.
I've already written most of
the code for those three files.
I'll import them
into my project.
And I'll drag the JSP file
into the web archive directory.
Finally, I want to modify
the web.xml configuration file
to point to my new JSP.
I'll do that
by opening up the web.xml,
and changing
the welcome file entry.
Let's run this again,
and you'll see
that I have a simple
HTML form that submits,
but it doesn't store anything
to the datastore yet.
Let's take care of that next.
App Engine's datastore is built
on top of Big Table,
a scalable, distributed
storage platform.
In addition
to low-level datastore APIs,
we provide standards-based
APIs in the form
of Java Data Objects
and Java Persistence API.
JDO and JPA are Java standards
for persisting
plain Java objects
to a datastore.
They're datastore agnostic,
mapping to relational databases,
XML, or in our case,
the App Engine datastore.
This means that I can
write a Java class
and persist those objects
directly to the datastore
without having to read
and write specific fields
to and from a table.
In this example,
we'll use JDO.
I'd like to store the
GuestBookEntry objects.
In order to do that,
I simply annotate the class.
JDO then performs a step called,
"enhancement,"
where it will modify
the bytecode of the class
so that it can persist
to the datastore.
First, I'll import
the annotation classes.
Next, I'll add an annotation
to the class
that indicates it is capable
of persistence.
Third, I'll annotate the fields
I wish to save
with a Persistent annotation
from JDO.
And finally,
every object in JDO must have
a unique primary key.
So I'll set the primary key
in the class.
I want the datastore to allocate
a unique key for me,
so I'll ask it to do that
in the annotation.
Saving the file causes
the DataNucleus enhancer
to run in the background,
making my class capable
of being saved to the datastore.
Now we need to actually save
to and query from the datastore.
JDO uses something called
the PersistenceManager
to interact with the datastore.
You get a JDO
PersistenceManager
using a
PersistenceManagerFactory.
Now, a PersistenceManagerFactory
is analogous
to a database connection pool,
and it has some overhead
associated with creating it.
In order to be efficient
in our application,
we want to create a singleton
instance of the factory,
and only create it once.
If you remember,
our JSP posts to the servlet,
which then creates
a GuestBookEntry object,
and should store it
to the datastore.
We create a PersistenceManager
from our factory.
Next, we save the object by
calling makePersistent.
Then we close our JDO
PersistenceManager.
That's it.
Now let's go to our JSP,
and query for all the entries.
We get
the PersistenceManager again.
I create a query
with SQL-like constructs,
and tell it which object
I'm looking for.
Finally, I execute the query
and assign the results
to a list
which I can iterate through.
Now, with our JDO
datastore code in place,
let's try it out.
I go back to my local server,
enter in an entry,
and voila.
The JSP posts to the servlet,
the servlet creates
a new entry object,
persists to our datastore,
and then redirects back
to the JSP.
The JSP then queries
the datastore
and lists out the entries.
I now have a fully functioning
guest book application.
I want to share that
with the world
by deploying to App Engine.
So I go back to Eclipse, and
click on the deploy icon
in the top-left corner.
This brings
the deploy dialogue box.
You'll notice there's
an error at the moment.
That's because I've not yet set
the application I.D.
I click on project settings,
and then click on the link
to the right, which takes me
to the App Engine console.
Every application
on App Engine must have
a unique application I.D.
to identify it.
I create a new I.D.
And then I go back to Eclipse
and set the I.D. in my project.
Now all I have to do is enter
my account I.D. and password.
App Engine will now compile my
Java source code, if needed,
then package everything up
in my web archive folder
and send it up to the cloud.
I can go back to my browser
and navigate
to my new guest book application
in the cloud.
You've just seen how easy
it is to go from creating
a new project to deploying
a working application
on App Engine.
But there's a lot more to the
App Engine environment
than we've demoed here,
including authentication,
mail, and the memcache API.
You can also easily
use Google Web Toolkit
to create modern AJAX frontends
entirely in Java.
Now, our Java language support
isn't yet feature complete.
We're giving developers early
access to the product
in order to solicit feedback
as soon as possible
and insure that the
end result meets your needs.
To learn more,
go to code.google.com/appengine.