Friday, July 9, 2010

Java-Backed Web Scripts

Hi., in this article i will explain about how to write Java backed web script in alfresco,if you are new to java this article may more helpful to you.just i will explain more which we have same thing in wiki i.e.,  

                      Java class is the controller rather than JavaScript .basically JavaScript is very light weighted language even we can write webscrpit using simple JavaScript but there are some reasons to write using Java.

1.accessing alfresco application services not available via JavaScript API
2.when the performance is absolutely critical get tighter control of the generated response.
4.when we need to access the Alfresco API
5.if we prefer stronger programming language like JAVA

the construction view for this script is shown below.

                    in this example i will explain simple java backed webscript  step wise which is in

Step1: use eclipse  to get the class file for a java code very i am using in same way.

a) create a java project in eclipse name it as java-backed-webscripts
b)create a folder and name it as lib import the jar files to this file from                 \tomcat\webapps\alfresco\WEB-INF\lib
c)create on more folder in the project and name it as source
d)right click the source folder then go to new>file name it as
                copy the below code in that
package org.alfresco.module.demoscripts;


import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;
import org.json.JSONException;
import org.json.JSONObject;

public class SimpleWebScript extends AbstractWebScript
    public void execute(WebScriptRequest req, WebScriptResponse res)
        throws IOException
    // build a json object
    JSONObject obj = new JSONObject();
    // put some data on it
    obj.put("field1", "data1");
    // build a JSON string and send it back
    String jsonString = obj.toString();
     catch(JSONException e)
     throw new WebScriptException("Unable to serialize JSON");


d)right click on the project and new>file -- create a file with the name build.xml
                  copy the code ass shown below.

<?xml version="1.0"?>

<project name="Sample Module" default="package-amp" basedir=".">

<property name="project.dir" value="." />
<property file="${project.dir}/" />
<property file="${project.dir}/" />

<property name="build.dir" value="${project.dir}/build" />
<property name="config.dir" value="${project.dir}/config" />
<property name="jar.file" value="${build.dir}/lib/${}.jar" />
<property name="amp.file" value="${build.dir}/dist/${}.amp" />

<target name="mkdirs">
<mkdir dir="${build.dir}/dist" />
<mkdir dir="${build.dir}/lib" />
<mkdir dir="${build.dir}/classes" />

<path id="class.path">
<dirset dir="${build.dir}" />
<fileset dir="${project.dir}/lib" includes="**/*.jar" />

<target name="clean">
<delete dir="${build.dir}" />

<target name="compile" depends="mkdirs">
<javac classpathref="class.path" debug="${debug}" srcdir="${project.dir}/source/java" destdir="${build.dir}/classes" target="1.5" encoding="UTF-8" />
<copy todir="${build.dir}/classes">
<fileset dir="${project.dir}/source/java" defaultexcludes="false">
<exclude name="**/*.java" />
<exclude name="**/.svn/**" />
<exclude name="**/CVS/**" />

<target name="package-jar" depends="compile">
<jar destfile="${jar.file}" encoding="UTF-8">
<fileset dir="${build.dir}/classes" excludes="**/custom*,**/*Test*" defaultexcludes="false" />

<target name="package-amp" depends="package-jar" description="Package the Module">


e)create one more file
module.title=test web script
module.description=Project to get class file for webscripts

f)then right click on build.xml  --> run as --> AntBuild

it will create a class file and jar file. in build folder. copy this jar file and paste it into  \tomcat\webapps\alfresco\WEB-INF\lib.

Step2: next  step is to declaring the webscript to spring  add the below code in web-scripts-application-context.xml, which is  /tomcat/webapps/alfresco/WEB-INF/classes/alfresco

<bean id="" 

Step3: next go to /tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco and create a folder and name it as demo

and then create a file in this folder with the name simple.get.desc.xml then add the below code in that  

  <shortname>The World's Simplest Webscript</shortname>
  <description>Hands back a little bit of JSON</description>
  <format default="">argument</format>

Step4:  Just  restart the server and type this url in the browser 


the JSON will return { “field1” : “data1” } 

Example 2:


  1. I tried installing the simple demo webscript. When I run it I get the following error:

    Web Script format '' is not registered

    Any advice?

  2. Check your xml files by opening it with any of your browser, make sure with out errors in that.

  3. and check the registered bean xml file also

  4. Hi, thanks for your post, very helpful.
    I miss some more info in amp ant task, appears empty

    Are there some instructions inside?


  5. Hi. I have implemented a script in Java and deployed it on Alfresco. It works great and Share dashlets are using it fine. But now I need to deploy it on Share. I have registered bean in slingshot configuration, deployed jar file in Share's WEB-INF/lib and placed description file under share's WEB-INF folder. Script is successfully deployed and registered but it doesn't work - it seams that I have put description file in wrong directory (i tried a lot of them).

    Do you have some working example for Java backed webscript deployed in Share?

    I use Alfresco Share 4.3.0b.

  6. This comment has been removed by the author.

  7. Also included in this dropdown list are links to administrative functions like "Application" settings, "Groups", "Users", and another new-for-3.4 capability, "Replication Jobs".

    alfresco workflow

  8. Could you please help me,how override mechanism will work, when we apply copy folder from source to destination in alfresco. If already exist in destination node.

    When we tried if folder is already exist in destination node then its creating with "Copy-foldername" but its not overridden with same folder.

    Please help me is there configuration required in alfresco or any change required trough webscript/javascript api

  9. What is the folder structure of any surf application ?