Struts 2 + Google AppEngine Integration

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.

gae-logo

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.

  1. Google App Engine plug-in is required to be installed in Eclipse
  2. Latest Java JDK
  3. Google App Engine Java SDK latest version
  4. Struts needs to be integrated into Eclipse
  5. Jars required

Installing the Google Plug-in in Eclipse

  1. Open Eclipse
  2. Click Help -> Install New Software option
  3. Available software window is opened
  4. Here, in work with text box, enter this url https://dl.google.com/eclipse/plugin/4.2
  5. All the available software will be displayed in the window. Select all of them and click on next
  6. Accept the license and click on finish
  7. Google plug-in is installed in your eclipse

1

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.

  1. Develop a web application based on the servlet API. This is because Google App Engine applications are developed based on Servlet API.
  2. 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.
  3. You need a deployment descriptor web.xml.
  4. Apart from the general Deployment descriptor, you need GAE specific deployment descriptor also. This file is called appengine-web.xml
  5. Data storage information also is required in your application development. This information is stored in special file called datastore-indexes.xml
  6. In order to access the appengine API, you need to include appengine-api.jar in the applications lib directory
  7. Remember, apart from the jar, all other files specified above should be placed under WEB-INF folder.
  8. Jar file should be placed under WEB-INF/lib folder
  9. All the class files of your application should be placed under WEB-INF/classes folder
  10. 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.

2

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

3

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


<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">

http://www.w3.org/2001/XMLSchema-instance</a>"

xmlns="<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a>"
xmlns:web="<a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>"

xsi:schemaLocation="<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a>
<a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>"    version="2.5">

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<listener>
<listener-class><appPackageName>.SecurityManagerListener </listener-class>
</listener>

</web-app>

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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Struts2 on Google App Engine</title>
</head>
<body>
<h1>Struts application on Google App Engine </h1>
<table>
<tr>
<td colspan="2" style="font-weight: bold;">Online Library:</td>
</tr>
<tr>
<td><a href="/findBook " />Search the Book you want</td>
</tr>
</table>
</body>
</html>

Now, develop findBook.jsp Page as shown below.

findBook.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
<a href="http://www.w3.org/TR/html4/loose.dtd">http://www.w3.org/TR/html4/loose.dtd</a>">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Find The Book</title>
</head>
<body>
<s:actionerror/>
<s:form action="Availability" method="post">
<s:textfield name="bookName" label="BookName"></s:textfield>
<s:submit name="findBook" value="Find Book"></s:submit>
</s:form>
</body>
</html>

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.


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"<a href="http://struts.apache.org/dtds/struts-2.0.dtd">

http://struts.apache.org/dtds/struts-2.0.dtd</a>">

<struts>
    <include file="struts-default.xml"></include>
    <action name="findBook">
       <result>/findBook.jsp</result>
    </action>
</package>
</struts>

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 <b>findBookAction</b> 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


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"<a href="http://struts.apache.org/dtds/struts-2.0.dtd">

http://struts.apache.org/dtds/struts-2.0.dtd</a>">

<struts>
<include file="struts-default.xml"></include>
<action name="findBook">
<result>/ findBook.jsp</result>
</action>
<action name="Availability" method="findBook"">
<result>/Availability.jsp</result>
<result name="input">/findBook.jsp</result>
</action>
</package>
</struts>

Finally, we need to construct the Availability.jsp file which is the page that is displayed once the user logs in successfully.

Availability.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
<a href="http://www.w3.org/TR/html4/loose.dtd">http://www.w3.org/TR/html4/loose.dtd</a>">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Online Library</title>
</head>
<body><s:actionmessage />
</body>
</html>

Running the application

Now, run the application by providing this url in your browser.

http://localhost:8080

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


<?xml version="1.0" encoding="utf-8"?>
 <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
   <application><strong>hemanthgae</strong></application>
   <version>1</version>
 </appengine-web-app>

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.

  1. Go to Google App Engine site. URL is http://appengine.google.com
  2. There create your application.
  3. 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.
  4. 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.
  5. 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.
  6. Enter the Appropriate Title of your application and enter the authentication option as per your need
  7. 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.
  8. Now, upload the application. To upload the application using Eclipse, click on the icon containing “G” in the toolbar.
  9. From the drop down list, select the option “Deploy to App Engine”.
  10. 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.
  11.  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 http://yourapplicationid.appspot.com/.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Ganesh says:

    say my application uses the Data base then How can i interact with database from Google App Engine?

Speak Your Mind

*