Discussions

Web tier: servlets, JSP, Web frameworks: Tomcat and Servlet relative path

  1. Tomcat and Servlet relative path (6 messages)

    I'm confused about where Tomcat maps html paths to within a Servlet. Suppose I have a servlet the outputs some HTML like
    <body background="images/bg.gif" bgcolor="#FFFFFF">

    I put the Servlet into \webapps\ROOT\WEB-INF\classes for the sake of argument.

    I can run the servlet by pointing a browser to http://localhost:8080/servlet/welcome

    but where do I need to put the Images directory so "bg.gif" is picked up? (It isn't \webapps\ROOT\WEB-INF\classes\images" for instance).

    Threaded Messages (6)

  2. Tomcat and Servlet Relative Path[ Go to top ]

    Yep - you can't put static resources such as html docs, images, or javascript files into the classes directory - it's stated in Servlet2.4 spec that anything under WEB-INF cannot be accessed directly by a browser.(so you will place JSP pages in there - it prevents a user from accessing a JSP directly - that is all requests will have to go through a front-controller servlet beforehand)

    You will need to put your images somewhere that the webbrowser (which will download the image) can see it, which will be at the same 'level' as WEB-INF. Hopefully this primitive directory structure diagram will help:

    --webapps\ROOT
         - WEB-INF
         - images
              - bg.gif
  3. Tomcat and Servlet Relative Path[ Go to top ]

    OK – makes sense. Is there anyway I can avoid having to go up a directory from the location of the servlet in the Path

    E..G:
    <body background="../images/bg.gif" bgcolor="#FFFFFF">

    Instead of <body background="images/bg.gif" bgcolor="#FFFFFF">

    ?

    Charles
  4. Tomcat and Servlet Relative Path[ Go to top ]

    You don't need to go up a directory - it is the web browser that is going to resolve and load the image, not the servlet. The servlet will just generate HTML, which may refer to other resources, that the browser will need to load. So the web-browser sees the "root" of your web application as the directory containing WEB-INF and Images directory in my last post.

    So if your servlet reads:
    <body background="images/bg.gif" bgcolor="#FFFFFF">
    when you run the servlet in a web browser, the image should load.

    Hope this helps
  5. Tomcat context[ Go to top ]

    When you define a context for this webapp, you give it a document root.

    So for instance:-

    http://localhost/mywebapp could point to:-

    /var/www/html/website_files/

    for sake of argument.

    For tomcat, under here you will have a WEB-INF folder, where you can put your classes and jar files, together with a web.xml file.

    You map the servlet URL to your servlet using a mapping in the web.xml. This is a virtual mapping, no physical directories need to exist. Tomcat knows that the files it wants will be in WEB-INF/classes or WEB-INF/lib

    So in a nutshell, you ordinary files sit in website_files, and your servlets under WEB-INF.
  6. Hi

    Every web application has the following directory structure

    ROOT(Folder)
      | - Where you place the dynamic content namely html, jsp and images.
      |
      WEB-INF(Folder)
        web.xml
          - xml file describing the web applications and the dynamic content.
        lib(Folder)
          - where you place the jar files used by the application.
        classes(Folder)
          - where you place the dynamic content namely your servlets.

    Any relative reference like "images/bg.gif" in a jsp will refer to the same folder where jsp resides. For example if the jsp resides in "<Root>/web/jsp/" then a reference to "images/bg.gif" will look for the image in "<Root>/web/jsp/images/" folder.

    In case of servlets any relative reference will always refer to the root directory. Hence the html code "<body background="images/bg.gif" bgcolor="#FFFFFF">" will look for the image in "<Root>/images/" folder.

    Cheers
    Kanag
  7. Great replies :)

    Regards,

    Praveen