Scalatra-Scalate 6. Survival Info

Robert Crowther Apr 2022
Last Modified: Feb 2023

Scalatra‐Scalate 6. Survival Info

PrevNext

It’s an existential question—against the universe, there’s only so much one person can do. But I’ll try. I can give warnings. I got your CSS functioning.

Scattered answers about Scala

That Seq() thing looked rum

It gets magnificent. You could try comprehend the magnificence of Scala’s collection system. Or go code, and hope you can know/guess enough to do the job. Key points—the collection system is only types of arrays/lists and a dict/associative array/hash‐table construction Scala calls ‘Map’. And there is an important division between ‘mutable’ and ‘immutable’ container types.

How do I loop, set vars, and all that stuff?

Scala has a cute, elegant and weird syntax. It won’t be guessable. Sad to say you’ll need to go look it all up. The webposts are thin, but Scala documentation is quality.

How do I read files, connect a database?

The Scala coders have put a lot of effort into making Java code callable from Scala. This is not like other languages with their weird wraps and special libraries—most of the time you can import a Java library then call it. Do check that Scala has not got a groovy implementation because, if it has, you should use that.

Java has long deep history of database connection, so that’s good. Databases are called using the Java Database Connection System—JDBC. Which system uses URLs. Enough said.

I want only to store some configuration!

Oh, Java is mad for classes. Partly because they can be solid, comprehensive and encapsulated; partly because they are good for multithreading. Scala has a answer for you, the Object. You can do this,

object MyObjectNeedsNoInstanciation {
  val terriblyUseful = 3121944
  val deepOfIt = Map(
    "lords" -> "OfNight",
    "space" -> "IsDeep",
  )
}

After you import, that data will be there.

Scattered answers about SBT

I have none. SBT was constructed to answer the problems with Scala building. Java tools were not working. It works well for the community. It is solid, with nice commandline behaviour—up‐arrows work, it retains history on restart, that kind of behaviour. SBT is also genius. It has circular dependency checking, five dimentional parameters, comprehensive instructions, a teleporter, and is a nightmare for aliens.

I’ll give an example. You want to know how the Jetty server is configured. You can’t find any Jetty files. So you think, “I should be able to ask SBT”. You try ‘jetty help’ (some sort of command then help, yes?). No go. ‘help jetty’? No go. What is that colon in ‘jetty:start’? You look at documentation. You have no idea what this word Scalatra asked you to type in, “jetty” is. Is it a ‘scope’, a ‘key’, a ‘setting’, a ‘task’? Where is it configured? You begin to wonder what ‘jetty’ is. To SBT? Is it the ‘builtin server’, a Java ‘artefact’, is it enabled as a ‘script’, a ‘plugin’?

Know what? I’m going to tell you. ‘jetty’ is a ‘task expression’. A key that triggers an action. Try this,

inspect jetty:start

Yep, ‘inspect jetty’ will return failure. However, I tell you more, this will be disappointing,

jetty:configuration

Thanks. And this?

jetty:containerPort

I guessed. Oh, there’s more you could try. See, SBT is programmable. That build file will let you define keys. So, no doubt, you can program the build file to run a task to extract the configuration. But how much do you want to do? By the by, the source code for the Jetty plugin will not help either.

In summary: if you are a stranger to this land, and find you need to use SBT, that is a project fail. Get out. Go find yourself some PHP.

Scattered answers about Java Servlets

What’s this about filters?

Filters change the request or response. Java Documentation,

Applications of filters include authentication, logging, image conversion, data compression, encryption, tokenizing streams, XML transformations, and so on.

Some systems call this ‘middleware’. Code that tampers with requests and responses on the way in, or out, not in the main code handling.

How do I persist in the server?

Good question. See, when a request comes in, Java makes a new class of the Servlet, Then the server code stacks the class instances, jiggles them, tries to find a place and time to run them. Don’t want permanent stuff in the Servlet.

So your question is about lifetime. If you’re talking about data lasting for the lifetime of a user’s negotiation with the site, ‘sessions’, start on the Java documentation of Client state. If your aim is to keep some static data about, like useful tables, make an object and put the data in.

object UsefulTables {
  val titles = Map(
    "motorway" -> "City",
    "dust" -> "OfTime",
  )
}

Because this is Scala, not Java, this code can be placed anywhere. To start, above the Servlet code will be good, because it will be accessible without import,

get("/title/:id") {
  UsefulTables.titles(id)
}

Do note that anything you keep around may be accessed concurrently.

If you want to keep structured data so it can be negotiated and accessed, you need a database and connection.

Where to put surplus code in Servlet organisation

Or, “…but I can’t just pack these Servlet methods with more methods! I need big code!” Well, Java likes the idea it is big code.

Official Java.SBT response,

projectName/src/main/scala/com/myName/app/

Which the build has set up for us. That last part of the path is the reverse URL. This is also like the test app, at,

src/test/scala/com/myName/app/ScalatraServletTests.scala

That said, Scala allows you to package with a declaration (not this filename fuss). So you could dump code in

projectName/src/main/scala/

Then package with,

package com.myName.app
...

Then import into the area of the Servlet,

import com.myName.app.className._
...

Warning: Servlet code may be concurrent.

Where do static resources like CSS and JS go?

See /articles/scalatra-scalate-4-static-resources.

What’s WEB‐INF?

Nothing in WEB‐INF is served by a Servlet server. It’s a kind of protection.

What’s a WAR file?

A java convention for bundling a web application into a compressed file. It only contains the necessary parts of the project. some servers, e.g. Apache Tomcat, can use a WAR file directly—they can unzip automatically. Which is smart for deployment and, because only necessary parts are included, adds some security. Try the Scalatra documentation or the extensive Apache documentation on basic deployment of Jetty.

Scattered answers about spirituality and philosophy

I’m not sure about these tools

Scalate is a comprehensive and capable piece of code. I’d say it’s not the best templating setup I’ve used, but that’s because of Java—the folder organisation is a mess, the languages are gnomic (from respect to other Java culture), and I miss a few facilities. You’re unlikely to use these tools again, but once you have the clues you can get a job done.

As for Scalatra, maybe you would say the say thing. Except, maybe—this is an amazing Sinatra clone. The way Scala is used to build that API is remarkable. So this gear is worth seeing, in a wider sense. Here is computer code without tricks, with flexibility, yet natural to read and write.

And I’ve not said much about the server Jetty. Jetty is very small and limited in action. It’s a Servlet technology, as Scalata documentation calls it, “clunky and old”. It’s a dérailleur gear to the JVM. I don’t know if they do, but NASA would trust Jetty on Mars. If you can’t use Jetty, either you’ve misconceived what you are doing, or you have no need to read this page.

You’re wasting my time

Indisputably, Scalatra/Scalte is not mainstream code. So you are unlikely to need this much, or go there often. But knowing about what can be done can sometimes be useful. Does your web gear read and write so naturally? And do you know how potent Jetty/Scalatra is? If you use something you make with this, this will not be the fail point in your service.

End of Part Six

Urrgh, but for an outsider this is intimidating. Between patchy and lazy‐structured documentation, and API overload and release culture that reads like medievil history books. And the most valuable conclusion here is that if you need to use SBT beyond loading a few supplementary libraries, you should maybe rethink if these are the tools for your work. Maybe a Ruby environment would be more productive? Well, if you’ve got this far, then I do want to offer some encouragement. This guide has now reached deployment on a Jetty server, and deployment of Servelets on a Jetty server is a gem. Part Seven awaits.

Refs

Read about Servlets, Java documentation,

https://docs.oracle.com/javaee/5/tutorial/doc/bnafe.html