Documentation versions (currently viewingVaadin 23)

Obtaining a Collaboration Engine Instance in a Servlet

When a VaadinService instance is not available, it is impossible to obtain a CollaborationEngine instance with the getInstance() method. In a servlet, the instance can be obtained with the getAttribute() method, as long as it has been configured. Please refer to Other Applications in production configuration documentation for details on how to configure Collaboration Engine.

CollaborationEngine ce = (CollaborationEngine) getServletContext()

In the following example, a message posted through a form is added to a topic as a system message:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    CollaborationEngine ce = (CollaborationEngine) getServletContext()
    if (ce == null) {
        resp.sendError(500, "Collaboration Engine has not yet been initialized");
    // Retrieve message that was sent in this POST request
    String userMessage = req.getParameter("message");
    String message = "An anonymous user posted the following message: " +
    sendMessageToTopic(ce, message);

public void sendMessageToTopic(CollaborationEngine ce, String message) {
        // Get SystemConnectionContext from Collaboration Engine
        SystemConnectionContext systemConnectionContext =

        MessageManager messageManager = new MessageManager(
            systemConnectionContext, systemUser, topicId, ce);

            .submit(message) // Send message to topic
            .whenComplete((a, t) -> {
                // Close the message manager when done