Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Solutions
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Cloud Functions
  • Getting started
    • Overview
    • Creating a function
      • Overview
      • Python
      • Node.js
      • PHP
      • Go
      • Bash
    • Creating a trigger
      • Overview
      • Timer
      • Trigger for Message Queue
      • Trigger for Object Storage
      • Trigger for Container Registry
      • Trigger for Cloud Logs
      • Trigger for Yandex IoT Core
  • Step-by-step instructions
    • All instructions
    • Using functions to get an IAM token for a service account
    • Managing rights to access functions
    • Managing functions
      • Creating a function
      • Managing function versions
      • Working in the code editor
      • Invoking a function
      • Updating a function
      • View monitoring charts
      • Viewing the execution log
      • Delete function
    • Managing triggers
      • Getting information about a trigger
      • Creating a timer
      • Creating a trigger for Message Queue
      • Creating a trigger for Object Storage
      • Creating a trigger for Container Registry
      • Creating a trigger for Cloud Logs
      • Creating a trigger for Yandex IoT Core
      • Updating a trigger
      • Deleting a trigger
  • Concepts
    • Overview
    • Function
    • Invoking a function
    • Runtime environment
      • Overview
      • Environment
      • Execution context
    • Builder
    • Trigger
      • Overview
      • Timer
      • Trigger for Message Queue
      • Trigger for Object Storage
      • Trigger for Container Registry
      • Trigger for Cloud Logs
      • Trigger for Yandex IoT Core
    • Dead Letter Queue
    • Log groups
    • Monitoring
    • Backups
    • Quotas and limits
  • Developing in Node.js
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Python
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Go
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in PHP
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
  • Developing in Bash
    • Overview
    • Request handler
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Java
    • Overview
    • Programming model
      • Overview
      • Function interface
      • YcFunction interface
      • HttpServlet class
      • Spring Boot
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in R
    • Overview
    • Programming model
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
  • Developing in C#
    • Overview
    • Programming model
      • Overview
      • Function interface
      • YcFunction interface
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Use cases
    • Creating skills for Alice
  • Pricing policy
  • Access management
  • API Functions reference
    • Authentication in the API
    • gRPC
      • Overview
      • FunctionService
      • OperationService
    • REST
      • Overview
      • Function
        • Overview
        • create
        • createVersion
        • delete
        • get
        • getVersion
        • getVersionByTag
        • list
        • listAccessBindings
        • listOperations
        • listRuntimes
        • listTagHistory
        • listVersions
        • removeTag
        • setAccessBindings
        • setTag
        • update
        • updateAccessBindings
  • API Triggers reference
    • Authentication in the API
    • gRPC
      • Overview
      • TriggerService
      • OperationService
    • REST
      • Overview
      • Trigger
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • pause
        • resume
        • update
  • Questions and answers
  1. Developing in Java
  2. Programming model
  3. HttpServlet class

Using the HttpServlet class to set a handler

  • Unsupported methods
  • Example of modeling various function behaviors when invoked using different HTTP methods

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

Cloud Functions will automatically redirect each HTTP request to your handler, depending on the HTTP method that this request was initiated with.For example, a GET request is redirected to your handler's doGet method and a POST request to the doPost method. For a successful redirect, the corresponding handler methods must exist, otherwise the function returns the HTTP method XXX is not supported by this URL message with code 405.

Unsupported methods

When using this model, please note that some methods of the HttpServletRequest and HttpServletResponse classes are not supported by Cloud Functions.

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 various function behaviors when invoked using different HTTP methods

Warning

Don't use the integration=raw parameter to call this function. If you do, the function won't get any data about the original request's methods, headers, and parameters.

The Handler.java file:

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 {
    // by setting the Content-Type to text/html, we let the browser render the HTML code
    response.setContentType("text/html");
    // displayed in bold when the function is invoked from the 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);
    // the printWriter class returned by the getWriter method, make sure it's closed
    response.getWriter().close();
  }
}

Sample requests:

The GET method:

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

Note

When executing a GET request, this string is displayed in bold and the tags disappear in the browser, because Content-Type: text/html was specified in the handler code.

The POST method, the name=Anonymous request parameter:

Hello, Anonymous

The PUT method:

HTTP method PUT is not supported by this URL
In this article:
  • Unsupported methods
  • Example of modeling various function behaviors when invoked using different HTTP methods
Language / Region
Careers
Privacy policy
Terms of use
Brandbook
© 2021 Yandex.Cloud LLC