Bootstrapping a web server in Scala

后端 未结 8 1716
走了就别回头了
走了就别回头了 2021-02-01 10:32

The following is possible using Python:

$ apt-get install python
$ easy_install Flask
$ cat > hello.py
from flask import Flask
app = Flask(__name__)

@app.rou         


        
8条回答
  •  广开言路
    2021-02-01 10:49

    You could use an embedded Jetty Server:

    /*
     * Required Libs: Jetty, Servlet API
     *
     * Compile:
     *   scalac -cp jetty-6.1.14.jar:jetty-util-6.1.14.jar:servlet-api-2.5-6.1.14.jar WebServer.scala
     *
     * Run:
     *  scala -cp .:jetty-6.1.14.jar:jetty-util-6.1.14.jar:servlet-api-2.5-6.1.14.jar WebServer
     */
    import org.mortbay.jetty.Server
    import org.mortbay.jetty.servlet.Context
    import javax.servlet.http.{HttpServlet,
                           HttpServletRequest, 
                           HttpServletResponse}
    
    class HelloServlet extends HttpServlet {
      override def doGet(req : HttpServletRequest, resp : HttpServletResponse) =
        resp.getWriter().print("Hello There!")
    }
    
    object WebServer {
      def main(args: Array[String]) {
        val server = new Server(8080)
        val root = new Context(server, "/", Context.SESSIONS)
        root.addServlet(classOf[HelloServlet], "/*")
        server.start()
    
        println("Point your browser to http://localhost:8080/")
        println("Type [CTRL]+[C] to quit!")
    
        Thread.sleep(Long.MaxValue)
      }
    }
    

    In case you target for a LOC comparison, You use the HTTP server embedded with the Sun JDK. Another solution could be to use javax.xml.ws.Endpoint and the Provider API.

提交回复
热议问题