7

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

3 Answers 3

20

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"/>
Sign up to request clarification or add additional context in comments.

2 Comments

you wrote 'Anything in is reachable directly', did you mean in docroot?
@Blankman yes. Anything in the docroot can be served directly. Anything in WEB-INF and below cannot.
7

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.

2 Comments

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
I referenced an image from my index.jsp, image didn't render either.
0

The general answer is - the root of your web application is webapp. Dynamic resources (as JSP pages or Freemarker templates) would better off be in a web-inf/ subfolder (they are accessible through classloader but not from a direct browser request).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.