Deploying a web application using the Java Servlet API
Deploying a web application using the Java Servlet API
Learn how to use the serverless technology and Java Servlet API to create a simple web application for managing a to-do list.
To create a web app:
- Get your cloud ready.
- Set up your environment.
- Create a Yandex Object Storage bucket.
- Create a YDB.
- Create Yandex Cloud Functions.
- Create an API gateway.
- Test your application.
If you no longer need the resources you created, delete them.
Getting started
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. 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 create or select a folder for your infrastructure on the cloud page
Learn more about clouds and folders here.
Required paid resources
The cost of resources for web application support includes:
- Fee for the number of requests to the API gateway and outgoing 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 for the function, and outgoing traffic (see Cloud Functions pricing).
Set up your environment
-
Create a service account and assign it the following roles for your folder:
-
Clone the repository
with the project:git clone https://github.com/yandex-cloud-examples/yc-serverless-servlet -
Add the contents of the
yc-serverless-servletlocal repository you cloned to theservlet.ziparchive. You will need this archive later to create functions in Yandex Cloud Functions.
Create an Yandex Object Storage bucket
Create a bucket and upload index.html to it:
-
In the management console
, select the folder where you want to create a bucket. -
Navigate to Object Storage.
-
Click Create bucket, and in the window that opens:
- Enter a bucket name consistent with the naming conventions.
- Limit the maximum bucket size, if required.
- In the Read objects, Read object list, and Read settings fields, select
With authorization. - Select the default storage class.
- Click Create bucket.
-
Select the bucket you created.
-
Click
Upload in the top-right corner of the page. In the project's folder, selectsrc/main/resources/index.html. -
Select the storage class for the file and click Upload.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder used by default is the one specified when creating the CLI profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also specify a different folder for any command using --folder-name or --folder-id. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.
-
To create a bucket, run this command, specifying a unique bucket name:
yc storage bucket create \ --name <bucket_name> \ --default-storage-class standardResult:
name: sample-bucket folder_id: b1g5bhjofg7o******** anonymous_access_flags: {} default_storage_class: STANDARD versioning: VERSIONING_DISABLED created_at: "2026-06-01T04:44:30.688587Z" resource_id: e3eqmpmknkus********For more information about the
yc storage bucket createcommand, see the CLI reference. -
To upload the
index.htmlfile to the bucket, run this command:yc storage s3api put-object \ --bucket <bucket_name> \ --key index.html \ --body "<local_path_to_file>" \ --content-type "text/html"Where:
--bucket: Name of the bucket you created earlier.--body: Local path toindex.htmlyou pulled from the repository. Here is an example:/Users/myuser/repositories/yc-serverless-servlet/src/main/resources/index.html.
Result:
etag: '"f38989ca9039c275f5f56840********"' request_id: 72d413fc********For more information about the
yc storage s3api put-objectcommand, see the CLI reference.
Create a Yandex Managed Service for YDB database
-
Create a database in serverless mode:
- In the management console
, select the folder where you created the bucket. - Navigate to Managed Service for YDB.
- Click Create a database.
- Specify the database Name.
- Under Database type, select
Serverless. - Click Create a database.
- Wait for the database to start. While being created, your database will have the
Provisioningstatus. Once it is ready for use, its status will change toRunning. - Select the created database.
- Under Connection, find the Endpoint field and save its value. You will need it later to create functions.
- In the management console
-
Create a table named
Tasks:-
To open the database root directory, in the left-hand menu, select
Navigation. -
To query your database, click New SQL query in the top-right corner. On the Query page that opens:
-
In the Query field, enter:
CREATE TABLE Tasks ( TaskId Utf8, Name Utf8, Description Utf8, CreatedAt Datetime, PRIMARY KEY (TaskId) ); -
Click
Run.
-
-
-
Create a database in serverless mode, specifying its name in this command:
yc ydb database create <DB_name> \ --serverlessResult:
done (22s) id: etnm3b9gco6k******** folder_id: b1g5bhjofg7o******** created_at: "2026-06-01T04:54:48Z" name: sample-database status: PROVISIONING endpoint: grpcs://ydb.serverless.yandexcloud.net:2135/?database=/ru-central1/b1gj9ja2h4ct********/etnm3b9gco6k******** serverless_database: storage_size_limit: "53687091200" location_id: ru-central1 backup_config: backup_settings: - name: daily backup_schedule: daily_backup_schedule: execute_time: hours: 17 backup_time_to_live: 604800s type: SYSTEM document_api_endpoint: https://docapi.serverless.yandexcloud.net/ru-central1/b1gj9ja2h4ct********/etnm3b9gco6k******** monitoring_config: {}Save the value of the
endpointfield. You will need this name at the next steps.For more information about the
yc ydb database createcommand, see the CLI reference. -
Install
theYDB CLIutility for managing YDB databases. -
Save the IAM token for authentication of queries via
YDB CLIto theIAM_TOKENenvironment variable:export IAM_TOKEN=$(yc iam create-token) -
Create a table named
Tasks:ydb \ --endpoint grpcs://ydb.serverless.yandexcloud.net:2135 \ --database "/ru-central1/b1gj9ja2h4ct********/etnjkrvloo7k********" \ scripting yql -s \ "CREATE TABLE Tasks ( TaskId Utf8, Name Utf8, Description Utf8, CreatedAt Datetime, PRIMARY KEY (TaskId) );"Where:
--endpoint: First part of theendpointfield value saved when creating the database (preceding/?database=), e.g.,grpcs://ydb.serverless.yandexcloud.net:2135.--database: Second part of theendpointfield value (following/?database=). Here is an example:/ru-central1/b1gj9ja2h4ct********/etnjkrvloo7k********.
Create Cloud Functions
Create a function for each servlet:
- In the management console
, navigate to the folder where you created the bucket and the database. - Navigate to Cloud Functions.
- Click Create function.
- Specify
add-taskfor the function Name and, optionally, add a Description. - Click Create.
- Under Editor, select
Java 21as the runtime, disable Add files with code examples, and click Continue. - Prepare the function code. To do this, select
ZIP archivein the Code source field. - In the File field, click Attach file and select
servlet.zipyou created earlier. - In the Entry point field, enter
yandex.cloud.examples.serverless.todo.AddTaskServlet. - Set Timeout to
10. - In the Service account field, select the account you created when setting up your environment.
- Add these environment variables:
ENDPOINT: Enter the first part of the Endpoint field value you saved when creating a YDB database (the one betweengrpcs://and/?database=), e.g.,ydb.serverless.yandexcloud.net:2135.DATABASE: Enter the second part of the Endpoint field value (following/?database=), e.g.,/ru-central1/r1gra875baom********/g5n22e7ejfr1********.
- Click Save changes and wait for the function version to compile.
- On the Overview page that opens, enable Public function.
- Repeat steps 3 through 14 to create a function named
list-taskswith theyandex.cloud.examples.serverless.todo.ListTasksServletentry point. - Repeat steps 3 through 14 to create a function named
delete-taskwith theyandex.cloud.examples.serverless.todo.DeleteTaskServletentry point.
-
Create a function named
add-task:yc serverless function create \ --name add-taskResult:
id: d4e3reah34e0******** folder_id: b1g5bhjofg7o******** created_at: "2026-06-01T05:07:13.071Z" name: add-task http_invoke_url: https://functions.yandexcloud.net/d4e3reah34e0******** status: ACTIVESave the
idof your new function. You will need it later to create an API gateway.For more information about the
yc serverless function createcommand, see the CLI reference. -
Create a version of the
add-taskfunction:yc serverless function version create \ --function-name add-task \ --runtime java21 \ --service-account-id <service_account_ID> \ --entrypoint "yandex.cloud.examples.serverless.todo.AddTaskServlet" \ --memory 128m \ --execution-timeout 10s \ --source-path "./servlet.zip" \ --environment ENDPOINT="<YDB_endpoint>",DATABASE="<DB_name>"Where:
-
--function-name: Name of the function whose version you want to create. -
--runtime: Runtime. -
--service-account-id: ID of the service account you created when setting up your environment. -
--entrypoint: Entry point in<function_file_name>.<handler_name>format. -
--memory: RAM amount available to the function. -
--execution-timeout: Maximum function execution time before timeout. -
--source-path: Local path to the previously createdservlet.ziparchive with the function code and required dependencies. -
--environment: Environment variables inkey=valueformat:ENDPOINT: First part of theendpointfield value you saved when creating your database (the one betweengrpcs://and/?database=), e.g.,ydb.serverless.yandexcloud.net:2135.DATABASE: Second part of theendpointfield value (following/?database=). Here is an example:/ru-central1/b1gj9ja2h4ct********/etnjkrvloo7k********.
Result:
done (52s) id: d4eve0ahaf3e******** function_id: d4e3reah34e0******** created_at: "2026-06-01T05:18:34.900Z" ... log_options: folder_id: b1g5bhjofg7o********For more information about the
yc serverless function version createcommand, see the CLI reference. -
-
Make the function public:
yc serverless function allow-unauthenticated-invoke add-taskFor more information about the
yc serverless function allow-unauthenticated-invokecommand, see the CLI reference. -
Repeat steps 1 through 3 to create a function named
list-taskswith theyandex.cloud.examples.serverless.todo.ListTasksServletentry point. -
Repeat steps 1 through 3 to create a function named
delete-taskwith theyandex.cloud.examples.serverless.todo.DeleteTaskServletentry point.
Create an API gateway
To enable interaction between services, create an API gateway:
-
In the management console
, select the folder where you created your bucket, database, and functions. -
Navigate to API Gateway.
-
Click Create API gateway.
-
Specify the gateway Name and, optionally, add a Description.
-
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_name> object: index.html presigned_redirect: false service_account_id: <service_account_ID> operationId: static /add: post: x-yc-apigateway-integration: type: cloud-functions function_id: <add-task_function_ID> operationId: addTask /list: get: x-yc-apigateway-integration: type: cloud-functions function_id: <list-tasks_function_ID> operationId: listTasks /delete: delete: x-yc-apigateway-integration: type: cloud-functions function_id: <delete-task_function_ID> operationId: deleteTaskWhere:
bucket: Name of the bucket to which you uploaded theindex.htmlfile.service_account_id: ID of the service account you created when setting up your environment./addsection,function_idparameter:add-taskfunction ID./listsection,function_idparameter:list-tasksfunction ID./deletesection,function_idparameter:delete-taskfunction ID.
-
Click Create.
-
In the list of API gateways that opens, click the one you created and save its URL (the Default domain field value).
-
Create a file named
todo.yamland save the following specification to it:openapi: 3.0.0 info: title: ToDo list version: 1.0.0 paths: /: get: x-yc-apigateway-integration: type: object-storage bucket: <bucket_name> object: index.html presigned_redirect: false service_account_id: <service_account_ID> operationId: static /add: post: x-yc-apigateway-integration: type: cloud-functions function_id: <add-task_function_ID> operationId: addTask /list: get: x-yc-apigateway-integration: type: cloud-functions function_id: <list-tasks_function_ID> operationId: listTasks /delete: delete: x-yc-apigateway-integration: type: cloud-functions function_id: <delete-task_function_ID> operationId: deleteTaskWhere:
bucket: Name of the bucket to which you uploaded theindex.htmlfile.service_account_id: ID of the service account you created when setting up your environment./addsection,function_idparameter:add-taskfunction ID you saved earlier./listsection,function_idparameter:list-tasksfunction ID you saved earlier./deletesection,function_idparameter:delete-taskfunction ID you saved earlier.
-
Create your API gateway by specifying the local path to the
todo.yamlfile you created earlier in this command:yc serverless api-gateway create \ --name todo-list \ --spec "./todo.yaml" \ --description "simple todo list"Result:
done (2s) id: d5d9ut8546r7******** folder_id: b1g5bhjofg7o******** created_at: "2026-06-01T05:49:08.648Z" name: todo-list description: simple todo list status: ACTIVE domain: d5d9ut8546r7********.wnq2w1o5.apigw.yandexcloud.net connectivity: {} log_options: folder_id: b1g5bhjofg7o******** execution_timeout: 300sSave the domain name (the
domainfield value) of your new API gateway. The gateway will be available athttps://<domain_name>.For more information about the
yc serverless api-gateway createcommand, see the CLI reference.
Test the application
To open the application, navigate to your new API gateway's URL in your browser. If everything is configured correctly, you will be able to create, view, and delete tasks in the application that opens.
How to delete the resources you created
To stop paying for the resources you created:
- Delete the API gateway.
- Delete the functions.
- Delete the database.
- Delete the
index.htmlfile, then delete the bucket.
The naming requirements are as follows:
- Length: between 3 and 63 characters.
- It can only contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
The bucket name must be unique within Yandex Object Storage. For more information, see Naming buckets.
Yandex Object Storage allows you to store objects in storages of various classes depending on how long you want to store your objects and how often you access them. For more information, see Storage class.
Serverless YDB databases require zero configuration, administration, load monitoring, or resource management from the user. For more information, see Serverless and dedicated operation modes.
With Yandex Cloud Functions functions, you can place your code in Yandex Cloud and run on request or trigger. For more information, see Function.
A bucket is a portion of Yandex Object Storage allocated for user data. For more information, see Bucket in Object Storage.
An API gateway is an interface for working with services on the internet or within Yandex Cloud, set in a declarative way using a specification. For more information, see Resource relationships in API Gateway.
A role is a set of permissions that defines the allowed scope of operations with Yandex Cloud resources. For more information, see Roles.