Embeded Tomcat with webapp directory within the cl

2019-08-07 00:52发布

问题:

So I have a Java application that use to be packaged as a war and then deployed to Tomcat, but now I have it setup so it all runs straight from a jar file using embedded Jetty as follows:

class JettyServer extends ServerTrait {

 val server = new Server()
  val connector = new ServerConnector(server)
  connector.setPort(BigSenseServer.config.options("httpPort").toInt)
  server.setConnectors(Array(connector))

  val context = new ServletContextHandler()
  context.setContextPath(BigSenseServer.webRoot)
  context.addServlet(new MasterServlet().getClass, "/*")
  context.addEventListener(new InitLoggingListener())
  context.addEventListener(new DBUpdateListener())

  val fileContext = new WebAppContext()
  fileContext.setContextPath(BigSenseServer.contentRoot)
  fileContext.setResourceBase(BigSenseServer.getClass.getResource("/io/bigsense/web").toExternalForm)

  val handlers = new HandlerCollection()
  handlers.setHandlers(Array( fileContext, context, new DefaultHandler()))
  server.setHandler(handlers)

  override def startServer() {
    server.start
    server.join
  }

  override def stopServer() {
    server.stop
  }
}

The webroot is /bigsense/api and the contentRoot is /static. In this configuration, static files like CSS and Javascript are served from the /io/bigsense/web package (kept in src/main/resources in SBT). In my original, the context-root was /bigsense and the servlet was mapped to api/*, so all the static content could be served directly from /bigsense/{js,css,whatever}.

I couldn't figure out how to get Jetty to do the same thing, but the current setup listed above works fine and I adjusted all my templates to get that static path from the same config object (which has a backend in a property file).

I want to create an embedded Tomcat implementation as well and I've read several guides, but they all seem to want a real webapp base directory. I can't find any examples which either just map a servlet directory without a webapp base or take the webapp base from the classpath (in a jar) instead of a real physical directory. I've tried things similar to the following:

EDIT Got the servlet working with the following. Now I just need the ServletContextListneres and a way to server files from the jar:

class TomcatServer extends ServerTrait {

  val tomcat = new Tomcat()
  tomcat.setPort(BigSenseServer.config.options("httpPort").toInt)
  val tmp = new File(System.getProperty("java.io.tmpdir"))

  val ctx = tomcat.addContext(BigSenseServer.webRoot,tmp.getAbsolutePath)
  Tomcat.addServlet(ctx,"bigsense",new MasterServlet())
  ctx.addServletMapping("/*","bigsense")

  override def startServer() = {
    tomcat.start()
    tomcat.getServer().await()
  }

  override def stopServer() = tomcat.stop
}

So I have a similar setup to Jetty for the main servlet. I can't find any functions on the context object for adding in the two ServletContextListner objects I have. I also need to be able to serve my static context from the jar on contentRoot (/static).