2

I'm using an embedded Jetty server in a Scalatra app. The issue is that it serves css files with text/html content type:

Here is the main method:

package yard.web

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.webapp.WebAppContext
import org.scalatra.servlet.ScalatraListener

object JettyMain {
  def main(args: Array[String]) {
    val server = new Server(9080)
    val context: WebAppContext = new WebAppContext("src/main/webapp", "/")
    context.setServer(server)
    context.setInitParameter(ScalatraListener.LifeCycleKey, "yard.web.ScalatraBootstrap")
    context.addEventListener(new ScalatraListener())
    server.setHandler(context)

    server.start()

    println("Press ENTER to stop server")
    Console.readLine()
    server.stop()
    server.join()
  }
}

The file is located at src/main/webapp/libs/bootstrap/css/bootstrap.css, and served with:

$ curl --head http://localhost:9080/libs/bootstrap/css/bootstrap.css
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Last-Modified: Sat, 06 Apr 2013 14:30:35 GMT
Content-Length: 127247
Accept-Ranges: bytes
Server: Jetty(8.1.10.v20130312)

Why is Jetty thinking it's an html file?


Here is the ScalatraBootstrap class for completeness:

package yard.web

import org.scalatra.LifeCycle
import javax.servlet.ServletContext
import yard.Settings
import yard.db.Store

class ScalatraBootstrap extends LifeCycle {
  override def init(context: ServletContext) {

    val settings = Settings.default
    val db = Store(settings).db

    context mount (new MainServlet, "/")
  }
}

Update: Using a ResourceHandler causes the css to be served with correct content type. However, the app doesn't work :(

Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90

1 Answers1

6

The CSS file is typically served from the org.eclipse.jetty.servlet.DefaultServlet.

Which is declared in the etc/webdefault.xml file in the distribution.

Since you are using embedded mode, you'll want to provide this manually by calling WebAppContext.setDefaultsDescriptor(String) with the path to your etc/webdefault.xml file.

And finally, the mime types themselves are loaded by the DefaultServlet via the mime.properties file, which is loaded by Jetty via a call to Classloader.getResource("/org/eclipse/jetty/http/mime.properties").

Note: the mime.properties file is found in the jetty-http-8.1.10.v20130312.jar file.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • `mime.properties` is there and has the correct css line. I'm using sbt to manage the deps, btw. Using `ResourceHandler` fixes the content type, but I can't find a way to combine it with the `WebAppContext`. Also, I'm not actually using any `web.xml` file. – Emil Ivanov Apr 06 '13 at 15:41
  • 1
    Doesn't matter that you have a `web.xml` or not. The [webdefault.xml](http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-webapp/src/main/config/etc/webdefault.xml?id=jetty-8.1.10.v20130312) is still required. Grab yourself a copy of it, modify it to suit your needs (such as removing the jsp support), get a file reference to it, and set that file reference via `WebAppContext.setDefaultsDescriptor(String)`. This is assuming that DefaultServlet is serving your CSS file. It could be Scala itself serving the CSS, if so, you'll need to configure Scala. – Joakim Erdfelt Apr 06 '13 at 18:28
  • you are correct. The problem was that Scala was serving the css. When I added a `web.xml` with the correct settings to have `default-servlet` serve them everything worked. – Emil Ivanov Apr 07 '13 at 07:27