progs/Application1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 03 Oct 2014 13:14:34 +0100
changeset 192 2cb42412f3fd
parent 103 bd6e45c7aa8d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
103
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
package controllers
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import play.api._
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import play.api.mvc._
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
import play.api.data._
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
import play.api.data.Forms._
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
/*
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
 * Answers a GET-request by sending a simple login form.
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
 *
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
 * Processes the POST-data by just printing the results.
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
 *
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
 */
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
object Application extends Controller {
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  // GET request -> login form
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  val index = Action { request =>
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    val form = """
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
               <form method="post"> 
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
               Login: <input type="text" name="login"><br>
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
               Password: <input type="password" name="password"><br>
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
               <input type="submit"></form>"""
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
    Ok(form).as(HTML)
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  }  
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  // POST data: processing the login data
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  val receive = Action { request =>
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    val form_data = Form(tuple ("login" -> text, "password" -> text))
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
    val (login, password) = form_data.bindFromRequest()(request).get 
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
    
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
    Ok(s"Received login: $login and password: $password")
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  }
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  
bd6e45c7aa8d added applications
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
}