jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes images in HTML/JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "images in HTML/JSP" Watch "images in HTML/JSP" New topic
Author

images in HTML/JSP

Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
Hi folks,

This is a pretty general question and I hope I am in the appropraite forum.

In HTML one can display an image with the following code:

<img src="path/image.jpg" width = "70" height="100" />

Now, if I want to display an image in a jsp and then use some logic to redisplay a second image in the same location on the page, how do I do it? The above is hard coded. In a jsp I cannot build a string and then display it, i.e.

<% String i1 = "path/image1.jpg";
String i2 = "path/image2.jpg";

.. some logic to assign either i1 or i2 to s..

String h = "<img src=" + s + "width=/""70" ... etc;
%>

as you see, although I have built a String, the HTML bit is still text. I can't think of a way of parameterising the HTML. I thought of actually writing different images into the file located at

path/image.jpg

but this feels wrong, since in web apps generally, writing to the deployed files is not usually recommended.

So, how does one display different images on a page?

with best regards,
Simon Ingram
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41885
    
  63
In a jsp I cannot build a string and then display it, i.e.

<% String i1 = "path/image1.jpg";
String i2 = "path/image2.jpg";

.. some logic to assign either i1 or i2 to s..

String h = "<img src=" + s + "width=/""70" ... etc;
%>


Why not? What prevents you from doing this? If the logic to decide between i1 and i2 can be decided in the server, then you can use JSP. If it depende on something on the client, or if you want to change images once the page has been generated and served to the browser, then you need to use a JavaScript solution.


Ping & DNS - my free Android networking tools app
Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
Hi Ulf,

the logic is on the server. the problem is not making the decision, it is the technical matter of displaying different images on a web page. Imagine the page shows some playing cards (jpeg files). Is it possible to keep the table static and use the servlet to write different images (cards) to the same location on the page? I can't see how to get round the fact that the HTML is not flexible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: images in HTML/JSP