The Java filter is loaded when the service container is started, and its load order is consistent with the order of registration in Web. Xml.
SERVLET3.0 below, you need to register in Web. XML with the following definitions.
<fileter> <filter-name></filter-name> <filter-class></filter-class> <init-param> <param-name></param-name> <param-value></param-value> </init-param></filter><filter-mapping> <filter-name></filter-name> < Servlet-name></servlet-name> <url-pattern></url-pattern></filter-mapping>
More than Servlet 3.0, you can register the filter directly in the class file as a comment.
@WebFilter (filtername= "", urlpatterns={}, initparams={@WebInitParam (name= "", Value= "")})
When a user sends a request to the server, Tomcat first creates a request object of type HttpServletRequest and a response object of type HttpServletResponse. Then determine the URL address of the room request or whether the corresponding servlet has a filter defined, and if so, call its Dofilter method. In this method, there is a very important statement, China.dofilter (request, response), if you want to continue to execute the room-side request servlet or URL must have this line of code, otherwise, the filter is not considered to give release, the customer request will be filtered out.
The parameters specified when filtering is defined in Web. XML can be read by the Init () method in the Filter class.
public void init (filterconfig config) throw servletexception { String encoding = cofig.getinitparamter ("encoding"); ..}
Java Servlet Learning Notes (ii)