Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Tutorials
    • All tutorials
    • URL shortener
    • Entering data into storage systems
    • Storing application runtime logs
    • Deploying a web application using the Java Servlet API
    • Developing a Slack bot
    • Developing a Telegram bot
    • Developing a custom integration in API Gateway
    • Developing CRUD APIs for movie services
    • Building a CI/CD pipeline in GitLab
    • Working with an API gateway via WebSocket
    • Creating an interactive serverless application using WebSocket
    • Automatically copying objects from one Object Storage bucket to another
    • Visualizing logs in Grafana using the Cloud Logging plugin
    • Canary release of a Cloud Functions function
    • Interactive debugging of Cloud Functions functions
    • Creating a Node.js function using TypeScript
    • Running a containerized app in Serverless Containers
    • Streaming Yandex Cloud Postbox events to Data Streams and analyzing them using DataLens
    • Using API Gateway to set up speech synthesis in SpeechKit
    • Connecting to YDB from a Cloud Functions function in Python
    • Connecting to a YDB database from a Cloud Functions function in Node.js
    • API Gateway protection with Smart Web Security
    • Deploying a web app with JWT authorization in API Gateway and authentication in Firebase
    • Automatic data upload to Yandex SpeechSense using Yandex Workflows
    • Configuring responses in Cloud Logging and Yandex Cloud Functions
    • Setting up Workflows integration with Tracker, YandexGPT, and Yandex Cloud Postbox
    • Developing functions in Functions Framework and deploying them to Yandex Serverless Containers

In this article:

  • Getting started
  • Required paid resources
  • Set up your environment
  • Create an Object Storage bucket
  • Create a Managed Service for YDB database
  • Create Cloud Functions functions
  • Create an API gateway
  • Test the application
  • How to delete the resources you created
  1. Serverless technologies
  2. Deploying a web application using the Java Servlet API

Deploying a web application using the Java Servlet API

Written by
Yandex Cloud
Improved by
Danila N.
Updated at May 7, 2025
  • Getting started
    • Required paid resources
  • Set up your environment
  • Create an Object Storage bucket
  • Create a Managed Service for YDB database
  • Create Cloud Functions functions
  • Create an API gateway
  • Test the application
  • How to delete the resources you created

Learn how to use serverless technologies and the Java Servlet API to create a simple web application for managing a task list.

To create a web application:

  1. Get your cloud ready.
  2. Set up your environment.
  3. Create an Yandex Object Storage bucket.
  4. Create a YDB database.
  5. Create Yandex Cloud Functions functions.
  6. Create an API gateway.
  7. Test your application.

If you no longer need the resources you created, delete them.

Getting startedGetting started

Sign up in Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or register a new account.
  2. On the Yandex Cloud Billing page, make sure you have a billing account linked and it has the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

If you have an active billing account, you can navigate to the cloud page to create or select a folder for your infrastructure to operate in.

Learn more about clouds and folders.

Required paid resourcesRequired paid resources

The cost of resources to support a web application includes:

  • Fee for the number of requests to the API gateway and outbound traffic (see Yandex API Gateway pricing).
  • Fee for YDB operations and data storage (see Yandex Managed Service for YDB pricing).
  • Fee for the number of function calls, computing resources allocated to a function, and outbound traffic (see Cloud Functions pricing).

Set up your environmentSet up your environment

  1. Create a service account and assign it the editor role for your folder.

  2. Clone the repository containing the project:

    git clone https://github.com/yandex-cloud-examples/yc-serverless-servlet
    
  3. Add the contents of the yc-serverless-servlet local repository you cloned to the servlet.zip archive. You will need this archive later to create Yandex Cloud Functions functions.

Create an Object Storage bucketCreate an Object Storage bucket

Create a bucket and upload index.html there:

Management console
  1. In the management console, select the folder where you want to create a bucket.
  2. Select Object Storage.
  3. Click Create bucket.
  4. On the bucket creation page:
    1. Enter a name for the bucket as per the naming requirements.
    2. Limit the maximum bucket size, if required.
    3. In the Object read access, Object listing access, and Read access to settings fields, select Restricted.
    4. Select the default storage class.
    5. Click Create bucket to complete the operation.
  5. Select the bucket you created.
  6. Click Upload and select the src/main/resources/index.html file in the project folder.
  7. Select the storage class for the file and click Upload.

Create a Managed Service for YDB databaseCreate a Managed Service for YDB database

  1. Create a database in serverless mode:

    Management console
    1. In the management console, select the folder where you created the bucket.

    2. Select Managed Service for YDB.

    3. Click Create a database.

    4. Enter the database Name. The naming requirements are as follows:

      • It must be from 2 to 63 characters long.
      • It may contain lowercase Latin letters, numbers, and hyphens.
      • It must start with a letter and cannot end with a hyphen.
    5. Under Database type, select Serverless.

    6. Click Create a database.

    7. Wait until the database starts. While being created, your database will have the Provisioning status. Once it is ready for use, its status will change to Running.

    8. Select the database created.

    9. Under Connection, find the Endpoint field and save its value. You will need it when creating functions.

  2. Create a table named Tasks:

    Management console
    CLI
    1. In the management console, select the folder where you created the database.

    2. Select Managed Service for YDB.

    3. Select a database on the Databases page.

    4. To open the DB root directory, navigate to the Navigation tab.

    5. To make a query to the database, click New SQL query in the top-right corner. The Query page will open.

    6. In the Query field, enter:

      CREATE TABLE Tasks (
        TaskId Utf8,
        Name Utf8,
        Description Utf8,
        CreatedAt Datetime,
        PRIMARY KEY (TaskId)
      );
      
    7. Click Run.

    Run this request:

    ydb -e grpcs://<YDB_endpoint> -d <DB_name> \
    scripting yql -s \
    "CREATE TABLE Tasks
    (
      TaskId Utf8,
      Name Utf8,
      Description Utf8,
      CreatedAt Datetime,
      PRIMARY KEY (TaskId)
    );"
    

Create Cloud Functions functionsCreate Cloud Functions functions

Create a function for each servlet:

Management console
CLI
API
Yandex Cloud Toolkit
  1. In the management console, navigate to the folder where you created the bucket and database.
  2. Select Cloud Functions.
  3. Click Create function.
  4. Enter a name, e.g., add-task, and description for the function.
  5. Click Create.
  6. Under Editor, select the java21 runtime environment, disable Add files with code examples, and click Continue.
  7. Prepare the function code. To do this, select ZIP archive in the Method field.
  8. In the File field, click Attach file and select the servlet.zip archive you created earlier.
  9. In the Entry point field, enter yandex.cloud.examples.serverless.todo.AddTaskServlet.
  10. Set Timeout to 10.
  11. In the Service account field, enter the account you created when setting up your environment.
  12. Add these environment variables:
    • ENDPOINT: Enter the first part of the Endpoint field value saved when creating the YDB database (the one between grpcs:// and /?database=), e.g., ydb.serverless.yandexcloud.net:2135.
    • DATABASE: Enter the second part of the Endpoint field value saved when creating the YDB database (the one following /?database=), e.g., /ru-central1/r1gra875baom********/g5n22e7ejfr1********.
  13. Click Save changes.
  14. On the Overview page, enable Public function.
  15. Repeat steps 3-14 and create a function named list-tasks with the yandex.cloud.examples.serverless.todo.ListTasksServlet entry point.
  16. Repeat steps 3-14 and create a function named delete-task with the yandex.cloud.examples.serverless.todo.DeleteTaskServlet entry point.
  1. Create a function named add-task:

    yc serverless function create --name=add-task
    

    Result:

    id: d4ejb1799eko********
    folder_id: aoek49ghmknn********
    created_at: "2021-03-08T14:07:32.134Z"
    name: add-task
    log_group_id: eolm8aoq9vcp********
    http_invoke_url: https://functions.yandexcloud.net/d4ejb1799eko********
    status: ACTIVE
    
  2. Create a version of the add-task function:

    yc serverless function version create \
      --function-name=add-task \
      --runtime java21 \
      --entrypoint yandex.cloud.examples.serverless.todo.AddTaskServlet \
      --memory 128m \
      --execution-timeout 10s \
      --source-path ./servlet.zip \
      --environment DATABASE=<DB_name>,ENDPOINT=<YDB_endpoint>
    

    Where:

    • --function-name: Name of the function whose version you want to create.
    • --runtime: Runtime environment.
    • entrypoint: Entry point in <function_file_name>.<handler_name> format.
    • --memory: Amount of RAM.
    • --execution-timeout: Maximum running time of the function until timeout.
    • --source-path: Path to the previously created servlet.zip archive with the function code and required dependencies.
    • --environment: Environment variables in key=value format.

    Result:

    done (1s)
    id: d4evvn8obisa********
    function_id: d4ejb1799eko********
    ...
    tags:
    - $latest
    log_group_id: ckg3qh8h363p********
    
  3. Make the function public:

    yc serverless function allow-unauthenticated-invoke add-task
    

    Result:

    done (1s)
    
  4. Repeat steps 1-3 and create a function named list-tasks with the yandex.cloud.examples.serverless.todo.ListTasksServlet entry point.

  5. Repeat steps 1-3 and create a function named delete-task with the yandex.cloud.examples.serverless.todo.DeleteTaskServlet entry point.

Use the create, createVersion, and setAccessBindings API methods for the Function resource.

You can create a function and its version using the Yandex Cloud Toolkit plugin for the IDE family on the JetBrains IntelliJ platform.

Create an API gatewayCreate an API gateway

To ensure interaction between the services, create an API gateway:

Management console
CLI
Yandex Cloud Toolkit
  1. In the management console, select the folder where you created your bucket, database, and functions.

  2. Select API Gateway.

  3. Click Create API gateway.

  4. Enter a name and description for the gateway.

  5. In the Specification field, add this specification:

    openapi: 3.0.0
    info:
      title: ToDo list
      version: 1.0.0
    paths:
      /:
        get:
          x-yc-apigateway-integration:
            type: object-storage
            bucket: <bucket>
            object: index.html
            presigned_redirect: false
            service_account: <service_account>
          operationId: static
      /add:
         post:
           x-yc-apigateway-integration:
             type: cloud-functions
             function_id: <add-task_ID>
           operationId: addTask
      /list:
        get:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <list-tasks_ID>
          operationId: listTasks
      /delete:
        delete:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <delete-task_ID>
          operationId: deleteTask
    

    Where:

    • bucket: Name of the bucket containing index.html.
    • service_account: ID of the service account you created when setting up your environment.
    • /add section, function_id parameter: add-task function ID.
    • /list section, function_id parameter: list-tasks function ID.
    • /delete section, function_id parameter: delete-task function ID.
  6. Click Create.

  1. Save the following specification to the todo.yaml file:

    openapi: 3.0.0
    info:
      title: ToDo list
      version: 1.0.0
    paths:
      /:
        get:
          x-yc-apigateway-integration:
            type: object-storage
            bucket: <bucket>
            object: index.html
            presigned_redirect: false
            service_account: <service_account>
          operationId: static
      /add:
        post:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <add-task_ID>
          operationId: addTask
      /list:
        get:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <list-tasks_ID>
          operationId: listTasks
      /delete:
        delete:
          x-yc-apigateway-integration:
            type: cloud-functions
            function_id: <delete-task_ID>
          operationId: deleteTask
    

    Where:

    • bucket: Name of the bucket containing index.html.
    • service_account: ID of the service account you created when setting up your environment.
    • /add section, function_id parameter: add-task function ID.
    • /list section, function_id parameter: list-tasks function ID.
    • /delete section, function_id parameter: delete-task function ID.
  2. Create an API gateway:

    yc serverless api-gateway create \
      --name todo-list \
      --spec=todo.yaml \
      --description "simple todo list"
    

    Result:

    done (41s)
    id: d5delqeel34q********
    folder_id: b1g86q4m5vej********
    created_at: "2021-03-08T14:07:32.134Z"
    name: todo-list
    description: simple todo list
    status: ACTIVE
    domain: d5dm1lba80md********.i9******.apigw.yandexcloud.net
    log_group_id: ckg2hdmevnvc********
    

You can create an API gateway using the Yandex Cloud Toolkit plugin for the IDE family on the JetBrains IntelliJ platform.

Test the applicationTest the application

To open the app, follow the link in the Default domain field of the API gateway you created.

How to delete the resources you createdHow to delete the resources you created

To stop paying for the resources you created:

  • Delete the bucket.
  • Delete the database.
  • Delete the functions.
  • Delete the API gateway.

Was the article helpful?

Previous
Storing application runtime logs
Next
Developing a Slack bot
Yandex project
© 2025 Yandex.Cloud LLC