Forums/Product Manuals/Raspberry Pi

Running a Web Application on Raspberry Pi

Gary
posted this on July 02, 2012 13:48

Installing a Web Application Server & Browser

Web Application Server

To run Elastic COBOL programs as web applications, we first need to install a Web Application Server. For this example we are going to use Apache Tomcat 7 which can be downloaded and installed for free. Start a graphical session ($ sudo startx) on your Pi and open up a command console (Start > Accessories > LXTerminal):

$ cd $HOME
$ wget http://mirrors.axint.net/apache/tomcat/tomcat-7/v7.0.28/bin/apache-tomcat-7.0.28.tar.gz
$ tar xzf apache-tomcat-7.0.28.tar.gz
$ mv apache-tomcat-7.0.28 tomcat7

Note: the exact file names above may be different depending on what version you download. See the Apache Tomcat home page for details.

Before we start Tomcat, we need to add a user that has permissions to deploy applications:

$ cd $HOME/tomcat/conf
$ vi tomcat-users.xml

Immediately after <tomcat-users> add the following line and save the file:

<user username="system" password="raspberry" roles="manager-gui"/>

Now let's start Tomcat:

$ cd $HOME/tomcat7/bin
$ sudo sh startup.sh
Using CATALINA_HOME: /home/pi/tomcat7 Using CATALINA_TMPDIR: /home/pi/tomcat7/temp Using JRE_HOME: /usr Using CLASSPATH: /home/pi/tomcat7/bin/bootstrap.jar:/home/pi/tomcat7/bin/tomcat-juli.jar

 

Browser

We recommend using the Chromium browser on the Pi. This can be installed as follows:

$ sudo apt-get install chromium-browser

To check Tomcat is running:

  • Start > Internet > Chromium Web Browser
  • Click "Make Chromium the default browser" and "Start Chromium"
  • Enter "http://localhost:8080" in the address bar and hit enter (if you don't see the console, you may need to wait a minute or two to let Tomcat finish loading).
  • Click "Manager App"
  • Enter "system" for user name and "raspberry" for password, and click "Login".

You should now see something similar to:

Capture.PNG

Compiling & Packaging the Application for Tomcat

$ cd $HOME/ec4rpi/samples/servlet-register/resources
$ ecc register
Elastic COBOL V12.6.27 Copyright (C) 2010-2012 Heirloom Computing

LOC: 439 (42 variables in 3 records)
Warnings: 0
Errors: 0
Result: Compilation SUCCESSFUL
Building: Class files

You can find out details and specifics on what needs to be done to successfully deploy a web application to Tomcat here. The following instructions are abridged specifically for this application:

$ mkdir WEB-INF; mkdir WEB-INF/classes # this is part of the required deployment structure
$ cp reg*class WEB-INF/classes # this is the application code produced by Elastic COBOL
$ unzip -q $HOME/ec4rpi/ecobol.jar -d WEB-INF/classes # this is the Elastic COBOL runtime
$ cp $HOME/elasticcobol.properties WEB-INF/classes # this is the Elastic COBOL license file
$ cat >WEB-INF/web.xml # this is the Tomcat deployment descriptor for this application
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>register</display-name>
<description>DeployWizard autogenerated deploy description</description>
<context-param>
<param-name>execution-environment</param-name>
<param-value>eclipse</param-value>
<description>
Describe the current execution environment to the program.
</description>
</context-param>
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>com.heirloomcomputing.ecs.run.servlet</servlet-class>
<init-param>
<param-name>servlet</param-name>
<param-value>register</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
[CTRL-D]
$ jar cf register.war register.html register.cbl WEB-INF # creates the .war deploy package

Deploying the Application Package

Deployment of the application package can be done via Tomcat's Web Application Manager. This was the application we started in the browser to validate that Tomcat was running.

  • In the browser window, scroll down to "Deploy" section.
  • Enter "/register" in "Context Path (required)".
  • Enter "/home/pi/ec4rpi/samples/servlet-register/resources/register.war" in "WAR or Directory URL".
  • Click "Deploy".
  • You should now see a message at the top of the Web Application Manager that states:

            [ Message: ][ OK - Deployed application at context path /register ]

  • Enter "http://localhost:8080/register/register.html" in the browser's address bar and hit enter -- this is the web form that is going to invoke the Elastic COBOL register servlet.
  • You can fill out the form (required fields are in bold), or you can simply scroll down to the bottom of the form and click "Register Software".

If you have not filled out all the required forms you should see a response from the Elastic COBOL register servlet that looks similar to this:

Capture.PNG

You can click on "register.cbl" at the top of the page to view the application source in the browser.

Updating the Application

A fast alternative to deploying via Tomcat's Web Application Manager, is to "hot deploy". This involves copying the application package (.war) directly into Tomcat's deployment directory. Tomcat will recognize this, and automatically undeploy the old application and then deploy the new one. This can be very useful when developing an application in an iterative manner. In your command console:

$ cd $HOME/ec4rpi/samples/servlet-register/resources
$ vi register.cbl # make any change -- e.g. change the contents of the data item SOMEONE.
$ ecc register
Elastic COBOL V12.6.27 Copyright (C) 2010-2012 Heirloom Computing

LOC: 439 (42 variables in 3 records)
Warnings: 0
Errors: 0
Result: Compilation SUCCESSFUL
Building: Class files
$ cp reg*class WEB-INF/classes # update the application code
$ jar cf register.war register.html register.cbl WEB-INF # rebuild the deploy package
$ sudo cp register.war $HOME/tomcat7/webapps # push the updated app to the hot deploy dir

Give Tomcat a minute or so to update the application, and then enter "http://localhost:8080/register/register.html" in the browser's address bar and hit enter to start the updated application.

This time when you click "Register Software" on the form, if you changed the contents of the SOMEONE data item, this should be visible on the response from the Elastic COBOL register servlet.