Where to store static files like html/css/javascript in a jetty project?

14,988

Solution 1

This isn't so much a Jetty question as it is a general Java webapp question. If you plan to serve them out directly (like *.css, *.css, images, etc), put them somewhere above WEB-INF but below your docroot. Java WebApps are all the following basic directory structure.

<docroot>
  +WEB-INF/
    +lib/
    +classes/

Anything in <docroot> is reachable directly through straight up http. Anything WEB-INF and below is not. A really simple webapp with one page (index.jsp), one image in an images directory, and its configuration file (web.xml) would look like this.

index.jsp
images/bob.jpg
WEB-INF/
  web.xml
  lib/
  classes/

In index.jsp you could reference bob.jpg like...

<img src="images/bob.jpg"/>

Solution 2

This is really a Maven question rather than a Jetty question.

Typically you would put your images (etc) in the maven webapp directory - i.e. source/main/webapp/ (not under web-inf)

How you structure things underneath that is up to you, but it will mostly depend on how much content you are expecting to put in, and how you think it is best to organise it.

source/main/webapp/assets/images is fine, but so is source/main/webapp/images or source/main/webapp/static/.

Then, within your HTML, you reference the images using whatever path you put in beneath the webapp bit.

Share:
14,988
Blankman
Author by

Blankman

... .. . blank

Updated on June 05, 2022

Comments

  • Blankman
    Blankman almost 2 years

    I have a maven project that I run using jetty:

    $ mvn run:jetty
    

    Where in my project should I be storing my static files like HTML, CSS, Javascript, images?

    My layout is using a simple web app arch type:

    /src/main/java/webapp/web-inf/views/
    

    Should I just create a folder there named e.g. 'assets' ?

    And then my view pages will reference the /assets folder somehow? I'm confused as to what path I will use in my html pages to reference an image like:

    /assets/images/logo.png
    
  • Blankman
    Blankman about 12 years
    you wrote 'Anything in is reachable directly', did you mean in docroot?
  • Blankman
    Blankman about 12 years
    So I added test.html inside the webapp folder, ran mvn clean install, mvn jetty:run and it says no mapping found for /test.html I am browsing 127.0.0.1:8080/test.html
  • Blankman
    Blankman about 12 years
    I referenced an image from my index.jsp, image didn't render either.
  • Bob Kuhar
    Bob Kuhar about 12 years
    @Blankman yes. Anything in the docroot can be served directly. Anything in WEB-INF and below cannot.