Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java HTML URL construction

 
peter m hayward
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every body I am generating HTML via java having just a bit of trouble with url construction so i am typically using

data.writeTofile("<h2><font color = blue>is a stock picture</font></h2>"); which works just fine together with the rest of the code to make the webpage

now i have a working url which pulls in a picture of a book, to be accurate its a picture of a blank to indicate that a picture is not available you can view this by pasting this http://www.communicatingwithpictures.talktalk.net//Cimages/no-image.gif in to the address bar or this code in an html document


data.writeTofile("          <img name='theBook'src=' http://www.communicatingwithpictures.talktalk.net//Cimages/no-image.gif' width='167' height='280'/>");

now what i am trying to do is replace the last part with a variable so that i can select different books to display at the moment the are a few books on this webspace an example of which is The Arabian Nights) Childrens Classics so changing no-image.gif for 9781858137162.jpg works fine

If however i substitute no-image.gif for a variable lets call it the lastbit and try to construct the url then it does not work can anyone tell me why or tell me how to solve the problem

so just to be clear


data.writeTofile("<h2><font color = blue>is a stock picture</font></h2>"); i use this type of arrangement to generate the html


data.writeTofile("          <img name='theBook'src=' http://www.communicatingwithpictures.talktalk.net//Cimages/no-image.gif' width='167' height='280'/>");

works fine

String lastbit ="no-image.gif"; this is so i can change to say lastbit ="9781858137162.jpg";

does not work why?

data.writeTofile("          <img name='theBook'src=' http://www.communicatingwithpictures.talktalk.net//Cimages/lastbit' width='167' height='280'/>");

if i change no-image.gif to 9781858137162.jpg manually it does work




 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
peter m hayward wrote:does not work why?

data.writeTofile("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<img name='theBook'src=' http://www.communicatingwithpictures.talktalk.net//Cimages/lastbit' width='167' height='280'/>");


That is a string literal. It does not use or attempt to use any variable. Just because you have a string with the same couple of characters as the variable name does not mean that those characters will be replaced by the variable. You have to actually use the variable. Maybe something like this will work:


I am sure there are better libraries for writing HTML in Java than you writing your own. This all looks very fragile to me. I think you should investigate some tool to use to handle the HTML writing for you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic