• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a servlet to load images  RSS feed

 
Daniel Zuckermann
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you ever "write" the output?

WP
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
    • 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
    Chrome IBM DB2 Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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
    Author and ninkuma
    Marshal
    Posts: 66158
    146
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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.
     
    Singh Harmeet
    Ranch Hand
    Posts: 115
    Eclipse IDE Java Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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
    • 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
    Author and ninkuma
    Marshal
    Posts: 66158
    146
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • 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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!