• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using a servlet to load images

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to load an image into a servlet and send the response to a JSP, but my code doesn't work :-(

In the JSP, the servlet is called like that:



And this is the servlet's doGet method:


The image is in the folder WebContent/images. Any ideas what might be wrong?
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where do you ever "write" the output?

WP
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your src URL is also suspect. The URL should be a server-relative URL starting with the context path.

as in: src="${pageContext.request.contextPath}/servlet-path"

which in your case would seem to be:
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two approaches that might help you debug quicker:
  • Use an HTTP tool to actually see the request and response. Chrome and Safari have one built right in. For Firefox, there are plugins such as HttpFox.
  • Rather than editing two files: a JSP and a servlet, just hit the servlet URL with the browser. When the image appears, you know the servlet is ready. Could cut down on turn-around time after a change.
  •  
    Daniel Zuckermann
    Ranch Hand
    Posts: 50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, at least calls the ImageLoader servlet.

    However, i am not sure which is the best way to write and read the image. Is ImageIO.read(input) a good choice?
     
    William P O'Sullivan
    Ranch Hand
    Posts: 859
    IBM DB2 Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Do a search on "serving images via servlet".

    I found a 10 line example, which I already hinted at your problem from.

    WP
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Daniel Zuckermann wrote:Well, at least calls the ImageLoader servlet.


    While it may work now, this way of forming URLs is fragile and easily broken. Just because you got something to work doesn't mean that it's working well.
     
    Ranch Hand
    Posts: 115
    Eclipse IDE Tomcat Server Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i you want the load images or any kind of binary data , using servlet , instead of using another toolkit libraries the servlet 3.0 api have good support to upload binary data and easy to use.
     
    Daniel Zuckermann
    Ranch Hand
    Posts: 50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you guys. I got it working with , but I still have some questions regarding the whole architecture of my simple application, which should basically generate some bean objects from an xml file and then display the text and the image for each data set.

    The xml should look similar to this:



    My question is if it's ok to read the text directly from the
    bean object like



    or is this consireded bad practice?


     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You should not be using jspx documents, but plain old JSP files. jspx is intended as an intermediary format or for generation by machine, and not indented for hand coding.

    And you should be using the EL to fetch data, not scriptlets or jspx expressions.
     
    It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic