Yandex Cloud
Search
Discuss with expertTry it for free
  • Customer Stories
  • Documentation
  • Blog
  • All Services
    • Cloud Interconnect
    • Cloud Backup
    • Cloud Registry
    • Yandex AI Studio
    • Compute Cloud
    • Object Storage
    • Managed Service for Kubernetes®
    • Yandex BareMetal
    • Smart Web Security
    • Security Deck
    • Managed Service for PostgreSQL
    • Managed Service for ClickHouse®
    • Monium
    • Cloud CDN
    • Network Load Balancer
    • Virtual Private Cloud
    • Cloud DNS
    • Application Load Balancer
    • Yandex Cloud Video
    • Stackland
    • Yandex Cloud Router
    • Yandex Managed Service for Trino
    • Managed Service for MySQL®
    • Managed Service for Valkey™
    • Managed Service for Apache Spark™
    • Yandex StoreDoc
    • Managed Service for OpenSearch
    • Managed Service for Apache Kafka®
    • Data Transfer
    • Yandex MPP Analytics Engine for PostgreSQL
    • Yandex Managed Service for Apache Airflow®
    • Data Processing
    • Yandex MetaData Hub
    • Managed Service for YDB
    • Managed Service for Sharded PostgreSQL
    • Managed Service for YTsaurus
    • Yandex WebSQL
    • DataLens
    • Yandex Search API
    • SpeechSense
    • SpeechKit
    • DataSphere
    • Vision OCR
    • Translate
    • Yandex Identity Hub
    • Key Management Service
    • Certificate Manager
    • Yandex Lockbox
    • Audit Trails
    • SmartCaptcha
    • Cloud Desktop
    • SourceCraft Code Assistant
    • Container Registry
    • Managed Service for GitLab
    • Managed Service for Prometheus®
    • Cloud Functions
    • API Gateway
    • Yandex Cloud Postbox
    • Message Queue
    • Serverless Integrations
    • IoT Core
    • Data Streams
    • Serverless Containers
    • Cloud Notification Service
    • Yandex Query
    • Identity and Access Management
    • Yandex Cloud Console
    • Resource Manager
    • Yandex Cloud Billing
    • Yandex Cloud Quota Manager
    • Cloud Apps
  • System Status
  • Marketplace
    • Featured
    • Infrastructure & Network
    • Data Platform
    • AI for business
    • Security
    • DevOps tools
    • Serverless
    • Monitoring & Resources
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Center for Technologies and Society
    • Yandex Cloud Partner program
    • Price calculator
    • Pricing plans
  • Customer Stories
  • Documentation
  • Blog
© 2026 Direct Cursus Technology L.L.C.
Yandex Cloud Functions
  • Comparing with other Yandex Cloud services
    • Overview
      • Overview
      • Function interface
      • YcFunction interface
      • HttpServlet class
      • Spring Boot
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Error handling
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes
  • FAQ

In this article:

  • Unsupported methods
  • Example of modeling how the function behaves for different HTTP methods
  • Example of function information output
  1. Developing in Java
  2. Programming model
  3. HttpServlet class

Using the HttpServlet class to set a Java handler

Written by
Yandex Cloud
Updated at July 2, 2026
View in Markdown
  • Unsupported methods
  • Example of modeling how the function behaves for different HTTP methods
  • Example of function information output

You can set a Java handler by overriding selected methods of the HttpServlet class.

Cloud Functions will automatically route each HTTP request to your handler depending on the HTTP method used to initiate the request. For example, a GET request will be routed to your handler’s doGet method, and POST, to doPost. For successful routing, these handler methods must exist; otherwise the function will return HTTP method XXX is not supported by this URL with code 405.

Unsupported methodsUnsupported methods

When using this model, note that Cloud Functions does not support some methods of the HttpServletRequest and HttpServletResponse classes.

HttpServletRequest:

  • getAuthType
  • getCookies
  • upgrade
  • authenticate
  • login
  • logout
  • getContextPath
  • getServletPath
  • getPathTranslated
  • getRealPath
  • getRemotePort
  • getLocalAddr
  • getLocalPort
  • getServerPort
  • isUserInRole
  • getUserPrincipal
  • getRequestedSessionId
  • getSession
  • changeSessionId
  • isRequestedSessionIdValid
  • isRequestedSessionIdFromCookie
  • isRequestedSessionIdFromURL
  • isRequestedSessionIdFromUrl

HttpServletResponse:

  • encodeURL
  • encodeRedirectURL
  • encodeUrl
  • encodeRedirectUrl

Example of modeling how the function behaves for different HTTP methodsExample of modeling how the function behaves for different HTTP methods

Warning

Do not use ?integration=raw to invoke this function. If you do, the function will not get any data about the original request’s methods, headers, or parameters.

Cloud Functions supports Maven, which is a dependency management system for Java.

To create a function:

  1. Create a file named Handler.java at /src/main/java/Handler.java and a file named pom.xml.
  2. Add the /src directory and the pom.xml file to a ZIP archive.
  3. Upload the ZIP archive to Cloud Functions.

pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.company.app</groupId>
    <artifactId>servlet</artifactId>
    <version>1</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

Handler.java:

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Handler extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // With Content-Type set to text/html, the browser renders the HTML code.
    response.setContentType("text/html");
    // It will be displayed in bold when the function is called from a browser.
    response.getOutputStream().print("<b>Hello, world. In bold.</b>");
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    var name = request.getParameter("name");
    response.getWriter().println("Hello, " + name);
    // Make sure to close the PrintWriter returned by getWriter.
    response.getWriter().close();
  }
}

Request examples:

GET method:

<b>Hello, world. In bold.</b>

Note

Running a GET request in a browser will display this string in bold without any tags since you specified Content-Type: text/html in your handler's code.

POST method, name=Anonymous as your request parameter:

Hello, Anonymous

PUT method:

HTTP method PUT is not supported by this URL

Example of function information outputExample of function information output

The following function outputs its metadata based on the invocation context.

pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.company.app</groupId>
    <artifactId>servlet</artifactId>
    <version>1</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

Handler.java:

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Handler extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestId = request.getHeader("Lambda-Runtime-Aws-Request-Id");
        String functionId = request.getHeader("Lambda-Runtime-Function-Name");
        String versionId = request.getHeader("Lambda-Runtime-Function-Version");
        String memoryLimit = request.getHeader("Lambda-Runtime-Memory-Limit");
        String tokenJson = request.getHeader("Lambda-Runtime-Token-Json");

        response.getOutputStream().print(String.format(
                "RequestID: %s\n" +
                "FunctionID: %s\n" +
                "VersionID: %s\n" +
                "MemoryLimit: %s\n" +
                "TokenJSON: %s",
                requestId,
                functionId,
                versionId,
                memoryLimit,
                tokenJson
        ));
    }
}

Request example:

{
    "httpMethod": "GET",
    "requestContext": {},
    "body": "",
    "isBase64Encoded": true
}

Response example:

{
    "multiValueHeaders": {
        "Content-Type": [
            "application/json"
        ]
    },
    "isBase64Encoded": false,
    "statusCode": 200,
    "headers": {
        "Content-Type": "application/json"
    },
    "body": "
        RequestID: 6ccf3084-a92a-43a4-9122-5520********\n
        FunctionID: b09hcbdla2ro********\n
        VersionID: b09h91stkts4********\n
        MemoryLimit: 1024\n
        TokenJSON: 
    "
}

Was the article helpful?

Previous
YcFunction interface
Next
Spring Boot
© 2026 Direct Cursus Technology L.L.C.