本文整理汇总了Scala中org.eclipse.jetty.servlet.ServletHolder类的典型用法代码示例。如果您正苦于以下问题:Scala ServletHolder类的具体用法?Scala ServletHolder怎么用?Scala ServletHolder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ServletHolder类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: RackServer
//设置package包名称以及导入依赖的类
package coder36.rack
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}
import com.typesafe.scalalogging.LazyLogging
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder}
import scala.collection.mutable.Map
class RackServer extends LazyLogging {
val port : Int = 8080
var path : String = ""
val apps = scala.collection.mutable.Map[String,RackServlet]()
def map(path: String) : RackServer = {
this.path = path
this
}
def onto(rack: Rack) = {
apps(path) = new RackServlet(rack, path)
}
def start() : RackServer = {
val context = new ServletContextHandler(ServletContextHandler.SESSIONS)
context.setContextPath("/")
val server = new Server(port)
server.setHandler(context)
apps.foreach( app => context.addServlet(new ServletHolder(app._2), (app._1 + "/*").replaceAll("//", "/")) )
println(s"\u001B[34mRack started... listening for HTTP on /0.0.0.0:${port}")
server.start
this
}
}
class RackServlet(val rack: Rack, val path: String) extends HttpServlet {
override def service(request: HttpServletRequest, response: HttpServletResponse) {
val res = Middleware.BaseRack( rack, request, response ).call(Map('base_path -> path))
val status = res._1
val headers = res._2
val body = res._3
response.setStatus(status)
headers.foreach( (h) => response.addHeader(h._1, h._2))
if( body != null ) response.getWriter().write(body)
}
}
示例2: Server
//设置package包名称以及导入依赖的类
package works.weave.socks.aws.orders.main
import org.eclipse.jetty
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
import org.glassfish.jersey.servlet.ServletContainer
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import scala.collection.JavaConverters._
import scala.util.control.NonFatal
import Server._
import org.eclipse.jetty.util.log.Slf4jLog
@Component
class Server(val jerseyApp : JerseyApp) {
def run() : Unit = {
jetty.util.log.Log.setLog(new Slf4jLog())
val port = System.getenv().asScala.getOrElse("PORT", "80").toInt
val context = new ServletContextHandler(ServletContextHandler.SESSIONS)
context.setContextPath("/")
val jettyServer = new jetty.server.Server(port)
jettyServer.setHandler(context)
val servletContainer = new ServletContainer(jerseyApp)
val servletHolder = new ServletHolder(servletContainer)
context.addServlet(servletHolder, "/*")
val jerseyServlet = servletHolder
jerseyServlet.setInitOrder(0)
try {
Log.info("Starting jetty")
jettyServer.start()
Log.info("Jetty started")
jettyServer.join()
Log.info("Jetty exited")
} finally {
Log.info("Destroying jetty")
try {
jettyServer.destroy()
} catch {
case NonFatal(e) => Log.error("Error during Jetty shutdown", e)
}
}
}
}
object Server {
val Log = LoggerFactory.getLogger(classOf[Server])
}