Export (0) Print
Expand All

Using the Azure Service Runtime Library in JSP

Updated: September 25, 2014

The following steps show you how to create and deploy a basic JSP application to Azure using the Azure Toolkit for Eclipse with Java (by Microsoft Open Technologies). A JSP example is shown for simplicity, but highly similar steps would be appropriate for a Java servlet, as far as Azure deployment is concerned.

The application will use functionality from the Azure Service Runtime library and will look similar to the following:

Hello World with Service Runtime

  1. Add the Azure Service Runtime library to your build path. Within Eclipse’s Project Explorer, right-click the MyHelloWorld project and click Properties.

  2. In the left-hand pane of the Properties dialog, click Java Build Path.

  3. Click the Libraries tab, and then click Add Library.

  4. In the Add Library dialog, click Package for Azure Libraries for Java (by MS Open Tech) and then click Next.

  5. Check Include in the project deployment assembly, and then click Finish.

  6. Click OK to close the Properties dialog.

  7. Modify the code in index.jsp, to include an import statement for the Service Runtime library, and also add in some functionality that uses this library. The following shows an index.jsp file that includes the import statement, and includes calls into the Service Runtime library.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1" import="com.microsoft.windowsazure.serviceruntime.*, java.util.Map" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     <b>Hello World!</b>
     <p>RoleEnvironment.isEmulated() returned <%=RoleEnvironment.isEmulated() %>.</p>
     <p>The ID of the RoleInstance is <%= RoleEnvironment.getCurrentRoleInstance().getId() %>.</p>
     <p>The name of the Role is <%= RoleEnvironment.getCurrentRoleInstance().getRole().getName() %>.</p>
        Map<String, RoleInstanceEndpoint> mapEndpoints = 
        for (Map.Entry<String, RoleInstanceEndpoint> entry : mapEndpoints.entrySet())
             out.println("The endpoint named '" + entry.getKey() + 
                 "' has value " + entry.getValue().getIpEndPoint().getAddress().getHostAddress() + ".");

    The page directive includes the import statement for the Service Runtime classes (as well as for java.util.Map, because one of the methods, RoleInstance.getInstanceEndpoints, returns a Map object). Several other Service Runtime methods are used. For example, RoleEnvironment.isEmulated returns a value that indicates whether the role environment is running in the compute emulator, and RoleEnvironment.getCurrentRoleInstance returns a RoleInstance object that represents the role instance in which the code is running.

  8. Save index.jsp. In the Azure toolbar, click the Run in Azure Emulator icon to launch your application in the Azure emulator. For additional information on running in the Azure emulator, see Creating a Hello World Application for Azure in Eclipse. That topic also describes how to deploy your application to Azure.

For additional information about the Service Runtime package, and other packages from Azure Libraries for Java, see http://dl.windowsazure.com/javadoc.

© 2014 Microsoft