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
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • Overview
      • Overview
      • Function interface
      • YcFunction interface
      • HttpServlet class
      • Spring Boot
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ

In this article:

  • Unsupported methods
  • Example of modeling various function behaviors when invoked using different HTTP methods
  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 September 23, 2024
  • Unsupported methods
  • Example of modeling various function behaviors when invoked using different HTTP methods

You can define a Java 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 used to initiate the request. A GET request, for instance, will be redirected to your handler's doGet method, and POST, to doPost. For a redirect to be successful, 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, please note that certain HttpServletRequest and HttpServletResponse methods 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 methodsExample of modeling various function behaviors when invoked using different HTTP methods

Warning

Do not use the ?integration=raw parameter 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 file:

<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 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();
  }
}

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 because you had 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

Was the article helpful?

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