Friday, July 29, 2011

Creating a Simple Application using Google App Engine

If hosting your application on the Cloud is your primary goal, then Google App Engine might be a good start. Here we are going to see a simple example of how to run an app on Google App Engine.

Install Google App IDE Plugin for your IDE. (For using Eclipse IDE, go to Eclipse.org download eclipse, and go to code.google.com and find how to get it installed for Eclipse)

Steps are more detailed in : http://code.google.com/appengine/docs/java/gettingstarted/installing.html

After Installing, Create a new Web Application Project

image

Name your Project, and deselect “Use Google Web Toolkit” unless needed.

image

This will be the file structure. Under /src/<package Name>/ you will find your Servlet Class source code. (.java file)

image

 

Next – lets modify the Servlet.

Open your servlet from src/((package_name))/((Project_name))Servlet.java & modify your servlet.java code to make it perform some work, when called as below which gives the date and current time.

   1:  package //your package name;
   2:  import java.io.IOException;
   3:  import javax.servlet.http.*;
   4:  import java.io.PrintWriter;
   5:  import java.text.SimpleDateFormat;
   6:  import java.util.Date;
   7:  import java.util.SimpleTimeZone;
   8:   
   9:  @SuppressWarnings("serial")
  10:  public class Conciliation1Servlet extends HttpServlet {
  11:      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  12:          SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
  13:          fmt.setTimeZone(new SimpleTimeZone(0,""));
  14:          resp.setContentType("text/html");
  15:          PrintWriter out = resp.getWriter();
  16:          out.println("<html><body><p> Current Time is: " + fmt.format(new Date())+ "</p></body></html>");
  17:      }
  18:  }



 


Open web.xml (usually in war/WEB-INF/lib/web.xml ), and modify as shown


   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3:  xmlns="http://java.sun.com/xml/ns/javaee"
   4:  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   5:  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   6:  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
   7:      <servlet>
   8:          <servlet-name> <!-- Give a Name for Servlet--> </servlet-name>
   9:          <servlet-class> <!-- Package Name --> </servlet-class>
  10:      </servlet>
  11:      <servlet-mapping>
  12:          <servlet-name> <!-- Same name as the Servlet provided above --> </servlet-name>
  13:          <url-pattern>/dothis </url-pattern><!-- Give the URL pattern, example /* (or) /dothis, always start the url pattern with / --> 
  14:      </servlet-mapping>
  15:      <welcome-file-list>
  16:          <welcome-file>index.html</welcome-file>
  17:      </welcome-file-list>
  18:  </web-app>



Now Modify index.html under war/WEB-INF/


   1:  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   2:  <!-- The HTML 4.01 Transitional DOCTYPE declaration-->
   3:  <!-- above set at the top of the file will set     -->
   4:  <!-- the browser's rendering engine into           -->
   5:  <!-- "Quirks Mode". Replacing this declaration     -->
   6:  <!-- with a "Standards Mode" doctype is supported, -->
   7:  <!-- but may lead to some differences in layout.   -->
   8:   
   9:  <html>
  10:    <head>
  11:      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  12:      <title>Hello App Engine</title>
  13:    </head>
  14:   
  15:    <body>
  16:      <h1>Hello App Engine!</h1>
  17:      
  18:      <table>
  19:        <tr>
  20:          <td colspan="2" style="font-weight:bold;">Available Servlets:</td>        
  21:        </tr>
  22:        <tr>
  23:          <td><a href="dothis">Current Date</a></td>
  24:        </tr>
  25:      </table>
  26:    </body>
  27:  </html>



Open and update version number (line 4) if needed in appengine-web.xml from war/WEB-INF/lib


If you have a google app engine setup for your google login id, then create a google application. You will be asked to choose an application_id. Use this application_id in line 3, before you upload it to google app engine for testing.


   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
   3:      <application><!—application id—></application>
   4:      <version>1</version> <!-- update version number if needed -->
   5:      
   6:      <!-- Configure java.util.logging -->
   7:      <system-properties>
   8:          <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
   9:      </system-properties>
  10:      
  11:  </appengine-web-app>



Execute the App in Debug Mode


image


image


Click on “Current Date” and you will see the current time.


image


Let us now try to customize it per user (in development server a default user test@example.com gets automatically created with app engine, but when you deploy it to google app engine, you will be asked to be authenticated by Google Sign In Process)


Modify the servlet.java as follows.


   1:  package //package name;
   2:  import java.io.IOException;
   3:  import javax.servlet.http.*;
   4:  import java.io.PrintWriter;
   5:  import java.text.SimpleDateFormat;
   6:  import java.util.Date;
   7:  import java.util.SimpleTimeZone;
   8:  import com.google.appengine.api.users.User;
   9:  import com.google.appengine.api.users.UserService;
  10:  import com.google.appengine.api.users.UserServiceFactory;
  11:   
  12:  @SuppressWarnings("serial")
  13:  public class Conciliation1Servlet extends HttpServlet {
  14:      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  15:          SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
  16:          fmt.setTimeZone(new SimpleTimeZone(0,""));
  17:          
  18:          UserService userService = UserServiceFactory.getUserService();
  19:          User user= userService.getCurrentUser();
  20:          
  21:          String navBar;
  22:          if (user != null)
  23:          {
  24:              navBar = "<p> Welcome, " + user.getNickname() + "! You can <a href=\""+ userService.createLogoutURL("/")+ "\"> Sign Out </a>.</p>";
  25:          } else {
  26:              navBar = "<p> Welcome! <a href=\""+ userService.createLoginURL("/") + "\"> Sign In or Register </a> to customize </p>";
  27:          }
  28:          
  29:          resp.setContentType("text/html");
  30:          PrintWriter out = resp.getWriter();
  31:          out.println("<html><body>");
  32:          out.println(navBar);
  33:          out.println("<p> Current Time is: " + fmt.format(new Date())+ "</p>");
  34:          out.println("</body></html>");
  35:      }
  36:  }



Execute the App in Debug Mode


Open the browser and view index.html


image


Click on Current Date


image


Now Click on “Sign In or Register”, in development it will take you to a simple console that will allow you to login as "test@example.com” but after you deploy in google app engine, you will be asked to login with your google id.


image


test@example.com is a default account that’s in development for debugging use, so choose “Log In”


It will take you back to first screen, because we gave a successful login as to take us to root of the site.


image


Now click on Current Date, lets see what happens now.


image


We should get this type of a window after it has authenticated you.


-- Thz it , its that’s simple–


Now you have a simple Web Application that is ready to be deployed to - Google App Engine


Steps to Upload your application to the cloud is more detailed on : http://code.google.com/appengine/docs/java/gettingstarted/uploading.html


NOTE: update your application_id in appengine-web.xml to the one in your google application admin console for your google account where you are planning to upload the app to. If this is not  the same you might have trouble uploading your application.

Tuesday, July 26, 2011

Getting rid of debt the right way

I think what Prof.Robert Shiller proposes makes a lot more sense at this juncture