How to get request parameters in a Scalatra Servlet? -
i have working scalactra servlet.
class myscalatraservlet extends myscalatrawebappstack { get("/") { response.getwriter().println("test") val scdate = com.example.app.scaladate.printdate() response.getwriter().println(scdate) val cmd = "spark-submit --jars /home/cloudera/desktop/sparkdisc/lib/dme.jar/..../discovery_2.10-1.0.jar hdfs:/user/cloudera/sample0.txt"//hard coded file path val output = cmd.!! // captures output response.getwriter().println(output) } }
the file path hard coded. there can allow user enter in filepath parameters? appreicited!
use get or post, can input parameter, this:
import org.scalatra._ import scalate.scalatesupport import com.google.gson.gson import java.util.concurrent.timeunit import java.util.date class queue(var id: string, var act: string, var waitsec: int) { override def tostring = id + ", " + act + ", " + waitsec.tostring } class myscalatraservlet extends scalatraservlet scalatesupport { get("/hello/:query") { <html> <body> <pre> {params("query")} </pre> </body> </html> } post("/hello") { println("a request comes: " + "%tf %<tt" format new date) val jsonstring = request.body try { val gson = new gson val queue = gson.fromjson(jsonstring, classof[queue]) // debugging println("your name: " + queue) if (queue.act.equals("stop")) { println("wait for: " + queue.waitsec.tostring) timeunit.seconds.sleep(queue.waitsec) } response.addheader("ack", "got it") } catch { case e: exception => e.printstacktrace response.addheader("ack", "boom!") } } notfound { // try render scalatetemplate if no route matched findtemplate(requestpath) map { path => contenttype = "text/html" layouttemplate(path) } orelse servestaticresource() getorelse resourcenotfound() } }
- get
you should refer scalatra 2.4 guide ... routes can see inputted query accessing http://localhost:8080/hello/query
but, think inputting filepath query not manner.
- post
above example, can make stop processing servlet. following json useful handle code.
{"id":"001","act":"stop","waitsec":100}
exec curl this:
$ curl -i -x post -d "{\"id\":\"001\",\"act\":\"stop\",\"waitsec\":100}" http://localhost:8080/hello -h "content-type: application/json"
for example, can replace json key&value filepath.
Comments
Post a Comment