7 Replies Latest reply: Jan 8, 2009 3:30 PM by 843785 RSS

    How to link to external images and files from out.println?

    843785
      Hi Everyone,

      I have a need to reference external images and files from within the Java project I'm working on. For example, out.println needs to reference an image via embedded <IMG> tag code.

      I also have a need to reference a CSS file from within an XSL file being transformed by Saxon in Java.

      Any help would be appreciated. I'm used to straight-up HTML and PHP, but am not sure where to put images files when it comes to working in Java.

      Thanks much,
      - Matt
        • 1. Re: How to link to external images and files from out.println?
          DrClap
          Mateo1041 wrote:
          For example, out.println needs to reference an image via embedded <IMG> tag code.
          No, it doesn't. That method just writes a line of text to somewhere. It doesn't need to reference any images.
          I also have a need to reference a CSS file from within an XSL file being transformed by Saxon in Java.
          No, you don't. There aren't any XSLT functions that do anything with CSS files.
          Any help would be appreciated. I'm used to straight-up HTML and PHP, but am not sure where to put images files when it comes to working in Java.
          You're going to have to look at your context straight instead of considering your entire system as a big ball of string. My guess: you are trying to generate HTML, and you are trying to output references to various files into that HTML. Such as <img> elements and references to CSS files. But your code doesn't need to access those files or reference them or do anything at all with them. All it has to do is to write their names into the HTML.

          Now probably you are having trouble with the concept that the file names you output are relative to the URL which output that HTML and so your code is not writing the correct relative paths for those files. That has nothing to do with the code, though. Just choose the correct relative paths and have the code write them.
          • 2. Re: How to link to external images and files from out.println?
            843785
            Let me clarify. I need to display an image, but am not sure where to place the image file. While I'm sure I can place it in any old directory, I would like to keep all files of the project within the Java directory structure of the project for easy distribution.

            Please re-read my question. I need to reference a CSS file in much the same way as the image file. I never said anything about an XSLT function.
            <link rel="stylesheet" type="text/css" href="style.css"/>
            Where can I place all these external files so as to keep them within the same directory structure and have them be easily portable? With PHP, I put all my files in the same directory and voila. The Java directory structure is more more complex (source files, build files, etc) and this is why I'm asking.

            Yes, I am looking to write HTML and reference files. My question is where to place said files.
            • 3. Re: How to link to external images and files from out.println?
              DrClap
              On the server, you put the images in the same directory as the HTML files if you want to access them via <img src="googoo.gif"> from those HTML files. Or you can put an "images" directory in that directory and then access them via <img src="images/googoo.gif">. But I'm sure you knew that already, based on what you said there. Same for CSS files.

              And if you are generating the HTML, then the result is going to have a URL which is the URL of the request that caused you to generate the HTML. Image and CSS files should be specified relative to that URL, exactly as if the HTML was actually there.

              So all that business about XSLT and so on is irrelevant. All you need to know is the URL of the request that fires off the generation of the HTML.

              And sorry, I don't know how to set up your IDE to make things come out that way. Whatever IDE you are using. That's a question about the IDE anyway, not about Java programming.
              • 4. Re: How to link to external images and files from out.println?
                843785
                Thanks, I appreciate the reply. I'm running Eclipse 3.4.1 in a Windows XAMPP/Tomcat environment. While I can place my files within some random XAMPP directory, it sounds difficult to transport. I'll do some reading on Eclipse itself and see if that answers my question. I'd ideally like the following external files to be available in something that is easy to package and follows the Java directory structure:

                * Image files
                * XSL file
                * CSS file

                Here's how it works. My Java project uses HttpClient and Saxon to do an XSL tranformation on the XSL file and incoming XML data from an external web service. The XSL file itself is what references the other CSS and image file.

                - Matt
                • 5. Re: How to link to external images and files from out.println?
                  DrClap
                  Mateo1041 wrote:
                  The XSL file itself is what references the other CSS and image file.
                  As I said before, that's not a good way to look at it. A better way to look at it is, the HTML which the XSL file generates is what references the CSS and image files. As far as the XSL file is concerned, those things are just text.
                  • 6. Re: How to link to external images and files from out.println?
                    843785
                    Which is great, but still doesn't answer my question. :-) I currently see two possible options:

                    1) Manually find the location in the Java project tree in which to place the external files (which hasn't worked so far)

                    -- or --

                    2) Import the files as resources into the Java project (Eclipse) and reference them that way

                    I just need to figure out which will work and be most portable for distribution.
                    • 7. Re: How to link to external images and files from out.println?
                      843785
                      I finally got this to work. Thanks to all who helped.

                      http://www.apachefriends.org/f/viewtopic.php?f=16&t=32649

                      My trouble with displaying external files such as images was due to a combination of things, most noticeably not fully understanding the web.xml file, how Tomcat structures things, nor the proper URL to use when viewing in a web browser.