de.velopmind | The Det about Programming

February 16, 2010

How to setup NB for GAE with Scala – Part III

Filed under: GoogleAppEngine, Netbeans, Scala — de.velopmind @ 5:10 am

Additional Scala configuration for writing Servlets

To write Servlets in Scala, the Scala Project has to import at least the Geronimo Servlet Spec jar from the appengine directory. This one is sufficient to let your Scala class implement the Servlet interface.

Add Servlet-Spec Jar

Step 1: Select “Add Jar/Folder” in the context menu of the Libraries entry in your Scala project tree. Go to the lib/shared folder of your app-engine SDK installation.

Step 2: Select the geronimo-servlet-spec.jar

Write Test servlet in Scala

In the Scala project in our package testapp create a new Scala class named e.g. MyScalaServlet, and put in the following code example:

package testapp

import javax.servlet.http.{HttpServlet         => HServlet,
                           HttpServletRequest  => HSRequest,
                           HttpServletResponse => HSResponse}

import scala.xml.Elem

class MyScalaServlet extends HServlet {

  def processRequest(request:HSRequest, response:HSResponse) {

        val theTitle = "A first Scala Servlet"

        val content = (
                    is at: {request.getRequestURL()}

        writeContent(response, content)

  def writeContent(response:HSResponse, cont:Elem) {
        val out:PrintWriter = response.getWriter()
        try     { out.println(cont) }
        finally { out.close() }

  override def doGet(request:HSRequest, response:HSResponse) {
      processRequest(request, response);

I should mention that at the time of this writing there seems still to be a little bug in the Scala plugin which results in the first import displayed as erroneous. The message is: value servlet is not a member of package javax.

Truth is, you can simply ignore this message and build the project as usual.

Well, the source above is a little example of Scala’s features and how a servlet may look like in Scala. But this is only one way to create Scala servlets, and except for the embedded XML/HTML stuff it is very close to a Java solution. You may come up with better and more sophisticated solutions, or find some in the internet.

So far that’s fine. We now only have to configure the Servlet in the Web Application’s web.xml file

Select the web.xml file in the configuration files folder of the Web Application project and double click it to open.

Now you get a form based editor for the different parts in the configuration file. Select Servlets and click Add Servlet Element . You should see an input form, which you can fill like in the following example:

Please note that the Browse button won’t work in this scenario, as the source is not located in this same project. So the full qualified servlet class name has to be inserted manually.

After pressing OK and rebuilding/deploying the WebApp project, you can see the result by calling the following URL in your browser: http://locahost:8080/test/scalaservlet

The End

I hope you enjoyed this little tutorial about Scala in Netbeans for the Google App Engine.

If you want to go further into using the app engine services –for example the persistence–, you will need further libraries which are configured in the same way like described above for the geronimo jar.

For tutorials about using e.g. JDO or JPA with Scala look for other sources in the internet, as this exceeds the topic for this little howto.

The basics are laid out, now it’s your turn!


1 Comment »

  1. […] in the upcoming installment we will further setup our Scala project for Servlet development:  Follow me … Possibly related posts: (automatically generated)How to setup Netbeans for Google App Engine with […]

    Pingback by How to setup NB for GAE with Scala – Part II « The Det about Programming — February 17, 2010 @ 5:06 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

%d bloggers like this: