Tagged: html

Android embedded html and java servlet engine aka JetSet Server

Recently I needed a back-end engine to drive my 3D design program I mashed-up (OpenJscad + bootstrap + backend save)  So on my Ubuntu machine I used Tomcat and with the usual spring sauce.

While this was nice an easy, I didn’t won’t to leave my laptop running  as a server, since my wife also wants to use this 3D design webapp.

Well why not using one of the Android boards available, there cheap and don’t use much power, which i can’t say from my Intel I7 ? (APC Rock, Olimex Olinuxino, Beaglebone, …)

So I remembered having written a servlet engine based upon some old 1998 opensource code I had saved on a drive.

It took me a weekend to rewrite the  core code to run it as an Android app. Ok the code isn’t the best written but it servers it’s purpose, a lightweight servlet and html engine, ready to be embedded on an Android board.

So Jetset Server was born, I tested the apk on the Olimex board I had lying around, made some config in the WIFI router, alas it took me a few hours to figure out how to put the board in the DMZ, but at the end the webserver was up and running and available on the internet from outside thanks to an no-ip dynamic domain name. The Olimex uses WIFI to connect to the router, since the installed app to sync the Androids dynamic IP address with the no-ip service connects a lot, I had to disable it to prevent locking my no-ip account.

ScreenshotJetSetEclipse

Now that the server was running I needed some simple admin webapp to configure the server, heck I needed a template engine and a UI framework. Well I quickly adopted Twitters bootstrap as the UI framework including JQuery and found a simple and effective template engine  in ‘MiniTemplator‘ for the java backend.

ScreenshotJetSetConfig_crop

Here’s an example snippet:

		<form action="admin.html" method="post">
			<fieldset>
				<legend>Configuration</legend>
				<label>Port</label> <input type="text"
					placeholder="server port number" id="port" name="port" value="${port}"> <span
					class="help-block">Port number where the server is listening
					on. Default is port 9090.</span>

				<button type="submit" class="btn">Save</button>
			</fieldset>
		</form>

Using the MVC pattern the object values are inserted in the template with the ${objectname}.

The servlet code:

package be.wodan.fhs;

import java.io.*;

import android.app.Service;
import android.content.SharedPreferences;
import be.wodan.fhs.publisher.Context;
import be.wodan.fhs.servlet.*;
import be.wodan.fhs.servlet.http.*;
import be.wodan.fhs.template.MiniTemplator;

public class AdminServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8338686139653151705L;
	public static final String PREFS_NAME = "JetSetPrefs";
	private Service service;
	private Context context;

	AdminServlet(Service service, Context context) {
		this.service = service;
		this.context = context;
	}

	// / Returns a string containing information about the author, version, and
	// copyright of the servlet.
	public String getServletInfo() {
		return "admin servlet";
	}

	// / Services a single request from the client.
	// @param req the servlet request
	// @param req the servlet response
	// @exception ServletException when an exception has occurred
	public void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		log("admin called");
		MiniTemplator template = new MiniTemplator(context.getDocumentPath()
				+ File.separator + "admin/admin.html");
		if (req.getMethod().equalsIgnoreCase("post")) {
			log("admin post");
			String port = (String) req.getParameter("port");

			log("admin port: " + port);
			// All objects are from android.context.Context
			SharedPreferences settings = service.getSharedPreferences(
					PREFS_NAME, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putInt("port", Integer.valueOf(port));

			// Commit the edits!
			editor.commit();
			template.setVariableOptEsc("port", String.valueOf(settings.getInt("port", 9090)));
			log("admin post end");
		} else {
			log("admin get");
			SharedPreferences settings = service.getSharedPreferences(
					PREFS_NAME, 0);
			template.setVariableOptEsc("port",
					String.valueOf(settings.getInt("port", 9090)));
			log("admin get end");

		}
		res.setStatus(HttpServletResponse.SC_OK);
		res.setContentType("text/html");
		ServletOutputStream p = res.getOutputStream();
		p.write(template.generateOutput().getBytes());
		p.flush();
		p.close();
	}

	public Service getService() {
		return service;
	}

	public void setService(Service service) {
		this.service = service;
	}

}

Future plans are:
- cleanup the code
- write a proper tutorial
- post it on github
- add a wikiservlet based upon my old wiki servlet.
- connect it to an Arduino board or IOIO for some cool stuff. 

Any suggestions or comments post a reply on this site.
Advertisements