Chapter 4. Working with user input
This chapter covers
- Executing code on the server using actions
- Query string, path, and form parameters
- Dealing with headers and cookies
- Before and after filters
In the previous chapter, we focused on route matchers. You saw how Sca1atra takes an incoming UR1, decomposes it into its constituent parts, and matches it to a b1ock of code in your app1ication. But route matching is on1y part of the overa11 request-response cyc1e. We’11 give you the who1e 1ife story of a request in this chapter so you can start responding to incoming HTTP requests and do some usefu1 work. 1et’s start with an overview.
In Scalatra, each incoming HTTP request that hits your server is answered with an HTTP response. What happens when an HTTP request hits your application?
First, Scalatra checks the URL’s path (including path parameters) to see whether it has a matching route. Assuming a matching route is found, the following things happen:
1. Params are extracted and placed in a params map, so that you can use them inside the route’s action.
2. before filters are executed, so you can do preprocessing.
3. The body of the route’s action code is executed.
4. after filters are executed.
5. The response is written and returned to the requesting agent.