Tutorial: Upload from Java, Download from JavaScript

by Max Rohde,

This tutorial describes how a simple unit of data, the text 'Hello, Java!' can be uploaded from a Java application and retrieved with a JavaScript application.

Upload from Java

This section lists all steps required to create a small application that store the text 'Hello, World' in the Appjangle cloud database.

Step 1: Download Client Libraries

First download the latest version of the Appjangle client libraries from this page.

Chose the 'Appjangle Client with Nextweb API'. The Nextweb API is more declarative in nature than the more low-level onedb API and therefore allows in most cases to write more concise code.

If you use Maven in your project, you can also use the provided Maven dependencies to link the library to your project.

Create a new project in your favorite IDE. This example will make use of eclipse.

Copy the downloaded .jar file into a your project directory (e.g. 'lib') and add the library to the classpath/ build path.

Step 3: Write App to Upload Data

Create a new class with the name 'Upload'. Add a Java main method to the class as follows:

import io.nextweb.Query;
import io.nextweb.Session;
import io.nextweb.jre.Nextweb;

public class Upload {

    public static void main(String\[\] args) {
        Session session = Nextweb.createSession();

        Query hello = session.seed().append("Hello, Java!");

        System.out.println("Created:\\n"+hello.get());

        session.close().get();
    }

}

Running this application should result in an output such as the following:

Created:
node("http://slicnet.com/seed1/seed1/2/1/2/h/sd/Hello\_\_1",
class java.lang.String)

You can now access the created node using the reported URI. For instance, by opening http://slicnet.com/seed1/seed1/2/1/2/h/sd/Hello__1

in a web browser.

Download from JavaScript

This section lists all steps required to create a small application that store the text 'Hello, World' in the Appjangle cloud database using JavaScript.

Step 1: Download and Extract Appjangle Bootstrap

Head over to github to download the Appjangle Bootstrap project or, even better, fork it if you are familiar with github.

https://github.com/appjangle/appjangle-bootstrap

Extract the project and open app.html in your faverioute HTML/JS editor.

Step 2: Write App to Download Data

Replace the text // Your JavaScript here with the following application:

<body>
    <script
        src="http://appjangle.com/js/v01/appjangle/appjangle.nocache.js">
    </script>

    <script>
        window.onNextwebOnedb = function() {
            var session = Nextweb.createSession();
            var hello = session
                    .node("http://slicnet.com/seed1/seed1/2/1/2/h/sd/Hello\_\_1");
            hello.get(function(node) {
                document.body
                        .appendChild(document.createTextNode(node.value()));
            });
        }
    </script>

</body>

Save app.html and close your editor.

Step 3: Upload App to Appjangle

Open the TextSync JAR file in the appjangle-bootstrap directory (for instance, by double clicking on the file if supported by your OS).

Drag and drop the file app.html onto the files list in the TextSync App and click [Synchronize].

Note: You will need to get an Appjangle account in order to use the TextSync App. All your uploaded applications will be stored under this account.

Step 4: Open Application

Open app.html again. In the beginning of the file, copy the URL to the right of one.upload .

Past the URL into your web browser. Add to the end of the URL .value.html and replace https with http at the beginning of the URL.

Loading the page should result in the output (also see an example deployed app here):

> Hello, Java

It's not 'Hello, JavaScript' since here we are loading the node created by the Java Hello, World application described above. Check out the Nextweb.io API docs to find out how to change the text stored in the Appjangle cloud to a more fitting 'Hello, JavaScript'.

This tutorial is also published on the Appjangle blog.

Tags: tutorial
Categories: javajavascript