How to render the abc.vm page through the Servlet

Hi all, we are facing an issue with rendering the ABC.vm file through the Servlet.

Could anybody guide us on how to render the velocity file through the Servlet?

Hi @manishjangir,
you could use a TemplateRenderer together with a Servlet:

Dependency:

<dependency>
    <groupId>com.atlassian.templaterenderer</groupId>
    <artifactId>atlassian-template-renderer</artifactId>
    <scope>provided</scope>
</dependency>

Servlet-Class:

import com.atlassian.templaterenderer.TemplateRenderer;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import org.springframework.beans.factory.annotation.Autowired;

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

public class MyServlet extends HttpServlet {

    @ComponentImport
    private final TemplateRenderer templateRenderer;

    @Autowired
    public MyServlet(TemplateRenderer templateRenderer) {
        this.templateRenderer = templateRenderer;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, Object> context = new HashMap<>();
        context.put("userName", "John Doe");
        context.put("issueKey", "TEST-123");

        resp.setContentType("text/html;charset=utf-8");
        templateRenderer.render("/templates/ABC.vm", context, resp.getWriter());
    }
}

atlassian-plugin.xml

<servlet key="my-custom-servlet" name="My Custom Servlet" class="com.mycompany.jira.servlet.MyServlet">
    <description>My custom servlet to render Velocity template</description>
    <url-pattern>/plugins/servlet/my-custom-servlet</url-pattern>
</servlet>

VM-Template:

<html>
<body>
    <h1>Hello $userName</h1>
    <p>This is related to issue: $issueKey</p>
</body>
</html>

Hope thats helps :slight_smile:

Cheers,
Daniel

Hi Daniel Grabke,

We are currently using Atlassian SDK version 8.2.7 and Java version 1.8.0_261. We attempted to implement the code you provided; however, we encountered an error while adding the template renderer dependency in the pom.xml file, which prevents us from using the template renderer in our Servlet.

Could you please advise us on the appropriate SDK and Java versions for the code you provided? Should we proceed with our current setup, or would you recommend using different versions?

Thank you for your assistance!