Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rendering of Servlet output

 
Velika Srbija
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Images and the page text are separate hits to the server.
Can you explain what you are doing in a little more detail?
 
Velika Srbija
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic