aspose file tools*
The moose likes Servlets and the fly likes Rendering of Servlet output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Rendering of Servlet output" Watch "Rendering of Servlet output" New topic
Author

Rendering of Servlet output

Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
I made a Servlet that generates JPEG image. While doing that, it also writes some data into the session bean.
JPEG image and bean's data should be displayed on the same JSP. But here is what happens. Displaying of JPEG image is a slower process than rendering HTML tags, so data from the session bean are rendered even before they are populated with Servlet.

Is there any way I can ensure that data from the session bean are displayed after displaying JPEG image?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Images and the page text are separate hits to the server.
Can you explain what you are doing in a little more detail?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
Here is in more detail.
While doing calculations needed to produce my image, I put some of those results in the session bean.
In the JSP I call a Servlet that produces image and right after that I have tags that are rendering mentioned results from the session bean.
It seems that a problem lies in the fact that browser renders image placeholder and all the text (including data from the session bean) in the page even before the image calculations are finished and all written to the session bean. So browser renders wrong data from the session bean (default or null values).
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Can you post your code?
If not, can you give us a detail explanation of what you are doing?
If you're spawning a new thread to create the image, be sure to describe how you are doing this in detail.
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
No I can not post code, bacause it is very big.

Here is excerpt from the program flow:

1) From my JSP page I call a servlet that generates image and a parameter from the session bean:

<img src="/myappcxt/createimage"/>
<%= ((SessGraphBean)session.getAttribute("sessBean")).getYMax() %>

2) My servlet named CreateImage.class contains inner class that do painting. While doing that it also writes some results into the session bean named SessGraphBean:

...
SessGraphBean sessgraph = (SessGraphBean)session.getAttribute("sessBean");
...
sessgraph.setYMax(y_max_value);
...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is the inner class spawning a new thread?

If so, you might want to consider not doing this. The page may take a little longer but you will know that the image is done before forwarding to the JSP.
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
No, the inner class is not spawning a new thread.

Once again. The problem lies in the fact that browser leaves placeholder for the image and continues to render next tags on the page. So, pay your attention on the next tag:

<%= ((SessGraphBean)session.getAttribute("sessBean")).getYMax() %>

Browser will render the default value of the yMax variable. It can not be aware that this variable will change as soon as servlet finish generetion of the image.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I think I see what you mean now.

The JSP is sending out two requests one for an image and another that reads the values generated by the request that creates the image.

Either way, you have a concurrency problem.

You could hit a servlet directly. In that servlet call the method that generates the image. After that method completes, forward the user to the JSP that calls the image and the values from the bean.

This way the JSP never gets called until the image generation is complete.
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
I have already done that.
But in my case it means I have to hit the Servlet twice, but it works.
Thanks you Ben Souther anyway.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rendering of Servlet output
 
Similar Threads
How do I retrieve images from LDAP using jsp?
Proble displaying images dynamically in jsp
creating image
want to insert photo(jpeg) in mssql image datatype
Displaying image on jsp