• Post Reply Bookmark Topic Watch Topic
  • New Topic

out -- in new Thread  RSS feed

 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am creating new (anonymous) Thread in my JSP and when I am trying to use out in that thread, it says that out needs to be declare as final.

I am doing some presentation work & downloading an image in the JSP, which takes times. To make it (down loading the image) in separate thread, I am doing the same exercise.

your comments & suggestions would be appreciated.

Thanks & regards,
Sanjeev.
[ August 24, 2004: Message edited by: sanjeevmehra mehra ]
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare a new final variable and assign it to 'out'. Do this outside the anonymous class.

final OutputStream outter = out;
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
downloading an image in the JSP

Don't do it - JSP are for character data, servlets are for binary as well as character data.
There is nothing to be gained by starting another Thread to write to the response.
Bill
 
Kingsly Theodar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating new (anonymous) Thread in my JSP and when I am trying to use out in that thread, it says that out needs to be declare as final.

First you need to know that "out" is a JspWriter object, which is available implicitly in jsps.
Actually it is a local variable in the _jspService method.

But you are trying to access this local/auto variable within a Anonymous classes which is not possible.

Anonymous classes can access local final variables of their outer method and not the non-final local variables.

So if you want to access the implicit JspWriter object the best way is to use the following approach,



You can always run the above thread as,
new Thread(new NamedClass(out)).start();

Hope this helps
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.

Kingsly Thewdar
You can always run the above thread as,
new Thread(new NamedClass(out)).start();

But again you are using out in anonymous new Thread in JSP. Is it like that


William Brogden
Don't do it - JSP are for character data, servlets are for binary as well as character data.
There is nothing to be gained by starting another Thread to write to the response.

Sorry, for not proviging the complete information. Actually I am calling a servlet (which is downloading the image) within
<img src=<%="/mypack/fetchtheimage?no="+imageNo%>>
And my intention was to put the above <img> tag into a anonymous new Thread.


Rovas Kram

final OutputStream outter = out;

final javax.servlet.jsp.JspWriter outter = out;
pls. let me test it.


Thanks to all for the response.

Regards,
Sanjeev.
[ August 25, 2004: Message edited by: sanjeevmehra mehra ]
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this works fine



but it throws comilation error taht out should be declared as final





thanks & regards,
sanjeev.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<img src=<%="/mypack/fetchtheimage?no="+imageNo%>>
And my intention was to put the above <img> tag into a anonymous new Thread.

I see a major conceptual error here. The request to that servlet will be made by the browser when it is putting together the page that your JSP wrote.
That is a SEPARATE request - carried out in a separate Thread whenever the user's browser gets around to requesting that image (if at all - the user's browser may have image fetching turned off.)
The HTML created by your JSP may have 0, 1 or many links to separate resources such as CSS files, images, multimedia, etc etc. EACH ONE causes a separate request.
Bill
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill thanks a lot. Thanks for the explanation.

My problem is when user does access the next button before starting of the downloading of the first image (all the presentation got available but downloading of image takes few seconds.), it provide the first image not the second one. I would appreciate your suggestions regarding this.


Thanks & Regards,
Sanjeev.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your application do? What are these slow-downloading images of? It sounds to me like you should optimise the image for the web so that it is presented within reasonable time.

Jules
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am passing URL of existing web application in my application which is providing the image & image size after downloading is around 1.8 MB.


thanks & regards,
Sanjeev.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is when user does access the next button before starting of the downloading of the first image (all the presentation got available but downloading of image takes few seconds.), it provide the first image not the second one. I would appreciate your suggestions regarding this.

I am still not quite sure of the sequence of events here. What is this about a second image?
If you want to prevent the user from jumping to the next page before the image is loaded, you can start with the next button disabled and use the Javascript onLoad() method to enable it only when the whole page is loaded.
Bill
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't really understand what you're trying to do. Do you actually display this 1.8Mb image on the web page once it's "downloaded" or does your app allow the user to download an image and save it to disk, in the same way you might download a ZIP file?

If the image is for display only, why does it need to be 1.8Mb? If it's just being downloaded, surely it's legitimate (and desirable?) to allow the user to continue using your app while this is going on. After all, a 1.8Mb image will take nearly 5 minutes to download on a 56Kbps modem; that's still 30 seconds on standard (UK) broadband.

Jules
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Size of scanned image is around .6 to .7 MB. But when I download it & present it on the page it's size is around 1.8 MB. I am not saving the image on client machine. (I checked the size of image, which was approx. 2-3 times more. This is surprise for me (size of image) . That's why I have posted the code, which is downloading the image.



I would highly appreciate your suggestions.


Thanks & regards,
Sanjeev.

*(Any reason for 6th posting dated "posted August 25, 2004 05:24 AM")

[ August 30, 2004: Message edited by: sanjeevmehra mehra ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still trying to understand what you are attempting. See if this description is correct. You want to have a JSP generated HTML page which includes an <img tag - only one img per HTML page. This page also provides "previous" and "next" page links.
The img source is a servlet that reads an image from a URLConnection.
Your problem is that if the user clicks the next page link before the image is loaded, the new page does not get an image.
If that is a correct statement, I would put more monitoring/log statements in the servlet that is supposed to serve the image. Perhaps the second attempt to open a URLConnection to the image source is getting refused because the first connection is still open. I don't see any code to catch and log exceptions there.
Bill
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjeev,

OK, I have a few suggestions and observations which I hope may be of use to you.

Firstly, I assume that you have a good reason for using a URLConnection in your servlet to locate the image, e.g. not wanting the real image URL to appear in the HTML source. If that's not the case then I suggest you just use the SRC attribute of the HTML IMG tag as I can't see what other benefit you're getting.

Secondly, unless you have a very good reason for wanting to serve a very detailed and/or very large image on your page (600-700Kb is still a massive image for the web). Best practice guidelines suggest that your whole page should be no more than 100Kb at the outside as the vast majority of users won't wait around for a larger page to download. If you have control over the images I suggest you optimise them for the web using a tool such as Adobe Photoshop.

You can easily reduce an image file of the size you're dealing with for presentation purposes by a factor of 10 without significant (or even any) perceptible degradation. You can always give the user an option to download the original image, accompanied by a suitable warning (i.e. this will take a few minutes on your 56Kbps modem).

Regarding the apparent difference in image size, I'm not sure what's going on there. Are you sure you have it right? How are you measuring the file sizes? If the image displays correctly in the browser it's extremely unlikely to be any different in size to that which you've read from the original URL.

Bill's point about logging is a good one. You need to know what's going on at the back end in detail. If you have two requests accessing your servlet simultaneously they'll both try to open a URL connection to locate an image. Does that work? (check your code with logging)

Another thought is that, if the image on your first page is downloading and you click on Next to navigate away from that page, I would expect the browser to give up on downloading that image and expend all its energies on the new page. Think about it in terms of any slow Internet page: by navigating away you're basically saying you're not interested in that page any more. I think I'm right in saying that.

So, where does all that leave you?

Jules
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill

Still trying to understand what you are attempting. See if this description is correct. You want to have a JSP generated HTML page which includes an <img tag - only one img per HTML page. This page also provides "previous" and "next" page links.
The img source is a servlet that reads an image from a URLConnection.

yes, you are right.




Your problem is that if the user clicks the next page link before the image is loaded, the new page does not get an image.

user gets whole (jsp) presentation immediately & also the prev. & next links.
If user waits to get download image or even a fraction of the image & clicks the prev or next link after that then (it throws the exception, which I am catching & ignoring) it shows the newly requested image in the template.
my problem is that
if user clicks on prev. or next link before it shows(download) even a fraction of image to the presentation page, user gets the first image (first image on the second (new) request).


Perhaps the second attempt to open a URLConnection to the image source is getting refused because the first connection is still open.

It does not refuse but takes time to entertain the new request. Whenever a new request comes before complete download of image; it throws exception for first image (which I am catching & ignoring) & it entertains the new request.

I am really thankful to you for giving so much time.

I think problem is, as you mentioned, connection from server (MyServlet class) is entertaining single request at time. Please post the suggestions or idea's you have.






Jules

Firstly, I assume that you have a good reason for using a URLConnection in your servlet to locate the image, e.g. not wanting the real image URL to appear in the HTML source. If that's not the case then I suggest you just use the SRC attribute of the HTML IMG tag as I can't see what other benefit you're getting.

you are right that it is possible to use the same link as SRC; <img SRC="URL of image">; but here problem is that user gets popup message that this contains secure & non-secure content, do you want to download it. I want to avoid this situation. (With direct access to the image, user gets the same size .gif image not 4 times more with .bmp image).
(I am using https not http for this application.)
Any other suggetion




Secondly, unless you have a very good reason for wanting to serve a very detailed and/or very large image on your page (600-700Kb is still a massive image for the web). Best practice guidelines suggest that your whole page should be no more than 100Kb at the outside as the vast majority of users won't wait around for a larger page to download. If you have control over the images I suggest you optimise them for the web using a tool such as Adobe Photoshop

It�s already optimized; believe me. I mean size cannot be reduce, for example not exactly but it's like scanning image of a newspaper. IT's not 600-700 kb but around 400+kb gif.
(Sorry I checked it again original size is around .4 MB not .6or .7MB, but the downloaded image (.bmp) is same as I mentioned 1.8 or more).


Regarding the apparent difference in image size, I'm not sure what's going on there. Are you sure you have it right? How are you measuring the file sizes? If the image displays correctly in the browser it's extremely unlikely to be any different in size to that which you've read from the original URL.

I am measuring the size of image through usual way, properties of the file (at source side).
At client side, saving the image from browser to hard disk, using save as, through right click on the image and checking the properties (size).
On source side it is .gif image with size .4 MB but when I try to save from client browser to client machine it save it as .bmp format with 1.8 MB.

Thanks for the concerns.


And if you have any other suggestions or alternatives, please share that.



Thanks & Regards,
Sanjeev.
[ August 30, 2004: Message edited by: sanjeevmehra mehra ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! I bet that in your servlet, the iURLConnection is an instance variable.

Probably the others are too. Since there is only one copy shared by all requests, no wonder it gets confused as to which image to show.

You should never use an instance variable in a servlet for data that changes between requests.
Bill
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, as simple as that. :roll:

There may still be some mileage in pursuing that <img src="url"> option. I know that you can get rid of IE's helpful little message though I can't remember exactly how. Basically you have to serve the images via HTTPS. Try using <img src="https://www.mycompany.com/images/myimage.gif">. I'm not convinced that works, however.

As for your Save As BMP, I think that's just IE being a PITA. It's probably not happy with your GIF format or something. Anyway, if you're reading a 400K GIF via your URLConnection you'll get a 400K GIF in the browser. That's still a minute at 56Kbps but if your users want to view your scanned documents then I guess they'll be expecting to wait that long. Might be an idea to give a warning though.

Anyway, making those variables local will solve your problem.

Jules
 
sanjeevmehra mehra
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Probably the others are too. Since there is only one copy shared by all requests, no wonder it gets confused as to which image to show.
You should never use an instance variable in a servlet for data that changes between requests.


It's bit disappointing for me taht I didn't took care for this. I am extremely sorry for all unnecessary inconvenience.

Thanks Bill & Jules for your instants suggestions. Jules, my application is over https & existing application is http base (URL for image).
(That why user gets message that �this contains secure & non-secure content, do you want to download it�; presentation content is from secure (https) & image is from http)


Please give me some idea about 6th posting.

Thanks & regards,
Sanjeev.


Thanks a lot, I have made the chnages & it's working fine.
Thanks & Regards,
Sanjeev.
[ August 31, 2004: Message edited by: sanjeevmehra mehra ]
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know about the "strange behaviour" in the JSP in the 6th posting.

Don't bother creating a separate thread; it's a waste of time. Your separate URLConnections are already running in separate threads on the server. If you kick off a thread from one of them you'll only have to make the parent thread wait (join) or your image won't be contained in the response. Forget about it.

Ways to speed up processing, if that's what you want, are:
  • Reduce the size of the images and/or optimise further; this is probably worth a try as you may be able to do it without loss of detail
  • Store the images locally to your current web app. That way you can cut down the overhead of the URL connection, extra network round-trips, and added code complexity by using <img src=""> as nature intended.
  • Include a warning that images will take time to download so that you don't lose your users (OK, that one doesn't speed it up )



  • Jules
    [ August 31, 2004: Message edited by: Julian Kennedy ]
     
    sanjeevmehra mehra
    Ranch Hand
    Posts: 75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    I am not using new Thread in my application.
    I asked reason for 6th posting just for the information purpose.
    Thanks For the Tips .

    Thanks & Regards,
    Sanjeev.
    [ August 31, 2004: Message edited by: sanjeevmehra mehra ]
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!