This tutorial shows how to create and deploy a basic Hello World application to Azure as a Web App by using the Azure Toolkit for Eclipse. A basic JSP example is shown for simplicity, but similar steps would be appropriate for a Java servlet, as far as Azure deployment is concerned.
When you have completed this tutorial, your application will look similar to the following illustration when you view it in a web browser:
- A Java Developer Kit (JDK), v 1.8 or later.
- Eclipse IDE for Java EE Developers, Luna or later. This can be downloaded from http://www.eclipse.org/downloads/.
- A distribution of a Java-based web server or application server, such as Apache Tomcat or Jetty.
- An Azure subscription, which can be acquired from https://azure.microsoft.com/free/ or http://azure.microsoft.com/pricing/purchase-options/.
- The Azure Toolkit for Eclipse. For information about installing the Azure Toolkit, see Installing the Azure Toolkit for Eclipse.
To create a Hello World application
First, we'll start off with creating a Java project.
- Start Eclipse, and at the menu click File, click New, and then click Dynamic Web Project. (If you don't see Dynamic Web Project listed as an available project after clicking File and New, then do the following: click File, click New, click Project..., expand Web, click Dynamic Web Project, and click Next.)
For purposes of this tutorial, name the project MyWebApp. Your screen will appear similar to the following:
- Click Finish.
- Within Eclipse's Project Explorer view, expand MyWebApp. Right-click WebContent, click New, and then click JSP File.
- In the New JSP File dialog box, name the file index.jsp, keep the parent folder as MyWebApp/WebContent, and then click Next.
- In the Select JSP Template dialog box, for purposes of this tutorial select New JSP File (html), and then click Finish.
When your index.jsp file opens in Eclipse, add in text to dynamically display Hello World! within the existing
<body>element. Your updated
<body>content should resemble the following example:
<body><b><% out.println("Hello World!"); %></b></body>
- Save index.jsp.
To deploy your application to an Azure Web App Container
There are several ways by which you can deploy a Java web application to Azure. This tutorial describes one of the simplest: your application will be deployed to an Azure Web App Container - no special project type nor additional tools are needed. The JDK and the web container software will be provided for you by Azure, so there is no need to upload your own; all you need is your Java Web App. As a result, the publishing process for your application will take seconds, not minutes.
- In Eclipse's Project Explorer, right-click MyWebApp.
In the context menu, select Azure, then click Publish as Azure Web App...
Alternatively, while your web application project is selected in the Project Explorer, you can click the Publish dropdown button on the toolbar and select Publish as Azure Web App from there:
If you have not already signed into Azure from Eclipse, you will be prompted to sign into your Azure account:
If you have multiple Azure accounts, some of the prompts during the sign in process may be shown more than once, even if they appear to be the same. When this happens, continue following the sign in instructions.
After you have successfully signed into your Azure account, the Manage Subscriptions dialog box will display a list of subscriptions that are associated with your credentials. If there are multiple subscriptions listed and you want to work with only a specific subset of them, you may optionally uncheck the ones you do want to use. When you have selected your subscriptions, click Close.
When the Deploy to Azure Web App Container dialog box appears, it will display any Web App containers that you have previously created; if you have not created any containers, the list will be empty.
If you have not created an Azure Web App Container before, or if you would like to publish your application to a new container, use the following steps. Otherwise, select an existing Web App Container and skip to step 7 below.
The New Web App Container dialog box will be displayed:
- Enter a DNS Label for your Web App Container; this will form the leaf DNS label of the host URL for your web application in Azure. (Note that the name must be available and conform to Azure Web App naming requirements.)
In the Web Container drop-down menu, select the appropriate software for your application.
Currently, you can choose from Tomcat 8, Tomcat 7 or Jetty 9. A recent distribution of the selected software will be provided by Azure, and it will run on a recent distribution of JDK 8 created by Oracle and provided by Azure.
- In the Subscription drop-down menu, select the subscription you want to use for this deployment.
In the Resource Group drop-down menu, select the Resource Group with which you want to associate your Web App. (Azure Resource Groups allow you to group related resources together so that, for example, they can be deleted together.)
You can select an existing Resource Group (if you have any) and skip to step g below, or use the following these steps to create a new Resource Group:
- Click New...
The New Resource Group dialog box will be displayed:
- In the the Name textbox, specify a name for your new Resource Group.
- In the the Region drop-down menu, select the appropriate Azure data center location for your Resource Group.
OPTIONAL: By default, a recent distribution of Java 8 will be deployed by Azure automatically to your web app container as your JVM. However, you can specify a different version and distribution of the JVM if your Web App requires it. To specify the JDK for your Web App, click the JDK tab, and select one of the following options:
- Deploy the default JDK offered by Azure Web Apps service: This option will deploy a recent distribution of Java 8.
- Deploy a 3rd party JDK available on Azure: This option allows you to choose from the list of JDKs which are provided by Microsoft Azure.
Deploy my own JDK from this download location: This option allows you to specify your own JDK distribution, which must be packaged as a ZIP file and uploaded to either a publicly available download location or an Azure storage account for which you have access.
- Click OK.
The App Service Plan drop-down menu lists the app service plans that are associated with the Resource Group that you selected. (App Service Plans specify information such as the location of your Web App, the pricing tier and the compute instance size. A single App Service Plan can be used for multiple Web Apps, which is why it is maintained separately from a specific Web App deployment.)
You can select an existing App Service Plan (if you have any) and skip to step h below, or use the following these steps to create a new App Service Plan:
- Click New...
The New App Service Plan dialog box will be displayed:
- In the the Name textbox, specify a name for your new App Service Plan.
- In the the Location drop-down menu, select the appropriate Azure data center location for the plan.
- In the the Pricing Tier drop-down menu, select the appropriate pricing for the plan. For testing purposes you can choose Free.
- In the the Instance Size drop-down menu, select the appropriate instance size for the plan. For testing purposes you can choose Small.
Once you have completed all of the above steps, the New Web App Container dialog box should resemble the following illustration:
Click OK to complete the creation of your new Web App container.
Wait a few seconds for the list of the Web App containers to be refreshed, and your newly-created web app container should now be selected in the list.
You are now ready to complete the initial deployment of your Web App to Azure:
Click OK to deploy your Java application to the selected Web App container.
By default, your application will be deployed as a subdirectory of the application server. If you want it to be deployed as the root application, check the Deploy to root checkbox before clicking OK.
Next, you should see the Azure Activity Log view, which will indicate the deployment status of your Web App.
The process of deploying your Web App to Azure should take only a few seconds to complete. When your application ready, you will see a link named Published in the Status column. When you click the link, it will take you to your deployed Web App's home page.
Updating your web app
Updating an existing running Azure Web App is a quick and easy process, and you have two options for updating:
- You can update the deployment of an existing Java Web App.
- You can publish an additional Java application to the same Web App Container.
In either case, the process is identical and takes only a few seconds:
- In the Eclipse project explorer, right-click the Java application you want to update or add to an existing Web App Container.
- When the context menu appears, select Azure and then Publish as Azure Web App...
- Since you have already logged in previously, you will see a list of your existing Web App containers. Select the one you want to publish or re-publish your Java application to and click OK.
A few seconds later, the Azure Activity Log view will show your updated deployment as Published and you will be able to verify your updated application in a web browser.
Starting, stopping, or restarting an existing web app
To start or stop an existing Azure Web App container, (including all the deployed Java applications in it), you can use the Azure Explorer view.
If the Azure Explorer view is not already open, you can open it by clicking then Window menu in Eclipse, then click Show View, then Other..., then Azure, and then click Azure Explorer. If you have not previously logged in, it will prompt you to do so.
When the Azure Explorer view is displayed, use follow these steps to start or stop your Web App:
- Expand the Azure node.
- Expand the Web Apps node.
- Right-click the desired Web App.
When the context menu appears, click Start, Stop, or Restart. Note that the menu choices are context-aware, so you can only stop a running web app or start a web app which is not currently running.
For more information about the Azure Toolkits for Java IDEs, see the following links:
- Azure Toolkit for Eclipse
- Azure Toolkit for IntelliJ
For more information about using Azure with Java, see the Azure Java Developer Center.
For additional information about creating Azure Web Apps, see the Web Apps Overview.
If you want to get started with Azure App Service before signing up for an Azure account, go to Try App Service, where you can immediately create a short-lived starter web app in App Service. No credit cards required; no commitments.