You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Sachin Dharmapurikar <dh...@gmail.com> on 2005/09/07 12:51:02 UTC

How to Share objects with Servlets in Same container

Hi All,
   In our company, we have already developed one solution which has
following use Case:

1. Form: To get the input from user about _Something_

2. Servlet: Depending on the inputs query to database _do_ some operations,
and as a side effect of these operations one java bean is created containing
information and data to create image/charts. Now this image generation is
done by a dedicated servlet.

3. Image generator servlet: As I discussed above, we get the java bean
containing data to create the image and creates image. This bean is having
lifetime of session and it's automatically deleted once session is over.

Since our application is having lot of presentation stuff, I think in our
application framework Cocoon is best fit. So I am planning to port my
application on Cocoon. From one-and-half week I am trying cocoon and after
stretching for long hours, I came to know that its excellent product and
framework in all. Now the question comes is how I can do it. What I have
thought is:

1. To take input from user: Cocoon Forms (Is it Stable? As in docs said,
developers are working on stable release of CForms)
2. To process the logic: XSP logic sheets, I will use these sheets to create
the beans also.
3. to present pages XSLT rendering.

Now I want your input on this design. Please quote on this.

The above stated design in almost in my vision. How to do it and so but I am
facing a real problem. The problem is:
1. Where to store the bean? In Servlet I used to use
HttpSessionBindingListner which allowed me to put the value in session and
forget. It will take care of deleting and clearing persistent objects.
2. How to pass directly this bean to Imageservlet?????

Thanks in advance...

Regards,
Sachin Dharmapurikar


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


RE: How to Share objects with Servlets in Same container

Posted by Jason Johnston <co...@lojjic.net>.
>>>From what I've seen XSP is being phased out (or at least discouraged for
most uses) now that we have better ways (Control Flow) to handle
processing.
>
> I didn't understood one thing, if XSP is outdated, do I need to serialize
> the Java program output again and again to XML if XSP is not there? Because
> AFAIK in cocoon the output of one stage to another goes in XML format. Do
> you have to say something on this?

Sure! :-)

XSP allows you to inject values from Java objects into the XML stream, but
rather uncleanly (IIUC) since it also mixes the logic of how to get those
Java objects into the presentation layer.  In Cocoon's MVC framework the
logic for getting the objects is done in the Flow, which then passes those
objects along to the presentation pipeline as view data:

  var myBean = doLogicToGetBean();
  var viewData = {
    "theBean" : myBean,
    "foo" : "bar",
    "now" : new Date() //etc...
  };
  cocoon.sendPage("the/display/pipeline", viewData);

Then you use something like JXTemplateGenerator[1] to grab the appropriate
values from the view data and inject them into the XML.  This is a cleaner
separation of concerns (logic in the flow, presentation in the template)
than XSP and that's probably the reason XSP is being discouraged.

You definitely don't want to generate representations of your beans as XML
each and every time; I've done this on a prior project and performance
sucks because you end up generating huge XML streams for just a few
values.

[1] http://cocoon.apache.org/2.1/userdocs/flow/jxtemplate.html

--Jason


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


RE: How to Share objects with Servlets in Same container

Posted by Sachin Dharmapurikar <dh...@gmail.com>.
Jason Johnston Wrote:
> 
> There may be other ways to do this that I don't know about, but putting
> the bean on the session and issuing a redirect to ImageServlet would
> definitely work (I've done similar things running Cocoon and ColdFusion
> together).  Note that the two servlets (CocoonServlet and ImageServlet)
> will have to be running in  the same servlet container to share session
> data.
> 
> Also, I'm not sure what exactly your ImageServlet involves, but Cocoon
> has facilities for generating PNG/JPG images from SVG documents, so if
> you wanted to try porting it over then you'd have a fully-Cocoon app and
> wouldn't have to worry about passing stuff between servlets.  Just a
> thought.
> 

First I would like to thanks for your prompt response and detailed advice.
In my case, as a result of user inputs we display a report page to the user.
This report page contains some dynamic text fetched/processed from database
and based on some database information we also have some chart generated.
This chart generation info I am planning to put into session (as we
discussed earlier). Now as HTML text and images cannot be sent in only one
response, we just embed 

REPORT PAGE SAMPLE:

<p>some text here</p>
<img src="/servlet/ImageServlet?id=10989">
<p>some more text here</p>

Now for each image in report this id is taken as input by the image servlet
and by retrieving its data from session it can generate the chart based on
that data. For this we use some java tool. I am not sure whether we can
generate charts by XML; I don't want to invest my efforts on writing the
chart API.

As you said very correctly, to make this solution workable both ImageServlet
and cocoon (servlet) should be in the same container.

>>From what I've seen XSP is being phased out (or at least discouraged for
>most uses) now that we have better ways (Control Flow) to handle
>processing.

I didn't understood one thing, if XSP is outdated, do I need to serialize
the Java program output again and again to XML if XSP is not there? Because
AFAIK in cocoon the output of one stage to another goes in XML format. Do
you have to say something on this?

Please suggest me something...

-Sachin


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: How to Share objects with Servlets in Same container

Posted by Jason Johnston <co...@lojjic.net>.
Sachin Dharmapurikar wrote:
> Hi All,
>    In our company, we have already developed one solution which has
> following use Case:
> 
> 1. Form: To get the input from user about _Something_
> 
> 2. Servlet: Depending on the inputs query to database _do_ some operations,
> and as a side effect of these operations one java bean is created containing
> information and data to create image/charts. Now this image generation is
> done by a dedicated servlet.
> 
> 3. Image generator servlet: As I discussed above, we get the java bean
> containing data to create the image and creates image. This bean is having
> lifetime of session and it's automatically deleted once session is over.
> 
> Since our application is having lot of presentation stuff, I think in our
> application framework Cocoon is best fit. So I am planning to port my
> application on Cocoon. From one-and-half week I am trying cocoon and after
> stretching for long hours, I came to know that its excellent product and
> framework in all. Now the question comes is how I can do it. What I have
> thought is:
> 
> 1. To take input from user: Cocoon Forms (Is it Stable? As in docs said,
> developers are working on stable release of CForms)

This terminology has been discussed on the developers list recently, and 
this is a perfect example of why it should be changed.  In this case 
"unstable" means that the Java API for CForms components is not yet firm 
and may change under developers, *not* that it is crash-prone.  In fact 
many people, including myself, have been using CForms in critical 
production environments for a while with no stability problems 
whatsoever.  So don't be scared when you read it's not marked "stable" yet.

> 2. To process the logic: XSP logic sheets, I will use these sheets to create
> the beans also.

I would suggest you use CForms to bind form values directly to/from your 
JavaBeans (see the binding framework), and the flow controller (either 
flowscript or javaflow) to pass those beans around for processing.  IMO 
this is the cleanest way to handle processing that I've ever seen in a 
web app framework, and one of the main reasons I've become a huge Cocoon 
junkie.  From what I've seen XSP is being phased out (or at least 
discouraged for most uses) now that we have better ways (Control Flow) 
to handle processing.

> 3. to present pages XSLT rendering.

Yes.  CForms already uses XSLT to create the HTML representation of its 
widgets, so you can just extend that to customize your layout or add an 
additional XSLT stage.

> Now I want your input on this design. Please quote on this.
> 
> The above stated design in almost in my vision. How to do it and so but I am
> facing a real problem. The problem is:
> 1. Where to store the bean? In Servlet I used to use
> HttpSessionBindingListner which allowed me to put the value in session and
> forget. It will take care of deleting and clearing persistent objects.

If you're using Control Flow you can put objects on the session, for 
example in flowscript: cocoon.session.setAttribute("name", theBean);.

Flowscript also allows you to get transparent session persistence by 
using global variables, but if you're intending that the object on the 
session be available to other servlets then cocoon.session.setAttribute 
is probably what you want.

> 2. How to pass directly this bean to Imageservlet?????

There may be other ways to do this that I don't know about, but putting 
the bean on the session and issuing a redirect to ImageServlet would 
definitely work (I've done similar things running Cocoon and ColdFusion 
together).  Note that the two servlets (CocoonServlet and ImageServlet) 
will have to be running in  the same servlet container to share session 
data.

Also, I'm not sure what exactly your ImageServlet involves, but Cocoon 
has facilities for generating PNG/JPG images from SVG documents, so if 
you wanted to try porting it over then you'd have a fully-Cocoon app and 
wouldn't have to worry about passing stuff between servlets.  Just a 
thought.

Best of luck, you've made an excellent choice moving to Cocoon.  We're 
happy to answer any more questions you have.
--Jason

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org