1

As an experiment in scala I want to set up a basic website with scala as a server side language. This would not carry out many interactive tasks - it would simply write and distribute HTML in response to HTTP requests.

I don't know much about how the piping of web servers works, but I've written lots of HTML, CSS, JS and PHP and I envisage this working much like the PHP back end in wordpress - the client sends an HTTP request for a page such as example.wordpress.com/2012/06/18/example_blog and the PHP on the server compiles an HTML web page and returns it to the user.

Although I'm open to suggestions, I'd rather not use a full blown framework such as Lift as I'm trying to build from the ground up. I'm only interested in the very basic task of taking an HTTP request as input and outputting an HTTP response.

GabrielG
  • 189
  • 1
  • 9

2 Answers2

3

Usually you'd take a Java EE server and implement a Servlet.

Here is one:

package myservlet

import javax.servlet.http._

class Servlet extends HttpServlet {
  /** Servlet's main method. */
  protected def welcome (request: HttpServletRequest, response: HttpServletResponse): Unit = {
    response.getWriter.write ("hi")
  }
  override def doGet (request: HttpServletRequest, response: HttpServletResponse): Unit = welcome (request, response)
  override def doPost (request: HttpServletRequest, response: HttpServletResponse): Unit = welcome (request, response)
}

Then you'd mention it in web.xml as usual:

<servlet><servlet-name>MyServlet</servlet-name>
  <servlet-class>myservlet.Servlet</servlet-class></servlet>
<servlet-mapping><servlet-name>MyServlet</servlet-name>
  <url-pattern>/</url-pattern></servlet-mapping>

Refer to any Java Servlet tutorial for details.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
ArtemGr
  • 11,684
  • 3
  • 52
  • 85
2

You may not want to deal with Java EE and the whole servlet thing, especially if you don't come from the "Java world".

There are some really lightweight HTTP toolkits in Scala like Blueeyes or Play mini but my favorite is definitely Unfiltered

EDIT: A more complete answer on this thread Scala framework for a Rest API Server?

Community
  • 1
  • 1
blouerat
  • 692
  • 4
  • 12