Stage 3: Profile settings servlet setup

At this point open the project in your IDE.

Servlet skeleton

Create a new class TwitterSettingServlet which will let you set Twitter credentials from the admin panel.
package com.example.ampstutorial;

import javax.servlet.http.HttpServlet;

public class TwitterSettingsServlet extends HttpServlet {

Http GET: Hello world

Let's start with very simple output. Override method doGet:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Hello world!");

New entry in Profile settings

To make your servlet visible in the Profile settings menu, add it to atlassian-plugin.xml

<!-- Load TwitterSettingsServlet -->
<servlet key="twitterSettingsServlet" class="com.example.ampstutorial.TwitterSettingsServlet">

<!-- Make twitter-settings servlet visible in profile settings menu -->
<web-item key="config-link" section="">
	<label key="Twitter Configuration"/>


Install the servlet with pi and go to http://localhost:3990/fecru/. Expand the top-rightmost menu and choose Profile settings. You should now see Twitter Configuration on the list. Click it to get your "Hello world!" message. Notice that the menu disappeared.

Decorate servlet with decorator

Add the following lines at the beginning of the servlet's  doGet  method:
request.setAttribute("decorator", "");

Remember to use pi each time to reload the servlet.

Refresh the page and check that after clicking Twitter Configuration the menu on the left no longer disappears.

Menu item highlighting

The Twitter Configuration item is not highlighted correctly when selected. That's because the decorator can't identify the link belonging to the active tab. We need to set the  attribute to the key of the Web Item. Replace the plain "Hello world!" string with:
	"<html><head>" +
	"<meta name='' content='com.example.ampstutorial.fecrutwitter:config-link'/>" +
	"</head><body><div>Hello world!</div></body></html>");

com.example.ampstutorial.fecrutwitter:config-link  is the plugin key (from the  <atlassian-plugin>  element) and the key of the Web Item, separated by a colon.

Now the correct text should be highlighted when you click on the Twitter Configuration link.


Your servlet can use a templating library to produce HTML. FishEye/Crucible provide some utility classes to help you use  Velocity , but you could use other libraries too.

Add TemplateRenderer dependency


Add TemplateRenderer component

<!-- Import the template renderer -->
<component-import key="templateRenderer" interface="com.atlassian.templaterenderer.TemplateRenderer" />

Create basic template

In the resources directory create the "Hello world!" template:

	<meta name='' content='com.example.ampstutorial.fecrutwitter:config-link'/>
Hello world!

Inject template renderer
import com.atlassian.templaterenderer.TemplateRenderer;
	private final TemplateRenderer templateRenderer;
	public TwitterSettingsServlet(TemplateRenderer templateRenderer) {
		this.templateRenderer = templateRenderer;

Use template

In doGet replace the response you had so far with:
	templateRenderer.render("/templates/twitterSettings.vm", ImmutableMap.of(), response.getWriter());


Was this page helpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport