Differences between interceptor and filter in struts2 and their execution sequence

Source: Internet
Author: User
Tags i18n


When an httprequest is received,
A) when external httpservletrequest arrives
B) initially passed to the servlet container to a standard filter chain
C) filterdispatecher searches for the corresponding actionmapper. If the corresponding actionmapper is found, it will grant the control permission to actionproxy.
D) actionproxy searches for and configures struts. xml through configurationmanager.
I. The next step will be to implement the command mode through actioninvocation (including calling some interceptor frameworks before calling the action)
Ii. interceptor does some interception or initial work
E) Once the action is returned, the corresponding result will be searched.
F) The result type can be JSP or freemark.
G) these components are returned to the request URL together with actionmapper (note the execution sequence of the interceptor)
H) The response is returned through the filter we configured in Web. xml.
(I) If actioncontextcleanup is currently used, filterdispatecher will not clear sreadlocal actioncontext; If actioncontextcleanup is not used, sreadlocals will be cleared.

 

 

1. the interceptor is based on the Java reflection mechanism, and the filter is based on function callback.
2. The filter depends on the servlet container, and the interceptor does not rely on the servlet container.
3. the interceptor can only work on Action requests, while the filter can work on almost all requests.
4. the interceptor can access objects in the Action context and value stack, but the filter cannot.
5. In the lifecycle of an action, the interceptor can be called multiple times, and the filter can only be called once during container initialization.

 

 

Four functions of org. Apache. struts2.dispatcher. filterdispatcher

 

Org. Apache. struts2.dispatcher. filterdispatcher is the main filter of struts2 and is responsible for four functions:
(1) execute actions
(2) Clear actioncontext
(3) maintain static content
(4) Clear the interceptors of xwork within the request Lifecycle

Note: This filter should filter all request URLs. It is usually set to/*. (filter all requests !)
Details:
(1) execute actions
The filter uses the actionmapper object to determine whether it should be mapped to the action. If the Mapper object indicates that it should be mapped, the filter chain will be terminated and the action will be called. This is very important. If sitemesh filter is used at the same time, the sitemesh filter should be placed before the filter, otherwise the output of the action will not be decorated.
(2) Clear actioncontext
To ensure memory overflow, the filter automatically clears actioncontext. This may cause some problems, such as sitemesh, when integrating with other frameworks. Actioncontextcleanup provides some information about how to handle these problems.
(3) maintain static content
Filters also maintain some public static Content Used in struts2, such as JavaScript files and CSS files. Search for requests in the/struts/* range and map the values following/struts/to some public struts packages. You can also search for requests in your class path. By default, the following package is searched: org. Apache. struts2.static. template. In this way, you only need to request/struts/XHTML/styles.css. The default style sheet of the xhtml ui topic will be returned. Likewise, JavaScript files required by the Ajax UI component can be found in the org. Apache. struts2.static package. If you want to add other searched packages, set the filter value in Web. xml by a comma-separated package list value for the "actionpackages" initial parameter.
It exposes sensitive information such as the database connection information in the properites file.
Note: The filter supports the following initial parameters:
Config-List of XML files separated by commas.
Actionpackages-List of packages separated by commas scanned by actions.
Configproviders-separated by commas (,) Implement the configurationprovider interface (used during configuration construction.
*-Any struts constant.
You can use the createdispatcher () method to customize dispather.

Attribute list:
(1) actionmapper: provides an actionmapper instance through injection.
(2) DISPATCHER: exposes a dispatcher instance to the subclass.
(3) encoding: stores the settings of strutsconstants. struts_i18n_encoding.
(4) filterconfig: provides a filterconfig instance through initial parameters.
(5) lastmodifiedcal: When caching static content, A formatted date is provided to set the header information.
(6) log: provides a logging instance.
(7) patchprefixs: stores the path prefix information of static resources.
(8) servestatic: stores the settings of strutsconstants. struts_serve_static_content.
(9) servestaticbrowsercache: stores the settings of strutsconstants. struts_serve_static_browser_cache.
Method list:
(1) Copy (inputstream input, outputstream output): Copy data from input to output.
(2) createdispatcher (filterconfig): Creates a default dispatcher object. If necessary, subclass can reload this method to customize a dispatcher object.
(3) destory (): Call dispatcher. Cleanup (), release the local thread in sequence, and destroy the dispatcher object.
(4) dofilter (servletrequest request, serveltresponse response, filterchain chain): process an action or the static content of a request.
(5) findinputstream (string name, string packageprefix): searches for static resources in a class path.
(6) findstaticresoruce (string name, httpservletrequest request, httpservletresponse response): searches for static resources and directly copies them to the response header.
(7) getcontenttype (string name): obtains the contenttype of the specified resource.
(8) getfilterconfig (): obtains the filterconfig instance.
(9) getservletcontext (): provides a workspace for some WebLogic versions.

(9) Init (filterconfig filtercongfig): Creates a default dispatcher object and sets the default package information of static resources to initialize the filter.
(10) Parse (string packages): returns an array of packages separated by commas.
(11) preparedispatcherandwraprequest (httpservletrequest request, httpservletresponse response): encapsulate the given request object and return an encapsulated httpservletrequest object. For example, the displayed processing multipart data.
(12) setmapper (actionmapper)
(13) setencoding (string Val)
(14) setservestaticcontent (string Val)
(15) setservestaticbrowsercache (string Val)

 

 

Appendix:

Description of the interceptor function provided by struts2 (xwork:

Interceptor

Name

Description

Alias interceptor

Alias

The request parameters are converted in different name parts between different requests, and the request content remains unchanged.

Chaining interceptor

Chain

This allows the attribute of the previous action to be accessed by the next action. It is now used in combination with the result (<result type = "chain">) of the chain type.

Checkbox interceptor

Checkbox

When the checkbox automatic processing code is added, the content of the unselected checkbox is set to false. By default, HTML does not submit the unselected checkbox.

Cookies interceptor

Cookies

The configured name and value are used to indicate cookies.

Conversion error interceptor

Conversionerror

Add the error from actioncontext to the action attribute field.

Create session interceptor

Createsession

Automatically create an httpsession to serve the interceptor that needs to use httpsession.

Debugging interceptor

Debugging

Different debugging pages are provided to display internal data conditions.

Execute and wait interceptor

Execandwait

Execute the action in the background and take the user to a waiting page in the middle.

Exception interceptor

Exception

Locate the exception to a screen

File Upload interceptor

Fileupload

Provides the file upload function.

I18n interceptor

I18n

Record the selected locale

Logger interceptor

Logger

Name of the output action

Message store interceptor

Store

Stores or accesses messages, errors, and field errors in the action class that implements the validationaware interface.

Model Driven interceptor

Model-driven

If a class implements modeldriven, place the result of GetModel in the value stack.

Scoped Model Driven

Scoped-model-driven

If an action implements scopedmodeldriven, the interceptor extracts the model from the corresponding scope and calls the setmodel method of action to put it into the action.

Parameters interceptor

Params

Set the parameters in the request to action.

Prepare interceptor

Prepare

If Acton implements preparable, the interceptor calls the prepare method of the action class.

Scope interceptor

Scope

A simple method to save the action status to the session and application.

Servlet config interceptor

Servletconfig

Provides methods to access httpservletrequest and httpservletresponse, and uses map.

Static Parameters interceptor

Staticparams

From the Struts. xml file, set the content in <action> <param> to the corresponding action.

Roles interceptor

Address: http://blog.csdn.net/d19891006/article/details/6900602


When an httprequest is received,
A) when external httpservletrequest arrives
B) initially passed to the servlet container to a standard filter chain
C) filterdispatecher searches for the corresponding actionmapper. If the corresponding actionmapper is found, it will grant the control permission to actionproxy.
D) actionproxy searches for and configures struts. xml through configurationmanager.
I. The next step will be to implement the command mode through actioninvocation (including calling some interceptor frameworks before calling the action)
Ii. interceptor does some interception or initial work
E) Once the action is returned, the corresponding result will be searched.
F) The result type can be JSP or freemark.
G) these components are returned to the request URL together with actionmapper (note the execution sequence of the interceptor)
H) The response is returned through the filter we configured in Web. xml.
(I) If actioncontextcleanup is currently used, filterdispatecher will not clear sreadlocal actioncontext; If actioncontextcleanup is not used, sreadlocals will be cleared.

 

 

1. the interceptor is based on the Java reflection mechanism, and the filter is based on function callback.
2. The filter depends on the servlet container, and the interceptor does not rely on the servlet container.
3. the interceptor can only work on Action requests, while the filter can work on almost all requests.
4. the interceptor can access objects in the Action context and value stack, but the filter cannot.
5. In the lifecycle of an action, the interceptor can be called multiple times, and the filter can only be called once during container initialization.

 

 

Four functions of org. Apache. struts2.dispatcher. filterdispatcher

 

Org. Apache. struts2.dispatcher. filterdispatcher is the main filter of struts2 and is responsible for four functions:
(1) execute actions
(2) Clear actioncontext
(3) maintain static content
(4) Clear the interceptors of xwork within the request Lifecycle

Note: This filter should filter all request URLs. It is usually set to/*. (filter all requests !)
Details:
(1) execute actions
The filter uses the actionmapper object to determine whether it should be mapped to the action. If the Mapper object indicates that it should be mapped, the filter chain will be terminated and the action will be called. This is very important. If sitemesh filter is used at the same time, the sitemesh filter should be placed before the filter, otherwise the output of the action will not be decorated.
(2) Clear actioncontext
To ensure memory overflow, the filter automatically clears actioncontext. This may cause some problems, such as sitemesh, when integrating with other frameworks. Actioncontextcleanup provides some information about how to handle these problems.
(3) maintain static content
Filters also maintain some public static Content Used in struts2, such as JavaScript files and CSS files. Search for requests in the/struts/* range and map the values following/struts/to some public struts packages. You can also search for requests in your class path. By default, the following package is searched: org. Apache. struts2.static. template. In this way, you only need to request/struts/XHTML/styles.css. The default style sheet of the xhtml ui topic will be returned. Likewise, JavaScript files required by the Ajax UI component can be found in the org. Apache. struts2.static package. If you want to add other searched packages, set the filter value in Web. xml by a comma-separated package list value for the "actionpackages" initial parameter.
It exposes sensitive information such as the database connection information in the properites file.
Note: The filter supports the following initial parameters:
Config-List of XML files separated by commas.
Actionpackages-List of packages separated by commas scanned by actions.
Configproviders-separated by commas (,) Implement the configurationprovider interface (used during configuration construction.
*-Any struts constant.
You can use the createdispatcher () method to customize dispather.

Attribute list:
(1) actionmapper: provides an actionmapper instance through injection.
(2) DISPATCHER: exposes a dispatcher instance to the subclass.
(3) encoding: stores the settings of strutsconstants. struts_i18n_encoding.
(4) filterconfig: provides a filterconfig instance through initial parameters.
(5) lastmodifiedcal: When caching static content, A formatted date is provided to set the header information.
(6) log: provides a logging instance.
(7) patchprefixs: stores the path prefix information of static resources.
(8) servestatic: stores the settings of strutsconstants. struts_serve_static_content.
(9) servestaticbrowsercache: stores the settings of strutsconstants. struts_serve_static_browser_cache.
Method list:
(1) Copy (inputstream input, outputstream output): Copy data from input to output.
(2) createdispatcher (filterconfig): Creates a default dispatcher object. If necessary, subclass can reload this method to customize a dispatcher object.
(3) destory (): Call dispatcher. Cleanup (), release the local thread in sequence, and destroy the dispatcher object.
(4) dofilter (servletrequest request, serveltresponse response, filterchain chain): process an action or the static content of a request.
(5) findinputstream (string name, string packageprefix): searches for static resources in a class path.
(6) findstaticresoruce (string name, httpservletrequest request, httpservletresponse response): searches for static resources and directly copies them to the response header.
(7) getcontenttype (string name): obtains the contenttype of the specified resource.
(8) getfilterconfig (): obtains the filterconfig instance.
(9) getservletcontext (): provides a workspace for some WebLogic versions.

(9) Init (filterconfig filtercongfig): Creates a default dispatcher object and sets the default package information of static resources to initialize the filter.
(10) Parse (string packages): returns an array of packages separated by commas.
(11) preparedispatcherandwraprequest (httpservletrequest request, httpservletresponse response): encapsulate the given request object and return an encapsulated httpservletrequest object. For example, the displayed processing multipart data.
(12) setmapper (actionmapper)
(13) setencoding (string Val)
(14) setservestaticcontent (string Val)
(15) setservestaticbrowsercache (string Val)

 

 

Appendix:

Description of the interceptor function provided by struts2 (xwork:

Interceptor

Name

Description

Alias interceptor

Alias

The request parameters are converted in different name parts between different requests, and the request content remains unchanged.

Chaining interceptor

Chain

This allows the attribute of the previous action to be accessed by the next action. It is now used in combination with the result (<result type = "chain">) of the chain type.

Checkbox interceptor

Checkbox

When the checkbox automatic processing code is added, the content of the unselected checkbox is set to false. By default, HTML does not submit the unselected checkbox.

Cookies interceptor

Cookies

The configured name and value are used to indicate cookies.

Conversion error interceptor

Conversionerror

Add the error from actioncontext to the action attribute field.

Create session interceptor

Createsession

Automatically create an httpsession to serve the interceptor that needs to use httpsession.

Debugging interceptor

Debugging

Different debugging pages are provided to display internal data conditions.

Execute and wait interceptor

Execandwait

Execute the action in the background and take the user to a waiting page in the middle.

Exception interceptor

Exception

Locate the exception to a screen

File Upload interceptor

Fileupload

Provides the file upload function.

I18n interceptor

I18n

Record the selected locale

Logger interceptor

Logger

Name of the output action

Message store interceptor

Store

Stores or accesses messages, errors, and field errors in the action class that implements the validationaware interface.

Model Driven interceptor

Model-driven

If a class implements modeldriven, place the result of GetModel in the value stack.

Scoped Model Driven

Scoped-model-driven

If an action implements scopedmodeldriven, the interceptor extracts the model from the corresponding scope and calls the setmodel method of action to put it into the action.

Parameters interceptor

Params

Set the parameters in the request to action.

Prepare interceptor

Prepare

If Acton implements preparable, the interceptor calls the prepare method of the action class.

Scope interceptor

Scope

A simple method to save the action status to the session and application.

Servlet config interceptor

Servletconfig

Provides methods to access httpservletrequest and httpservletresponse, and uses map.

Static Parameters interceptor

Staticparams

From the Struts. xml file, set the content in <action> <param> to the corresponding action.

Roles interceptor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.