A nice example of how familiar Java programming "feels" in Scala are Servlets :
package com.muchsoft.scala.servletsThe looks around as Java, right? Not quite: When importing we can make Redefinitions that
import javax.servlet.http.HttpServlet import javax.servlet.http
. HttpServletRequest {=> Request, HttpServletResponse
=> Response}
MeinErstesScalaServlet class extends HttpServlet {override def
doGet (request: Request, response: response) {
val output =
\u0026lt;html>
\u0026lt; head> Scala \u0026lt;title> Hello \u0026lt;/ title> \u0026lt;/ head>
\u0026lt;body>
\u0026lt;h1> Scala Hello World \u0026lt;/ h1>
\u0026lt;/ body>
\u0026lt;/ html>
response.getWriter (). Print (output)
}}
override before the method definition is mandatory, also a key word and no annotation. The return type of method doGet ()
is not specified and thus corresponds to the Java
void. For Scala we had before the opening curly Clamp methods : Unit =
can write. doGet Within
()
we put the issue on XML literal
(as non-modifiable value
val; with var
scale variables, one tries to avoid in functional languages). This XML literal, we write as usual in the output stream of the Response object. The type of output
is scala.xml.Elem
. The structure of the web application corresponding to the JavaEE standard:
of the Scala compiler produces Java byte code ends up in
WEB-INF/classes
. The Scala standard library scala-library.jar
be delivered (from the Scala installation directory) must be in WEB-INF/lib
with. In WEB-INF/web.xml
we map the class
com.muchsoft.scala.servlets.MeinErstesScalaServlet to the URL "hallo.scala. After you deploy (deployment), for example in Tomcat or GlassFish the servlet in the Web browser will be invoked with the URL http://localhost:8080/halloscala/hallo.scala . To compile the servlet class must have the servlet API on the build path (class path) are located. This can be done servlet example from the Tomcat installation directory
-api.jar
or from the GlassFish directory javaee.jar
embed as an external library. Now we improve the first Scala Servlet and separate display and control each other a little better:
MeinZweitesScalaServlet class extends HttpServlet {
def output =
\u0026lt;html>
\u0026lt;head> \u0026lt;title> Scala Hello \u0026lt;/ title> !; \u0026lt;/ head>
\u0026lt;body>
\u0026lt;h1> Scala Hello World \u0026lt;/ h1>
\u0026lt;p> time on the server: {now} \u0026lt;/ p>
\u0026lt;/ body>
\u0026lt;/ html>
def now = new java.util.Date
override def doGet (request: Request, response: response) {.
response.getWriter () print (output)
}}
output
is now a function outside of doGet ()
, which relies on the function now
to output a current time stamp. Both functions are defined without parameters brackets and must be called without parentheses. If we had defined the parameters of functions with empty parentheses, we could have chosen it when you call us not if we write down the brackets or. It has been the convention developed to omit the brackets in the definition, if a function, no side effects (side effects) has. for a "full" web application is missing a form. Here we go:
MeinDrittesScalaServlet class extends HttpServlet {JSF, Wicket
def output (username: String) =
\u0026lt;html>
\u0026lt;head> \u0026lt;title> Scala Hello World \u0026lt;/ title> \u0026lt;/ head>
\u0026lt;body> ;
\u0026lt;h1> Hello {username} \u0026lt;/ h1>
\u0026lt;p> time on the Server: {now} \u0026lt;/ p>
\u0026lt;form method="get" action="hallo3.scala">
\u0026lt;p> What's your name?
\u0026lt;input name="benutzer"
/> \u0026lt;input type="submit" value="Submit"
/> \u0026lt;/ p>
\u0026lt;/ form>
\u0026lt;/ body>
\u0026lt; / html>
def now = new java.util.Date
override def doGet (request: Request, response: response) {val
param = getParameter request "user"
val = username
if ((param == null) is mapped in web.xml
to the URL "hallo3.scala" may be the call in the local application server with
http://localhost:8080/halloscala/hallo3.scala?benutzer=Thomas
.
In practice, you probably do not servlets programming more direct, but a framework like Struts
,
etc. . Insert We see here but well, how easy it into existing Java-Scala (web) applications can integrate. And if you want to develop web applications really functional, you can use the Scala Lift
framework.
0 comments:
Post a Comment