cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Joseph Jupin" <>
Subject AW: Having Servlet call Cocoon and then capturing results...
Date Fri, 08 Feb 2002 14:37:13 GMT
Hey, All,

Since I posted that response of having a serlvet talk to 
cocoon and capture the results, I've had several requests 
on how to do this.  First off, let me give the reason why 
we chose this way:

We have a state machine that is sending xml documents of 
activities across the net.  The result can be renderer in 
either xml, wml, pager-format or even XUL for swing.  The 
issue for is that we wanted just a fronting servlet to 
handle the request that we all talk to and then separate 
the transformations (using cocoon) into it's own separate 
process.  That why, since the xml is dynamic, we can 
easily switch on parameters, etc.

So, on to how I solved this:

first off, I have a sub-sitemap and in it, I have the 
following pipeline segment.  Notice that my servlet passes 
in several parameters, but for now, I'm only interested in 
the "xml" parameter and "style" parameter.  (this assumes 
that you set up the request selector as shown in the 
cocoon sitemap.xmap section of selectors.)

   <!-- original zodiac translator matcher... -->

   <map:match pattern="translate">

      <map:generate type="stream">
         <map:parameter name="form-name" value="xml"/>

      <map:select type="request">
         <map:parameter name="parameter-name" 
      <map:when test="fancy">
      <map:transform src="stylesheets/fancy.xsl" />
      <map:transform src="stylesheets/generic.xsl" />
     <map:serialize type="html"/>


So, what does this say?  It states that take whatever is 
streamed in as the xml parameter and then select the 
transform based on the "style" parameter and serialize the 
results as html.

In my servlet, I have the following (which calls cocoon - 
the results from cocoon are then sent to the httpResponse 
writer (note:  I have a sub-site called "zodiac" - and no, 
I haven't mapped the root of cocoon yet in tomcat - we're 
still in development and that will come later when we go 

       StringBuffer uri = new 
        BufferedReader br = null;

        try {

           // setup my url connection stuff to be sent to 

           URL miurl = new URL(uri.toString());
           URLConnection con = miurl.openConnection();
type","application/x-www-form-urlencoded ; 

           String params = "&mode=" + mode + "&style=" + 
style + "&lang=" + lang + "&action=" + action;

           // note:  here, s is passed in as the string of
           //        of xml data (properly formed).
           //        Now, you need to figure the length of
           //        of the data being sent since the 
           //        won't be able to figure out how to 
           //        reading for parameters!  <---  VERY
           //        important!!!
           String length = 
           //  computed my length, now, set that request
           //  property and start writing to the stream!

           DataOutputStream dos = new 
           dos.writeBytes("xml=" + 
URLEncoder.encode(s.toString()) + params);

           sb = new StringBuffer();

            // done.
            // read any results.
            // and handle them accordingly in your 
            //  I just capture it all in a stringBuffer
            //  and then do any manipulation in other 
            //  methods...

            InputStreamReader isr = new 
            br = new BufferedReader(isr);

            String aline;
            while ((aline=br.readLine()) != null)


        } catch (MalformedURLException mfe) {
           System.out.println("mfe: " + mfe.toString());
        } catch (IOException ioe) {
           System.out.println("ioe: " + ioe.toString());
        } catch (Exception e) {
           System.out.println("e: " + e.toString());

        return sb;

The returned string buffer (sb) is then sent to the writer 
for output - it works great as a fronting servlet that I 
can change paremeters to and get dynamic xml & html files 
back from.

hope this helps...

peace.  JOe...

Please check that your question has not already been answered in the
FAQ before posting. <>

To unsubscribe, e-mail: <>
For additional commands, e-mail: <>

View raw message