Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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:

    XML Code:
    <img src= "ImageServlet" width = "40" height = "50" alt="<%= bean.getItemid() %>"
    the image is not displayed at all.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default 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?

  3. #3
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default 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.

  5. #5
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Geting image from a servlet in a jsp

    OK, after clicking on page source, this is what is seen by the browse:

    XML 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.,
    XML Code:
    <img src= "c:myimagefile" width = "40" height = "50" alt="1"/>
    it works without any problems.
    Last edited by Dreaming; 01-23-2012 at 03:40 PM.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default 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?

  7. #7
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default 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?

  9. #9
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Geting image from a servlet in a jsp

    When I use <mywebsite>/ImageServlet the image is displayed.

  10. #10
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: Geting image from a servlet in a jsp

    which is the location of the jsp?.
    Try
    PHP Code:
    <img src= "/ImageServlet" width = height = "50" alt="1"/> "40"/>

  11. #11
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  12. #12
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default 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

  13. #13
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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

    XML 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>

  14. #14
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: Geting image from a servlet in a jsp

    The url is with bar
    PHP Code:
    <url-pattern>/ImageServlet</url-pattern>
    PHP Code:
    <img src="/ImageServlet">

  15. #15
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  16. #16
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Geting image from a servlet in a jsp

    Quote Originally Posted by arielb View Post
    The url is with bar
    PHP Code:
    <url-pattern>/ImageServlet</url-pattern>
    PHP 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.

  17. #17
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: Geting image from a servlet in a jsp

    puts this:
    <head>
    <meta http-equiv="Pragma" content="no-cache">
    </head>

  18. #18
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  19. #19
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Geting image from a servlet in a jsp

    Quote Originally Posted by arielb View Post
    puts this:
    OK, thanks, did that, no difference.

  20. #20
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: Geting image from a servlet in a jsp

    Html code prevents the cache store to the existing one you should clean it.

Page 1 of 2 12 LastLast

Similar Threads

  1. Image Upload use Only servlet and jsp
    By RaviGupta0709 in forum Java Servlet
    Replies: 2
    Last Post: 05-31-2011, 07:49 AM
  2. How to upload image in jsp/servlet
    By ibhaviik in forum Java Servlet
    Replies: 11
    Last Post: 11-16-2010, 12:32 PM
  3. geting value from text
    By Mekonom in forum New To Java
    Replies: 10
    Last Post: 12-15-2009, 01:22 AM
  4. sending image file from JSP to Servlet
    By ravian in forum Advanced Java
    Replies: 2
    Last Post: 01-10-2008, 03:34 PM
  5. problems when loading an image in servlet
    By oregon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •