Now a days, Cloud computing is becoming more and more popular that large organizations are tending to develop their web applications and deploy them onto the global centre from where all the clients around the world can access the application with ease. Google App Engine also termed as GAE is a PAAS (platform as a service) cloud computing which is used to develop the applications and deploy them onto the Google managed data centres. The use of deploying the applications onto the Google managed data centres is that your application can be properly scaled as the requests for the application increases gradually. App Engine automatically monitors the traffic of the requests and increases the resources as per the need. This facility is free to some extent and if the consumption of the resources increases, then additional charges will be levied for the application owners.
In order to integrate Struts applications with Google App Engine, certain pre-requisites should be satisfied. Those pre-requisites are listed below. We will make use of Eclipse to integrate the Struts 2 application with Google App Engine.
- Google App Engine plug-in is required to be installed in Eclipse
- Latest Java JDK
- Google App Engine Java SDK latest version
- Struts needs to be integrated into Eclipse
- Jars required
Installing the Google Plug-in in Eclipse
- Open Eclipse
- Click Help -> Install New Software option
- Available software window is opened
- Here, in work with text box, enter this url https://dl.google.com/eclipse/plugin/4.2
- All the available software will be displayed in the window. Select all of them and click on next
- Accept the license and click on finish
- Google plug-in is installed in your eclipse
Download and Install Google App Engine Java SDK
It is important to download Google App Engine Java SDK latest version. Using this only, we are going to develop the java applications that can be deployed onto the Google App Engine. The Google App Engine Java SDK will be available from the link.
From this location selection the option Download the Google App Engine SDK. Under this option, select the option Google App Engine SDK for Java. This will Show various options to download the SDK. From the available options, select the option appengine-java-sdk-1.8.1.1.zip. Once the download is complete the SDK is available for use for developing the application.
Note: Remember, that if you have installed the plug-in for java, then the SDK for Google app engine will be available under the Eclipse source folder’s /plugins/com.google.appengine.eclipse.sdkbundle_1.8.1.1/appengine-java-sdk-1.8.1.1
Integrating Struts into Java Eclipse
Please refer the article on migrating old Struts applications into new applications to integrate the struts into eclipse.
Jars required
The following jars are required to be configured in Eclipse.
- commons-fileupload-1.2.2.jar
- commons-io-2.0.1.jar
- testng-6.0.1.jar
- appengine-api-1.0-sdk-1.6.1.jar
- xwork-core-2.2.3.jar
- ognl-3.0.1.jar
- freemarker-gae-2.3.18.jar
- commons-lang-2.5.jar
- commons-logging-1.1.1.jar
- javassist-3.11.0.GA.jar
- struts2-core-2.2.3.jar
- struts2-gae-0.1.jar
- appengine-api-labs-1.6.1.jar
- appengine-jsr107cache-1.6.1.jar
- appengine-local-runtime.jar
- appengine-testing.jar
Developing Java Application to integrate with Google App Engine
The following steps are required to develop the java application and integrate it into Google App Engine.
- Develop a web application based on the servlet API. This is because Google App Engine applications are developed based on Servlet API.
- All the steps that you need to follow to develop a simple general web application will be applied for developing applications for Google App Engine as well.
- You need a deployment descriptor web.xml.
- Apart from the general Deployment descriptor, you need GAE specific deployment descriptor also. This file is called appengine-web.xml
- Data storage information also is required in your application development. This information is stored in special file called datastore-indexes.xml
- In order to access the appengine API, you need to include appengine-api.jar in the applications lib directory
- Remember, apart from the jar, all other files specified above should be placed under WEB-INF folder.
- Jar file should be placed under WEB-INF/lib folder
- All the class files of your application should be placed under WEB-INF/classes folder
- All the static files should be stored under WebContent folder of your web application.
In order to run the application, first you have to run your local app server. For this navigate to Google SDK source folder and from there navigate to bin folder. From here, issue the command as dev_appserver appName.
This steps starts the local application server and all the requests for the application will get started to be served from the port 8080.
Once the java Web Application is developed, you have to deploy it into Google App Engine. So that users around the world can make use of it. The process of deploying the application will be discussed at the end of this article once we complete developing the application.
Before starting to develop the application, make sure that Java and Eclipse plug-in for Google is installed properly. In order to ensure that Google Plug-in is properly installed in eclipse, the icon with “g” in it should be there as shown below.
Remember that you should not use JRE 7 in your eclipse while installing this plug-in. It will create compatibility problems.
Create a new project
Click on the toolbar icon (“G”) as shown above and from the drop down list, select the option “Create new Web Application”. A new window opens. There you provide the name of your project and select the package. If you don’t specify the package, it will not allow you to create the project. Make sure that the Google Web Tool kit and Google App Engine Checkboxes are selected.
Once the project is created, you should see the project structure something like shown below
With this, you are ready to start coding. All the infrastructure required to run the application is in place and the SDK required to develop the application is available to the project. Also, please add the jar files specified earlier into WEB-INF/lib folder. These are App Engine Supporting Jar files.
Add the following jars as well into the WEB-INF/lib folder.
- commons-fileupload-1.2.1.jar
- commons-io-1.3.2.jar
- commons-logging-1.1.jar
- freemarker-2.3.13.jar
- ognl-2.6.11.jar
- struts2-core-2.1.6.jar
- xwork-2.1.2.jar
These jar files will be available in struts lib package. Once you add these jar files into the lib folder, configure them for the project’s build path also as well.
Generally while configuring the jars in the project build path, some jars will be in error or warning mode saying that they will not be available to the application. In order to solve this, add these jars under the Google directory as well for the application. To do this, select the properties of your project, then under Google, go the web application and add these jars in that location.
Developing struts application
Remember, in struts all the requests for the actions will pass through the filter dispatchers in order to invoke the appropriate actions. Hence we need to modify the web.xml file in order to serve the struts action requests. Enter the following code into web.xml
Web.xml
&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;web-app xmlns:xsi="&lt;a href="http://www.w3.org/2001/XMLSchema-instance"&gt; http://www.w3.org/2001/XMLSchema-instance</a>" xmlns="&lt;a href="http://java.sun.com/xml/ns/javaee"&gt;http://java.sun.com/xml/ns/javaee&lt;/a&gt;" xmlns:web="&lt;a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt; http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>" xsi:schemaLocation="&lt;a href="http://java.sun.com/xml/ns/javaee"&gt;http://java.sun.com/xml/ns/javaee&lt;/a&gt; &lt;a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt; http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>" version="2.5"> &lt;filter&gt; &lt;filter-name&gt;struts2&lt;/filter-name&gt; &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;struts2&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;index.html&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;listener&gt; &lt;listener-class&gt;&lt;appPackageName&gt;.SecurityManagerListener &lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt;
Developing the Static Files
We will start developing the Login Page as well the default page (index.html). In this application, the login page will be called from the index page.
Index.html
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;Struts2 on Google App Engine&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Struts application on Google App Engine &lt;/h1&gt; &lt;table&gt; &lt;tr&gt; &lt;td colspan="2" style="font-weight: bold;"&gt;Online Library:&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;a href="/findBook " /&gt;Search the Book you want&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt;
Now, develop findBook.jsp Page as shown below.
findBook.jsp
&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt; &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " &lt;a href="http://www.w3.org/TR/html4/loose.dtd"&gt;http://www.w3.org/TR/html4/loose.dtd&lt;/a&gt;"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt; Find The Book&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;s:actionerror/&gt; &lt;s:form action="Availability" method="post"&gt; &lt;s:textfield name="bookName" label="BookName"&gt;&lt;/s:textfield&gt; &lt;s:submit name="findBook" value="Find Book"&gt;&lt;/s:submit&gt; &lt;/s:form&gt; &lt;/body&gt; &lt;/html&gt;
Once the static files are created, these files needs to be configured in the struts configuration so that struts application will be connected with these files to collect the user input and then supply them to the action to perform the business logic.
This configuration will be done in struts.xml file. Create the struts.xml file in the folder where logger properties file is present.
&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "&lt;a href="http://struts.apache.org/dtds/struts-2.0.dtd"&gt; http://struts.apache.org/dtds/struts-2.0.dtd</a>"> &lt;struts&gt; &lt;include file="struts-default.xml"&gt;&lt;/include&gt; &lt;action name="findBook"&gt; &lt;result&gt;/findBook.jsp&lt;/result&gt; &lt;/action&gt; &lt;/package&gt; &lt;/struts&gt;
Creating the ServletContextListener
Now, we need to create the servletContextListener class.
This is required to restrict the OGNL security listener to restrict our application from serving the requests. Create a java class called SecurityManagerListener and enter the code as shown below
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import ognl.OgnlRuntime; public class SecurityManagerListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent ctx) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent event) { OgnlRuntime.setSecurityManager(null); } }
This makes sure that the security manager is disabled for application. Next, create a package called freemaker.core under your src. Next, download the java file from the following link and place that file under this freemaker.core folder TextBlock.java
Now create the Find Book action java class. This class will process whether the bookName entered is present in the library or not.
findBookAction.java
import com.opensymphony.xwork2.ActionSupport; public class &lt;b&gt;findBookAction&lt;/b&gt; extends ActionSupport { private static final long serialVersionUID = 1L; private String bookName; public String findBook(){ boolean found = searchMyLibraryForTheBook(library.getBookName()); if (found) addActionMessage(“Fantastic…Requested Book was in Online Library”); else addActionMessage(“Sorry Boss…Requested Book was not in Online Library”); return SUCCESS; } addActionError("Sorry Boss…Requested Book was not in Online Library "); return SUCCESS; } public boolean searchMyLibraryForTheBook(String bookName) { if (bookName.equalsIgnoreCase("Struts 2")) return true; return false; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; }
Configure the Struts.xml file to identify this action.
Struts.xml
&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "&lt;a href="http://struts.apache.org/dtds/struts-2.0.dtd"&gt; http://struts.apache.org/dtds/struts-2.0.dtd</a>"> &lt;struts&gt; &lt;include file="struts-default.xml"&gt;&lt;/include&gt; &lt;action name="findBook"&gt; &lt;result&gt;/ findBook.jsp&lt;/result&gt; &lt;/action&gt; &lt;action name="Availability" method="findBook""&gt; &lt;result&gt;/Availability.jsp&lt;/result&gt; &lt;result name="input"&gt;/findBook.jsp&lt;/result&gt; &lt;/action&gt; &lt;/package&gt; &lt;/struts&gt;
Finally, we need to construct the Availability.jsp file which is the page that is displayed once the user logs in successfully.
Availability.jsp
&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt; &lt;%@ taglib prefix="s" uri="/struts-tags"%&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " &lt;a href="http://www.w3.org/TR/html4/loose.dtd"&gt;http://www.w3.org/TR/html4/loose.dtd&lt;/a&gt;"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt;Online Library&lt;/title&gt; &lt;/head&gt; &lt;body&gt;&lt;s:actionmessage /&gt; &lt;/body&gt; &lt;/html&gt;
Running the application
Now, run the application by providing this url in your browser.
Enter the book name you want to search in the online library. And click on submit.
If the book is found, the application will display the page that says Book is available in Online Library. If the book is not found, then application will display the message saying that Book is not available in the Online library
Appengine-web.xml
This file will be located in your web application’s lib folder. Update this file with the following content
&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;appengine-web-app xmlns="http://appengine.google.com/ns/1.0"&gt; &nbsp; &lt;application&gt;&lt;strong&gt;hemanthgae&lt;/strong&gt;&lt;/application&gt; &nbsp; &lt;version&gt;1&lt;/version&gt; &lt;/appengine-web-app&gt;
Deploy the application onto the Google App Engine
Once, you confirm the application is running properly on local server, it is time to deploy the application onto Google App Engine. To do so, you have to follow the below steps.
- Go to Google App Engine site. URL is http://appengine.google.com
- There create your application.
- There it will ask for the Verification Information. You can use the same google account which you are using for your Gmail for uploading purpose. Once you provide the verification system, it will perform the verification.
- It will ask for the application identifier. You have to make sure that the application identifier that you specified in your appengine-web.xml is specified in this site as well.
- Make sure that the application ID that you maintain in your appengine-web.xml and the one you specify here should be same. Also remember that special characters and Upper case letters are not allowed in this id.
- Enter the Appropriate Title of your application and enter the authentication option as per your need
- Once you click on finish, the application registration is completed. Remember that once the application is registered, you cannot un-register it. But you can delete it in future. Even you delete the application, next time you cannot register another application with the same name as that of this application.
- Now, upload the application. To upload the application using Eclipse, click on the icon containing “G” in the toolbar.
- From the drop down list, select the option “Deploy to App Engine”.
- Follow the instructions specified from this point onwards. Keep in hand your application identifier as it will be asked during the upload process. This identifier is nothing but the one you specified in your appengine-web.xml file.
- Eclipse will automatically pick up the information present in the war file of the project and deploy it into the Google App Engine.
Check the status of the application once it is deployed.
Generally, it will take some time for the application to be deployed onto the app engine and set right go. This is because, it has to do some internal settings like creating the indexes for Data Stores. Once this is done, you can test your application by entering the following URL in your web browser