Esporta (0) Stampa
Espandi tutto

Using the Windows Azure Service Runtime Library in JSP

Aggiornamento: gennaio 2014

The following steps show you how to create and deploy a basic JSP application to Windows Azure using the Windows Azure Plugin 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 Windows Azure Service Runtime library and will look similar to the following:

Hello World con runtime del servizio


To use the Service Runtime library in JSP

  1. Add the Windows 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 Windows 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="*, java.util.Map" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
    <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 Windows Azure toolbar, click the Run in Windows Azure Emulator icon to launch your application in the Windows Azure emulator. For additional information on running in the Windows Azure emulator, see Creating a Hello World Application for Windows Azure in Eclipse. That topic also describes how to deploy your application to Windows Azure.

For additional information about the Service Runtime package, and other packages from Windows Azure Libraries for Java, see

© 2014 Microsoft