Geting image from a servlet in a jsp
I am trying to get an image from a servlet. When I try to run the servlet alone, it works and the image is displayed. However, when I try to run it from the jsp with this:
Code:
<img src= "ImageServlet" width = "40" height = "50" alt="<%= bean.getItemid() %>"
the image is not displayed at all.
Re: Geting image from a servlet in a jsp
What html does the browser get from the JSP?
Is it different from when you run the servlet alone?
Re: Geting image from a servlet in a jsp
Well, just normal html codes as I pasted above. When I run the servlet alone, it works and the image is displayed. I did some tracing and it seems that the servlet is never called from the jsp. But I thought <img src= "ImageServlet" width = "40" height = "50" alt="<%= bean.getItemid() %>" should have called the servlet. If I substitutce src = "ImageServlet" with a local file on my PC, e.g., src ="C:/xyz.jpg", the image is displayed without any issues. What I don't get is why the servlet is not being called? I thought when the code reached img src="ImageServlet" that the servlet should be called automatically.
Re: Geting image from a servlet in a jsp
Well, from what you have posted in the OP, that tag isn't closed.
WHich is why Norm is asking what the HTML (ie what is seen on the client when you view source) is in both instances.
Because it is pretty certain to be different.
Re: Geting image from a servlet in a jsp
OK, after clicking on page source, this is what is seen by the browse:
Code:
<img src= "ImageServlet" width = "40" height = "50" alt="1"/>
and so on until the loop loops through all the items, which are 12, each time incrementing the image if (the alt field) by 1.
and as you can see the tag is closed. And as I said, if I replace it with, e.g., Code:
<img src= "c:myimagefile" width = "40" height = "50" alt="1"/>
it works without any problems.
Re: Geting image from a servlet in a jsp
You said "when I run the servlet alone it works"...so which of the above <img> tags works against the servlet?
Re: Geting image from a servlet in a jsp
The one where I used a local image file works; the one when I use the servlet as image source, does not work.
Re: Geting image from a servlet in a jsp
Right, so:
"When I try to run the servlet alone, it works and the image is displayed."
isn't actually the case.
When you use <your website>/ImageServlet in your browser what happens?
Re: Geting image from a servlet in a jsp
When I use <mywebsite>/ImageServlet the image is displayed.
Re: Geting image from a servlet in a jsp
which is the location of the jsp?.
Try Code:
<img src= "/ImageServlet" width = height = "50" alt="1"/> "40"/>
Re: Geting image from a servlet in a jsp
I don't know what you mean by your question of "which is the location of the jsp?" but I tried your code and still the same.
Re: Geting image from a servlet in a jsp
have reviewed the web.xml, that contains <servlet-mapping>
and <servlet> to ImageServlet
If you can put here, as written in the web.xml
Re: Geting image from a servlet in a jsp
I didn't understand what you meant but I guess you meant that I post here the web.xml file? This is the part for that servlet
Code:
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>servlet.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/ImageServlet</url-pattern>
</servlet-mapping>
<servlet>
Re: Geting image from a servlet in a jsp
The url is with bar
Code:
<url-pattern>/ImageServlet</url-pattern>
Code:
<img src="/ImageServlet">
Re: Geting image from a servlet in a jsp
I am also seeing some strange behaviour, probably the problem lies with caching. As I said before, when I run the servlet on its own, the image is displayed. Now if after running the servlet, I run the jsp, it displays the last image that was displayed when I ran the servlet. So it seems the images are being cached somewhere and instead of looping through all the images (have a for-loop in the jsp), it just basically does nothing and only tries to fetch the last image, or something. Don't know, it is confusing. If there is a way to disable caching, can someone tell me, then will see if that makes difference.
Re: Geting image from a servlet in a jsp
Quote:
Originally Posted by
arielb
The url is with bar
Code:
<url-pattern>/ImageServlet</url-pattern>
Code:
<img src="/ImageServlet">
That is not a problem at all. Actually all the url patterns for my servelts are like that and they have worked without me putting / before them, so that is definitely not the problem. And just to be sure, I tried that and, as expected, it made no difference.
Re: Geting image from a servlet in a jsp
puts this:
Quote:
<head>
<meta http-equiv="Pragma" content="no-cache">
</head>
Re: Geting image from a servlet in a jsp
I don't know. I tried reproducing the problem where the last image is displayed but could not, it just seems to happen randomly. Something is amiss somewhere.
Re: Geting image from a servlet in a jsp
Quote:
Originally Posted by
arielb
puts this:
OK, thanks, did that, no difference.
Re: Geting image from a servlet in a jsp
Html code prevents the cache store to the existing one you should clean it.