Skip to content

Instantly share code, notes, and snippets.

@pedrofurla
Created September 9, 2013 15:51
Show Gist options
  • Select an option

  • Save pedrofurla/6497566 to your computer and use it in GitHub Desktop.

Select an option

Save pedrofurla/6497566 to your computer and use it in GitHub Desktop.
Showing the show off to S11001001
// Show the problem
override def doGet(req: HttpServletRequest, resp: HttpServletResponse) {
resp.setContentType("text/html")
resp.setCharacterEncoding("UTF-8")
// TROUBLE // TROUBLE // TROUBLE
var a:Int = req.getParameter("a").toInt
var b:Int = req.getParameter("b").toInt
var c:Int = req.getParameter("c").toInt
val responseBody: NodeSeq =
<html>
<body>
<h1>Hello, world!</h1>
A some de {a} + {b} + {c} = {a+b+c}
</body>
</html>
resp.getWriter.write(responseBody.toString)
}
// Show the java style solution (a quick one)
override def doGet(req: HttpServletRequest, resp: HttpServletResponse) {
resp.setContentType("text/html")
resp.setCharacterEncoding("UTF-8")
def getParam(p:String):Integer=Integer.parseInt(req.getParameter(p))
// LESS TROUBLE // LESS TROUBLE // LESS TROUBLE
// but very awkward! Gambis de primeira rolando solta!
var a:Integer = null
var b:Integer = null
var c:Integer = null
try {
a = getParam("a")
} catch {
case ex : NumberFormatException =>
}
try {
b = getParam("b")
} catch {
case ex: NumberFormatException =>
}
try {
c = getParam("c")
} // LOOK NO CATCH!
val responseBody: NodeSeq =
<html>
<body>
<h1>Hello, world!</h1>
{
if (a != null && b != null & c != null) s"A some de {a} + {b} + {c} = {a+b+c}"
else s"Algum dos números não é valido: a: `{a}` ou b: `{b}` ou c: `{c}` "
}
</body>
</html>
resp.getWriter.write(responseBody.toString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment