Overview | Package | Class | Deprecated | Index | Help
| Groovy Documentation
| FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## groovy.servlet
[Java] Class ServletBinding

    java.lang.Object
      groovy.lang.GroovyObjectSupport
          groovy.lang.Binding
              groovy.servlet.ServletBinding

    public class ServletBinding
    extends Binding

Servlet-specific binding extension to lazy load the writer or the output stream from the response.

### Eager variables

- `"request"` : the `HttpServletRequest` object
- `"response"` : the `HttpServletRequest` object
- `"context"` : the `ServletContext` object
- `"application"` : same as context
- `"session"` : shorthand for `request.getSession(`false`)` - can be null!
- `"params"` : map of all form parameters - can be empty
- `"headers"` : map of all `request` header fields

### Lazy variables

- `"out"` : `response.getWriter()`
- `"sout"` : `response.getOutputStream()`
- `"html"` : `new MarkupBuilder(response.getWriter())` - `expandEmptyElements` flag is set to true
- `"json"` : `new JsonBuilder()`

As per the Servlet specification, a call to `response.getWriter()` should not be done if a call to `response.getOutputStream()` has already occurred or the other way around. You may wonder then how the above lazy variables can possibly be provided - since setting them up would involve calling both of the above methods. The trick is catered for behind the scenes using lazy variables. Lazy bound variables can be requested without side effects; under the covers the writer and stream are wrapped. That means `response.getWriter()` is never directly called until some output is done using 'out' or 'html'. Once a write method call is done using either of these variable, then an attempt to write using 'sout' will cause an `IllegalStateException`. Similarly, if a write method call on 'sout' has been done already, then any further write method call on 'out' or 'html' will cause an `IllegalStateException`.

### Reserved internal variable names (see "Methods" below)

- `"forward"`
- `"include"`
- `"redirect"`

If `response.getWriter()` is called directly (without using out), then a write method call on 'sout' will not cause the `IllegalStateException`, but it will still be invalid. It is the responsibility of the user of this class, to not to mix these different usage styles. The same applies to calling `response.getOutputStream()` and using 'out' or 'html'.

### Methods

- `"forward(String path)"` : `request.getRequestDispatcher(path).forward(request, response)`
- `"include(String path)"` : `request.getRequestDispatcher(path).include(request, response)`
- `"redirect(String location)"` : `response.sendRedirect(location)`

Authors:
  Guillaume Laforge
  Christian Stein
  Jochen Theodorou

Method Summary
`java.lang.Object` | `ServletBinding(HttpServletRequest request, HttpServletResponse response, ServletContext context)` Initializes a servlet binding.
`void` | `forward(java.lang.String path)`
`java.lang.Object` | `getVariable(java.lang.String name)` @return a writer, an output stream, a markup builder or another requested object
`java.util.Map` | `getVariables()`
`void` | `include(java.lang.String path)`
`void` | `redirect(java.lang.String location)`
`void` | `setVariable(java.lang.String name, java.lang.Object value)`

Methods inherited from class Binding
getProperty, getVariable, getVariables, hasVariable, setProperty, setVariable

Methods inherited from class GroovyObjectSupport
getMetaClass, getProperty, invokeMethod, setMetaClass, setProperty

Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()

Method Detail

### ServletBinding

    public java.lang.Object ServletBinding(HttpServletRequest request, HttpServletResponse response, ServletContext context)

  Initializes a servlet binding.
  Parameters:
    `request` - the HttpServletRequest object
    `response` - the HttpServletRequest object
    `context` - the ServletContext object

### forward

    public void forward(java.lang.String path)

### getVariable

    @Override
    public java.lang.Object getVariable(java.lang.String name)

  Returns:
    a writer, an output stream, a markup builder or another requested object

### getVariables

    @Override
    public java.util.Map getVariables()

### include

    public void include(java.lang.String path)

### redirect

    public void redirect(java.lang.String location)

### setVariable

    @Override
    public void setVariable(java.lang.String name, java.lang.Object value)

Groovy Documentation