Solved: Unable to Access Context Variables from Velocity Template

Hey Everyone,

I am trying to create a JIRA plugin, and all I am trying to do at this point is to pass a variable from my Java Servlet to my Velocity Template. This is what my code looks like:

import java.util.HashMap;
import java.util.Map;

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

import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;

import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.templaterenderer.TemplateRenderer;
import javax.inject.Inject;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;

public class HomeServlet extends HttpServlet
    private static final String PLUGIN_STORAGE_KEY = "com.atlassian.plugins.dashboard.refapp.settings";

    private final TemplateRenderer renderer;
    private final PluginSettingsFactory pluginSettingsFactory;

    public HomeServlet(TemplateRenderer renderer, PluginSettingsFactory pluginSettingsFactory)
        this.renderer = renderer;
        this.pluginSettingsFactory = pluginSettingsFactory;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
        Map<String, Object> context = new HashMap<String, Object>();
        PluginSettings pluginSettings = pluginSettingsFactory.createGlobalSettings();

        if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".token") == null) {
            context.put("token", pluginSettings.get(PLUGIN_STORAGE_KEY + ".token"));

        if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".email") == null) {
            context.put("email", pluginSettings.get(PLUGIN_STORAGE_KEY + ".email"));

        if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".password") == null) {
            context.put("password", pluginSettings.get(PLUGIN_STORAGE_KEY + ".password"));

        System.out.println(pluginSettings.get(PLUGIN_STORAGE_KEY + ".token"));
        System.out.println(pluginSettings.get(PLUGIN_STORAGE_KEY + ".email"));
        System.out.println(pluginSettings.get(PLUGIN_STORAGE_KEY + ".password"));

        context.put("myvar", "Hello World!");

        renderer.render("templates/dashboard.vm", context, response.getWriter());    

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
        PluginSettings pluginSettings = pluginSettingsFactory.createGlobalSettings();
        pluginSettings.put(PLUGIN_STORAGE_KEY + ".token", request.getParameter("token"));
        pluginSettings.put(PLUGIN_STORAGE_KEY + ".email", request.getParameter("email"));
        pluginSettings.put(PLUGIN_STORAGE_KEY + ".password", request.getParameter("password"));


This will render the velocity template, but I cannot seem to get it to access the variables I pass. In my println statement, it prints the variable fine. In my template, if I add the following code, the variable does not display. It only shows ‘$token’


I got this code from the JIRA developer documentation, so I am not entirely sure why it is not working. Am I passing the context incorrectly? Am I accessing it incorrectly?

I have printed out the $context variable from the template, and that prints out an object with a bunch of classes, though, none of them are my context variables (or give me access to them).

Also, this is not to be used with Confluence. Any help will be appreciated. I have been stuck on this issue for hours.


Hi @zach.s,

Were you able to see the $myvar variable in your velocity template? If so, then you’re already able to pass the variable from your servlet class to the template. If you can’t see the $token variable, then maybe it does not go in the condition that you’ve set. One way to test this is to set a simple variable in your context and display it in the template. You can also see this: How to pass variables from java class to vm



Sorry i kept this topic open. I copied the code above from another post. If you notice the == null part of my code, it would make my conditions for token, email, and password empty. The fix was to make it !=. It was a complete F up on my part for not checking the code I copied. This problem is solved.

Hi @zach.s,

Nice! Can you please mark this as solved on your end? So other developers would be guided as well :slight_smile:


Anne Calantog