Package groovy.servlet
Class ServletBinding
- java.lang.Object
- 
- groovy.lang.GroovyObjectSupport
- 
- groovy.lang.Binding
- 
- groovy.servlet.ServletBinding
 
 
 
- 
- All Implemented Interfaces:
- GroovyObject
 
 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 HttpServletRequestobject
- "response" : the HttpServletRequestobject
- "context" : the ServletContextobject
- "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())-expandEmptyElementsflag is set to true
- "json" : new JsonBuilder()
 response.getWriter()should not be done if a call toresponse.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 meansresponse.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 anIllegalStateException. Similarly, if a write method call on 'sout' has been done already, then any further write method call on 'out' or 'html' will cause anIllegalStateException.Reserved internal variable names (see "Methods" below)- "forward"
- "include"
- "redirect"
 response.getWriter()is called directly (without using out), then a write method call on 'sout' will not cause theIllegalStateException, 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 callingresponse.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)
 
- 
- 
Constructor SummaryConstructors Constructor Description ServletBinding(HttpServletRequest request, HttpServletResponse response, ServletContext context)Initializes a servlet binding.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidforward(java.lang.String path)java.lang.ObjectgetVariable(java.lang.String name)java.util.MapgetVariables()voidinclude(java.lang.String path)voidredirect(java.lang.String location)voidsetVariable(java.lang.String name, java.lang.Object value)Sets the value of the given variable- 
Methods inherited from class groovy.lang.BindinggetProperty, hasVariable, setProperty
 - 
Methods inherited from class groovy.lang.GroovyObjectSupportgetMetaClass, invokeMethod, setMetaClass
 
- 
 
- 
- 
- 
Constructor Detail- 
ServletBindingpublic ServletBinding(HttpServletRequest request, HttpServletResponse response, ServletContext context) Initializes a servlet binding.- Parameters:
- request- the HttpServletRequest object
- response- the HttpServletRequest object
- context- the ServletContext object
 
 
- 
 - 
Method Detail- 
setVariablepublic void setVariable(java.lang.String name, java.lang.Object value)Description copied from class:BindingSets the value of the given variable- Overrides:
- setVariablein class- Binding
- Parameters:
- name- the name of the variable to set
- value- the new value for the given variable
 
 - 
getVariablespublic java.util.Map getVariables() - Overrides:
- getVariablesin class- Binding
 
 - 
getVariablepublic java.lang.Object getVariable(java.lang.String name) - Overrides:
- getVariablein class- Binding
- Parameters:
- name- the name of the variable to lookup
- Returns:
- a writer, an output stream, a markup builder or another requested object
 
 - 
forwardpublic void forward(java.lang.String path) throws ServletException, java.io.IOException- Throws:
- ServletException
- java.io.IOException
 
 - 
includepublic void include(java.lang.String path) throws ServletException, java.io.IOException- Throws:
- ServletException
- java.io.IOException
 
 - 
redirectpublic void redirect(java.lang.String location) throws java.io.IOException- Throws:
- java.io.IOException
 
 
- 
 
-