Configure static content for webapp with Jetty 9

Can someone tell me how to set up static content for webapp with Jetty 9. I tried to do this for a while, and I was out of luck. All requests for static content (e.g. css / * or img / *) just go straight to my servlet handler for /.

root
    |-war
        |-css
        |-img
        |-js
        |-WEB-INF
                |-web.xml

My web.xml looks like this:

<webapp>
    ...
    ...
    <servlet-mapping>
        <servlet-name>App</servlet-name>
        <url-pattern>/app</url-pattern>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- SOMETHING HERE FOR STATIC CONTENT ?? -->

</webapp>

I just don’t understand what to do with this example from the berth documents:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/scratch</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="resourceBase">/home/jesse/scratch</Set>
      <Set name="directoriesListed">true</Set>
    </New>
  </Set>
</Configure>

I tried playing with it and putting the file in different places, but I can not get it to work. My servlets are handled fine, but my pages do not have styles, images or js because they cannot find the content.

+4
source share
2 answers

I ended up just hitting this in my web.xml:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
</servlet-mapping>

, default , , mime, , .

+1

Jetty Maven, : http://jamesphilipps.wordpress.com/2012/06/13/serving-static-content-with-the-maven-jetty-plugin/

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>7.1.0.RC0</version>
  <configuration>
    <contextHandlers>
      <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
        <contextPath>/static</contextPath>
        <resourceBase>src/main/webapp/static</resourceBase>
      </contextHandler>
    </contextHandlers>
  </configuration>
</plugin>
+1

Source: https://habr.com/ru/post/1538850/


All Articles