Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Here's a simple example to show you the kind of thing you can do from a Groovlet.
Notice the use of implicit variables to access the session, output & request. Also notice that this is more like a script as it doesn't have a class wrapper.

Code Block
java
java

if (!session) {
  session = request.getSession(true);
}

if (!session.counter) {
  session.counter = 1
}

println """
<html>
    <head>
        <title>Groovy Servlet</title>
    </head>
    <body>
Hello, ${request.remoteHost}: ${session.counter}! ${new Date()}
    </body>
</html>
"""
session.counter = session.counter + 1

...

variable name

bound to

note

request

ServletRequest

-

response

ServletResponse

-

context

ServletContext

unlike Struts

application

ServletContext

unlike Struts

session

getSession(false)

can be null! see (star) A

params

 

a Map object

headers

 

a Map object

out

response.getWriter()

see (star) B

sout

response.getOutputStream()

see (star) B

html

new MarkupBuilder(out)

see (star) B

jsonnew StreamingJsonBuilder(out)see (star) B

(star) A The session variable is only set, if there was already a session object. See the 'if (session == null)' checks in the examples above.

...