On Tue, Jan 26, 2010 at 6:07 PM, Andreas Schaefer <[email protected]> wrote:
> Hi
>
> To see if Sling would fit our project I would need to be able to handle calls 
> from a client (Ext/JS) sending JSon to server and expecting a JSon response 
> back. In the samples I read that it is possible to register a servlet inside 
> Sling but I am not quite sure how to do it. The only place where I found a 
> servlet used is in 
> 'org.apache.sling.sample.slingshot.impl.ResourceTypeConversionServlet' or the 
> 'org.apache.sling.launchpad.testservices.war.servlets.TestServlet'.

Also look in the Sling codebase. SlingInfoServlet [1]  is a simple
example that registers a servlet to handle requests to
/system/sling/info. Or look at JsonQueryServlet [2], which registers
for all requests ending with ".query.json".

> Would that be the way to handle JSon calls from a client inside Sling?

Sling comes with a default JSON renderer. Just add ".json" to any
resource URI, and you'll get a JSON representation, e.g.:
http://localhost:8080/your/node.json

If you need a custom JSON format, you could write your own servlet, or ...

> Could that be done with an ESP file instead?

Absolutely. If you're not happy with the default JSON representation
of a resource, you could easily implement your own in ESP:
<%
  response.setContentType("application/json");
%>
{
  your_custom_json_property: <%=currentNode.something%>
}
Saving this script as /apps/servlet/default/customjson.esp in your
repository, and then doing a request to
http://localhost:8080/your/node.customjson should invoke your script
and return the JSON.

Also see the JSONQueryServlet for how to return query results in JSON:
http://in-the-sling.blogspot.com/2008/09/how-to-use-json-query-servlet.html

If you haven't done so already, check out the Sling in 15 minutes
guide: http://sling.apache.org/site/discover-sling-in-15-minutes.html

Hope this gets you started - if you run into any trouble, don't
hesitate to ask on this list.

[1] 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java?view=markup
[2] 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/JsonQueryServlet.java?view=markup

-- 
Vidar S. Ramdal <[email protected]> - http://www.idium.no
Sommerrogata 13-15, N-0255 Oslo, Norway
+ 47 22 00 84 00 / +47 21 531941, ext 2070

Reply via email to