public interface FilterAction
FilterAction
s are components of a frequently used type of Filter
s. Such filters do
a piece of work before or after calling the FilterChain
, but never wrap the request or
response. Examples are filters that set request attributes or cookies. This piece of works can be encapsulated by a
FilterAction
.
execute(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
is an action's
method that executes the desired piece of work. The action can be configured by
init(javax.servlet.FilterConfig)
which is the equivalent to
Filter.init(javax.servlet.FilterConfig)
. The destroy()
can be used to clean up
resources. A simple filter action may look like this.
public class YourAction implements FilterAction { private YourService service; public void init(FilterConfig config) throws ServletException { String param = config.getInitParameter("your parameter"); service = new YourService(param); } public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { request.setAttribute("your attribute", service.getNextValue()); } public void destroy() { service.shutdown(); } }
Filter tools provides tow filter that are executing actions. The
PreFilterChainActionsFilter
executes actions
before it invokes the filter chain and
the PostFilterChainActionsFilter
executes it
afterwards. You use filter actions by creating your own filter that inherits one of the two filters. Look at this
example:
public class YourFilter implements PreFilterChainActionsFilter { public YourFilter() { super(new FirstAction(), new SecondAction()); } }
Filter tools provides a FilterActionSwitch
that selects one of two actions based on a Predicate
FilterAction actionSwitch = FilterActionSwitch.execute(new DoSomething()) .when(new APredicate()) .otherwiseExecute(new DoSomethingElse());
Modifier and Type | Method and Description |
---|---|
void |
destroy()
Called to indicate to an action that it is being taken out of service.
|
void |
execute(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
The
execute method of the action is called by a filter before or after the
FilterChain is invoked depending on the filter itself. |
void |
init(javax.servlet.FilterConfig filterConfig)
Called to indicate to an action that it is being placed into service.
|
void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException
filterConfig
- A filter configuration object used by a servlet container
to pass information to an action during initialization.javax.servlet.ServletException
- if an exception has occurred that interferes with the action's normal operationvoid execute(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException
execute
method of the action is called by a filter before or after the
FilterChain
is invoked depending on the filter itself.request
- an HttpServletRequest
response
- an HttpServletResponse
IOException
- if an input or output exception occursjavax.servlet.ServletException
- if an exception has occurred that interferes with the action's normal operationvoid destroy()
This method gives the action an opportunity to clean up any resources that are being held (for example, memory, file handles, threads).
Copyright © 2014–2015. All rights reserved.