This is a cookbook on how to do different things programmatically in plugins. It's supposed to help developers, give them instructions on how to use the wiki api in the spirit of "Don't tell me, show me".

This page was originally created on our intranetpage to help students to write the i3GPlugins. I guess it is a much better place to put it here. The code is for 2.2, so please feel free to add code if it is done differently in 2.4. (but please don't remove the old code). Please also make notes if it is bad style and correct it with the proper way.

Thanks --ChristophSauer 12-Sep-2006


  • If Plugins embody code that might be useful for other Plugins, factor out the code from the execute method in a public static method, so that other plugins can use that Code as well. For example the PageHeading plugin gets the PageHeading of a certain Page. This might be useful for other Plugins as well...
  • If Plugins use code that is generally used (like described below) but does not embody the core functionality of that plugin (like in the case of the PageHeading: getPageHeading()), factor it out into a PluginUtility class.

Coding a JSPWiki Plugin#

Create Link to Page#

This link however will not be recognized by the ReferingPages Plugin...

String pageLink = context.getURL(WikiContext.VIEW, pageName);
String linkTitle = pageName;
return "<a href=\"" + pageLink + "\">" + linkTitle + "</a>";

Alternate method to create a real wikipage-link using the WikiEngine#

    return context.getEngine().textToHTML(context,"[" + pageName + "]");
- by twieschadlo

Create Link to Attachment#

WikiEngine engine = context.getEngine();
AttachmentManager mgr = engine.getAttachmentManager();
Attachment att = mgr.getAttachmentInfo(context, src);

if (att != null) 
    src = context.getURL(WikiContext.ATTACH, att.getName());

Attach a file#

How to attach a file from a directory to a wiki page

String source = "Hello World!";
Attachment att = new Attachment(pagename, filename);
InputStream is = new StringBufferInputStream(source); 
AttachmentManager am = wiki.getAttachmentManager(); 

am.storeAttachment(att, is);

Creating a page programatically#


Writing a Servlet for a WikiPlugin#

Get WikiEngine and WikiContext#

If you are adding a servlet to your plugin suite you don't have a WikiEngine Object, which is passed as parameters for plugins. Here's how to get one outside the plugin interface...
public class MyServlet extends HttpServlet {

    private WikiEngine m_wikiEngine;
    private WikiContext m_wikiContext;

    public void init(ServletConfig arg0) throws ServletException 
        m_wikiEngine = WikiEngine.getInstance(getServletConfig());
        WikiContext m_wikiContext = new WikiContext(m_wikiEngine, wpage);

Get ServletContext from WikiContext#

ServletContext servletContext = 

Read PropertyFile#

ServletContext servletContext = 

InputStream propertyStream = 
Properties props = new Properties();
props.load( propertyStream );

Read HttpSession#

Enumeration enum = context.getHttpRequest().getSession().getAttributeNames();
while(enum.hasMoreElements()) {
      String key = (String) enum.nextElement();
      Object attribute = context.getHttpRequest().getSession().getAttribute(key);
      System.out.println("  "+key+": "+attribute+ "/class: "+attribute.getClass());

context no longer has getHttpRequest(); I gather the intent is to use context.getWikiSession().getMessages(...).

--JerryAndrews 31 May 2007

It still totally has! getHttpRequest() has not gone anywhere...


Authentification in Session#

the authentification object in the HttpSession is an argument with the name "currentUser" with class com.ecyrd.jspwiki.auth.UserProfile.
UserProfile wup = context.getCurrentUser();
context.getHttpRequest().getSession().setAttribute("currentUser", wup);

Ant Tasks#

Make jar file out of your Plugin
<project name="VisualApplet" default="jar" basedir=".">

	<!-- The class files are actually put in this directory.  It is
	       a good habit to keep .class -files separate from the .java -files. -->
	<property name="" value ="build" />
	<property name="code.src" value ="src" />
	<target name="compile" depends="init"
	          description="Builds the source code.">
	  	<mkdir dir="${}"/>
	    <javac srcdir="${code.src}"
	      <compilerarg line="-source 1.4"/>

    <target name="jar" depends="init, compile" 
    	description="Packages the application's jar archive file from already compiled project bin">

        <delete file="${}.jar"/>
        <jar jarfile="${}.jar" manifest="etc/app.manifest">
            <fileset dir="${}" includes="**/*.class" excludes="**/*Test.class"/>


It would be nice to have this article supplied with source code of examples. Thanks

--chx, 19-Jan-2007

Thanks for putting together a cookbook. Is it still valid for 2.4/2.5?

--NascifAbousalhNeto, 11-May-2007

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-6) was last changed on 01-Jun-2007 09:46 by JanneJalkanen