Servlets 如何在Scalatra Servlet中获取请求参数?
我目前有一个工作的ScalactraservletServlets 如何在Scalatra Servlet中获取请求参数?,servlets,scalatra,Servlets,Scalatra,我目前有一个工作的Scalactraservlet class MyScalatraServlet extends MyScalatraWebAppStack { get("/") { response.getWriter().println("Test") val scDate = com.example.app.ScalaDate.printDate() response.getWriter().println(scDate) val c
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 the output
response.getWriter().println(output)
}
}
文件路径当前是硬编码的。我可以做些什么来允许用户输入filepath参数吗?非常感谢您的帮助 使用GET或POST,您可以输入参数,如下所示:
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 with 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])
// for 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 to render a ScalateTemplate if no route matched
findTemplate(requestPath) map { path =>
contentType = "text/html"
layoutTemplate(path)
} orElse serveStaticResource() getOrElse resourceNotFound()
}
}
Exec curl如下所示:
例如,您可以替换filepath的JSON键和值。使用GET或POST,您可以输入参数,如下所示:
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 with 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])
// for 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 to render a ScalateTemplate if no route matched
findTemplate(requestPath) map { path =>
contentType = "text/html"
layoutTemplate(path)
} orElse serveStaticResource() getOrElse resourceNotFound()
}
}
Exec curl如下所示:
例如,您可以替换filepath的JSON键和值。使用GET或POST,您可以输入参数,如下所示:
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 with 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])
// for 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 to render a ScalateTemplate if no route matched
findTemplate(requestPath) map { path =>
contentType = "text/html"
layoutTemplate(path)
} orElse serveStaticResource() getOrElse resourceNotFound()
}
}
Exec curl如下所示:
例如,您可以替换filepath的JSON键和值。使用GET或POST,您可以输入参数,如下所示:
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 with 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])
// for 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 to render a ScalateTemplate if no route matched
findTemplate(requestPath) map { path =>
contentType = "text/html"
layoutTemplate(path)
} orElse serveStaticResource() getOrElse resourceNotFound()
}
}
Exec curl如下所示:
例如,您可以替换filepath的JSON键和值