Google App Engine tutorial

5,325 views

Published on

Google App Engine tutorial for Java. Demonstrates how to open an account, setup a connection between your server and an Android app and some more features of GAE.

Published in: Technology, News & Politics

Google App Engine tutorial

  1. 1. Google App Engine tutorial 1 Google App Engine tutorial - 2013
  2. 2. Table of contents • Open an account • Installing GAE plugin for eclipse • Installing JRE and JDK • Register & Login example • GAE Junit • More features of GAE • Example source code Note: As for now, it is possible to write your server side code in Python, Java or PHP. This slideshow demonstrates an example using Java. 2 Google App Engine tutorial - 2013
  3. 3. Open an account Suppose we have a Gmail account: nameforthetutorial@gmail.com. Go to the address: https://developers.google.com/appengine/ and click Sign Up. 3 Google App Engine tutorial - 2013
  4. 4. Open an account Fill in the project name. The Project ID is your unique server name. You can use the one that was generate for you or set one as you like as long as no one else is already using it. Your server address will be http://<your_project_id>.appspot.com. Click Get Started. 4 Google App Engine tutorial - 2013
  5. 5. Open an account In the next window click your project name. 5 Google App Engine tutorial - 2013
  6. 6. Open an account Here you can view all of your projects. You can open up to 10 projects per Gmail account. Click your project name. 6 Google App Engine tutorial - 2013
  7. 7. Open an account Now click App Engine. 7 Google App Engine tutorial - 2013
  8. 8. Open an account You made it. That’s your main dashboard. Here you manage your app. 8 Google App Engine tutorial - 2013
  9. 9. Installing GAE plugin for eclipse Depends on your eclipse version, follow the instructions in the relevant link: https://developers.google.com/eclipse/docs/install-eclipse-4.3 https://developers.google.com/eclipse/docs/install-eclipse-4.2 https://developers.google.com/eclipse/docs/install-eclipse-3.7 When finished you’ll see a blue Google symbol ( Now sign in to your account: In the lower right corner click Sign in to Google: 9 Google App Engine tutorial - 2013 ) in your upper toolbar.
  10. 10. Installing JRE and JDK You may have problems with your JDK or JRE, so download the latest versions from the java site: http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads1880261.html http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads1880260.html 10 Google App Engine tutorial - 2013
  11. 11. Register & Login example A simple example only to understand how to communicate between your Android app and your GAE server. We will use POST request. Usually Get request is for getting back data from the sever, while POST request is for storing data in the server. In Get request the parameters are passed in the url, while in POST request a package is being sent to the server with the parameters encoded inside. The following example uses POST request both for getting back data from the server and for storing data in the server. The reason is that in order to get back the data from the server, our password is being sent on the web and we don’t want it to appear in the url. Note: Using POST request is not a sufficient protection for sending password on the web. An SSL protection is needed as will be shown next. 11 Google App Engine tutorial - 2013
  12. 12. Register & Login example – Server side Click the Google symbol ( Project… 12 ) in your eclipse. Then choose New Web Application Google App Engine tutorial - 2013
  13. 13. Register & Login example – Server side Fill in your server side app name and also a package name. Click Finish. 13 Google App Engine tutorial - 2013
  14. 14. Register & Login example – Server side Your server side project should look like in the image. Predictable problems: - If you are missing the App Engine SDK or the JRE System Library then: Right click your project name > properties > Java Build Path > choose Libraries tab > in the menu on the right choose Add Library… > choose Google App Engine > Next > Finish. In the same way for the JRE but instead of choosing Google App Engine choose JRE System Library. - If the warWEB-INFlib is empty then try: Right click your project name > properties > expand Google > App Engine > uncheck Use Google App Engine box > OK. Then do it again but now check the Use Google App Engine box and your libraries should be added. Note: In general Project > Clean do magic. 14 Google App Engine tutorial - 2013
  15. 15. Register & Login example – Server side Now Go to appengineweb.xml and set your project id. That’s the connection between your code and the Google App Engine project you opened earlier. 15 Google App Engine tutorial - 2013
  16. 16. Register & Login example – Server side Create two classes: Register & Login. We need them in order to define two servlets: https://clean-abacus-384.appspot.com/login https://clean-abacus-384.appspot.com/register 16 Google App Engine tutorial - 2013
  17. 17. Register & Login example – Server side Define the two classes we’ve just created as servlets, and map them to the corresponding urls. For example, according to the written on the right the url https://clean-abacus-384.appspot.com/login is mapped to the servlet Login which is referred to the com.serversideexample.Login class, which we defined in the previous slide. In lines 34-42 you can see the definition of the security constraint for using SSL. For further reading about applying SSL protection in GAE: https://developers.google.com/appengine/docs/ja va/config/webxml#Secure_URLs 17 Google App Engine tutorial - 2013
  18. 18. Register & Login example – Server side Register class extends HttpServlet in order to define the Register servlet. The datastore may contain many kinds of entities all together in the same table (there is only one table). Here we create a Person kind entity by specifying it in the entity’s constructor. 18 Google App Engine tutorial - 2013
  19. 19. Register & Login example – Server side As before Login class extends HttpServlet in order to define the Login servlet. We should specify which entity we’re looking for (line 47). Note: in CompositeFilter also “or” operator may be used. In FilterOperator also “gt”, ”gte”, ”lt”, ”lte”, ”ne”, ”in” may be used. 19 Google App Engine tutorial - 2013
  20. 20. Register & Login example – Server side Deploy your project: Right click your project name > Google > Deploy to App Engine and then click Deploy. 20 Google App Engine tutorial - 2013
  21. 21. Register & Login example – Client side Suppose you have a little experience in Android. Create a new app with the following permissions: Now set main_activity.xml to look like that: 21 Google App Engine tutorial - 2013
  22. 22. Register & Login example – Client side A simple activity which listens the two buttons and calls the execute function in the ClientAsync class in order to perform the POST request to the GAE server. 22 Google App Engine tutorial - 2013
  23. 23. Register & Login example – Client side ClientAsync extends AsyncTask in order to work in background, sets a POST request with the encoded parameters which the server is expecting for. ClientAsync uses HttpsURLConnection for supporting GAE’s SSL security constraint. Finally displays the server’s answer with the TextView object. 23 Google App Engine tutorial - 2013
  24. 24. Register & Login example – Testing Starting point: Datastore is empty. Fill in some details and click Register. 24 Google App Engine tutorial - 2013
  25. 25. Register & Login example – Testing After clicking Register the Person will be shown in the datastore. Note: The App engine gives every entity an unique id (key). For further reading about keys: https://developers.google.com/appengine/docs/java/datastore/entities#Java_Genera ting_keys 25 Google App Engine tutorial - 2013
  26. 26. Register & Login example – Testing Further Login Testing: 26 Google App Engine tutorial - 2013
  27. 27. GAE Junit Test your code in a convenient way. Setting up the framework: • Right click your project name > properties > Java Build Path > choose Libraries tab > in the menu on the right choose Add External Jars… > go to eclipsepluginscom.google.appengine.eclipse.sdkbundle_1.8.6appengine-java-sdk1.8.6libimpl and select appengine-api.jar, appengine-api-labs.jar, appengine-apistubs.jar > Open > OK. • Right click your project name > properties > Java Build Path > choose Libraries tab > in the menu on the right choose Add External Jars… > go to eclipsepluginscom.google.appengine.eclipse.sdkbundle_1.8.6appengine-java-sdk1.8.6libtesting and select appengine-testing.jar > Open > OK. • Right click your project name > properties > Java Build Path > choose Libraries tab > in the menu on the right choose Add Library… > choose Juint > Next > choose Junit 4 > Finish. For further reading about Junit: https://developers.google.com/appengine/docs/java/tools/localunittesting 27 Google App Engine tutorial - 2013
  28. 28. GAE Junit Junit: To run the tests do Run > Run As… > Junit Test Note: The changes are local and have no effect on your GAE datastore. 28 Google App Engine tutorial - 2013
  29. 29. More features of GAE Jackson/Gson: An easy way to transfer objects threw the web. User class should be at the server side and at the client side. In the server side we convert user to json string into the response and in the client side we read the json string and convert it back to user. Server side: Client side: For further reading about Jackson: http://wiki.fasterxml.com/JacksonInFiveMinutes http://wiki.fasterxml.com/JacksonDownload For further reading about Gson: https://code.google.com/p/google-gson/ 29 Google App Engine tutorial - 2013
  30. 30. More features of GAE Objectify/JDO/JPA: It’s possible to define an entity as a class. More comfortable, less dirty code. For further reading about Objectify: https://code.google.com/p/objectify-appengine/ For further reading about JDO: https://developers.google.com/appengine/docs/java/datastore/jdo/overview-dn2 For further reading about JPA: https://developers.google.com/appengine/docs/java/datastore/jpa/overview 30 Google App Engine tutorial - 2013
  31. 31. More features of GAE Sending mail from your server: You can send and receive emails on behalf of your project ID. Great for password recovery, confirm registration etc. For further reading about mailing options: https://developers.google.com/appengine/docs/java/mail/ 31 Google App Engine tutorial - 2013
  32. 32. More features of GAE Logging messages to Logs console: You can log out notes of some categories: severe, warning and info. You can view your logs in the Logs section in your app’s console. Great for debugging  … … For further reading about logging: https://developers.google.com/appengine/docs/java/#Java_Logging 32 Google App Engine tutorial - 2013
  33. 33. More features of GAE Running on local host: You can run your app locally. Do Run As > Web Application. Your server’s url now is http://localhost:8888/. You can get to your app’s console in: http://localhost:8888/_ah/admin. Note: you may need to remove your SSL protection (use http://...) when testing locally. 33 Google App Engine tutorial - 2013
  34. 34. More features of GAE More relevant topics: • Transactions: atomic operations in the datastore. Obligatory for concurrent writes to datastore. https://developers.google.com/appengine/docs/java/datastore/transactions • ThreadSafe: allowing your server to run your server side in parallel. https://developers.google.com/appengine/docs/java/config/appconfig#Java_appengine_w eb_xml_Using_concurrent_requests • Backends: background services for heavy computations. https://developers.google.com/appengine/docs/java/backends/ •Blobstore: storing large files like images, videos etc. https://developers.google.com/appengine/docs/java/blobstore/ • Quotas: limits for free datastore usage. https://developers.google.com/appengine/docs/quotas#Datastore 34 Google App Engine tutorial - 2013
  35. 35. Example source code https://drive.google.com/folderview?id=0B0ZJh5uX_P5iLXNQdXVZc2stSDQ&usp=sharing Daniel Abitbul – abitbul6@gmail.com Omer Shpigelman – omer.shpigelman@gmail.com 35 Google App Engine tutorial - 2013

×