Sunday, July 18, 2010

Learning about ESP pages in Sling

Lately I've been doing a fair amount of server-side scripting using ESP (ECMAScript Pages) in Sling. At first blush, such pages tend to look a lot like Java Server Pages, since they usually contain a lot of scriptlet markup, like:

<%
// script code here
%>

and

<%=
// stuff to be evaluated here
%>

So it's tempting to think ESP pages are simply some different flavor of JSP. But they're not. From what I can tell, ESP pages are just server pages that get handed to an EspReader before being served out. The EspReader, in turn, handles the interpretation of scriptlet tags and expression tags (but doesn't compile anything into a servlet). Bottom line, ESP is not JSP, and despite the availability of scriptlets tags, things work quite a bit differently in each case.

Suppose you want to detect, from an ESP page or a JSP page, what kind of browser a given page request came from. In a Sling JSP page you could do:

<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %><%

%><sling:defineObjects/>

<html><body>

<%

java.util.Enumeration c = request.getHeaders("User-Agent");

String s = "";

while ( c.hasMoreElements() )

s += c.nextElement();

%>


<%= s %>

</body></html>

But what do you do in ESP? Remember, <sling:defineObjects/> is not available in ESP.

It turns out that Sling automatically (without the need for any directives) exposes certain globals to the JavaScript Context at runtime, and one of them is a request object. Thus, in ESP you'd simply do:



<%

c=request.getHeaders("User-Agent");

s = "";

while ( c.hasMoreElements() )

s += c.nextElement();

%>

<%= s %>


Very similar to the JSP version.

So the next question I had was, what are the other globals that are exported into the JavaScript runtime scope by Sling? From what I can determine, the Sling globals available in ESP are:

currentNode
currentSession
log
out
reader
request
resource
response
sling


currentNode is the JCR node underlying the current resource; currentSession is what it sounds like, a reference to the current Session object; log refers to the org.slf4j.Logger; reader, I'm not sure about (is it a reference to the EspReader?); request is a reference to the SlingHttpServletRequest; resource is the current Resource; response is, of course, a reference to the SlingHttpServletResponse; and sling is a SlingScriptHelper. All of these are available all the time, throughout the life of any ESP script in Sling.

For more information, try the Sling Javadocs here or Day's page of resources here (note, in particular, the list of References on the right).

18 comments:

  1. I just have this energy level that I've never had before. The only thing that's changed is I've started wearing my Power Balance bracelet and nike running shoes
    Power Balance wholesale
    Silly Bands
    Reebok Easytone

    ReplyDelete
  2. I just have this energy level that I've never had before. The only thing that's changed is I've started wearing my Power Balance bracelet and nike running shoes
    Power Balance wholesale
    Silly Bands
    Reebok Easytone

    ReplyDelete
  3. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete

  4. very useful info, and please keep updating........
    Best Online Software Training

    ReplyDelete
  5. perfect explanation about java programming .its very useful.thanks for your valuable information.
    goldenslot
    GCLUB Casino
    Gclub
    GClub casino

    ReplyDelete
  6. I just needed to record a speedy word to express profound gratitude to
    you for those magnificent tips and clues you are appearing on this
    site. Java and J2EE Training in Chennai | Java and J2EE Training Institutes in Chennai

    ReplyDelete
  7. I read this article. I think You put a lot of effort to create this article. I appreciate your work.
    Dissertation Writing Services

    ReplyDelete
  8. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Best Hadoop training in chennai

    ReplyDelete
  9. Thank you much for this tutorial; this is an informative and valuable blog. Visit for
    SEO Service in Delhi
    Web Development Company

    ReplyDelete
  10. Informative article, this is just what I was looking for, thank you!!

    DevOps Online Training

    ReplyDelete
  11. I simply want to tell you that I am just beginner to blogs and absolutely enjoyed you’re website. More than likely I’m planning to bookmark your blog . You really come with awesome stories.

    RPA Training in Chennai

    DevOps Training

    ReplyDelete
  12. I accept there are numerous more pleasurable open doors ahead for people that took a gander at your site.we are providing ReactJs training in Chennai.
    For more details: ReactJs training in Velachery | ReactJs training in chennai

    ReplyDelete
  13. THANKS FOR INFORMATION

    you can search low-cost website with high-quality website functions.
    Today Join Us
    Call: +91 - 8076909847

    website development company in Delhi

    levantro
    interior designers in delhi


    livewebindia
    website designing company in delhi

    SEO service in Delhi

    Best It Service Provider:

    1. Website Designing And Development.
    2. SEO Services.
    3. Software Development.
    4. Mobile App Development.

    ReplyDelete

Add a comment. Registration required because trolls.