There are only three major components in a Web project:
Filter filter
Listener.
Servlet
In the Web, a filter is a class javax.servlet.Filter.
A filter is a response (response) that is used by an overly-worried user (request) at execution time, or by worrying about the return from the requested resource.
Implement a most basic filter
1: The filter only filters the URL (path).
2: Filter developers to achieve.
3: Filter because it is the core group of the Web, the filter's implementation must also be configured in the XML.
4: Three life cycle method: Init,destory,dofilter (Perform a worry task). Each request of the user executes the Dofilter method, and
Init,destory will only be executed once.
Init method Execution Time: The object of the filter is initialized directly at the start of the project, so the Init method is executed at Tomcat startup.
First step: Write a class to implement the filter interface
Package Cn.hongxin.filter;import Java.io.ioexception;import javax.servlet.filter;import Javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.ServletResponse; Public classOnefilter implements Filter {//This method only executes once when Tomcat is started Public voidInit (Filterconfig filterconfig) throws Servletexception {System.err.println ("the filter is initialized:"+ This); } //This method performs a worry task Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain) throws IOException, Servlet Exception {System.err.println ("1: Performing the Over-filtration:"+ This); //executes the next filter and executes the target servlet if there is no next filterChain.dofilter (request, response);//2:System.err.println ("3: Target component, execution completed ...");} Public voiddestroy () {System.err.println ("filter dead."+ This); }}
Step Two: Configure this filter in Web. xml
<filter> <filter-name>one</filter-name> <filter-class> cn.itcast.filter.onefilter</filter-class> </filter> <!--configure which URL to filter on - <filter-mapping> <filter-name>one</filter-name> <url-pattern>/ One </url-pattern> </filter-mapping>
Step Three: Test execution
Settings for Url-pattern in Ilter filter-mapping
Settings for Url-pattern:
/* = Request this servlet for all URLs | are being considered by this filter.
/= can only be given to Servlets. -All other URLs that are not processed by the servlet are handled by the servlet specified by this URL. The default servlet.
*.jspx = all ends with jspx request to this servlet or be intercepted by a filter. such as Http://local:8080/proj/abc.jspx
/jsps/* = All paths beginning with/jsps/are requested to this servlet or intercepted by a filter.
/jsps/*.jspx wrong: Sun rules * Both sides can not appear at the same time characters.
<filter-mapping> <filter-name>one</filter-name> <!--blocking over all URLs--- < url-pattern>/index.jsp</url-pattern> <url-pattern>/jsps/*</url-pattern> <!--intercept all URL settings on this servlet- <servlet-name>OneSerlvet</servlet-name> </ Filter-mapping>
Other configurations in the filter
If you have jsps/abc.jsp this page, there are two ways to display this page:
1: In the Address bar request: http://local:8080/project/jsps/abc.jsp-can be intercepted because: path conforms to/jsp/abc.jsp
2:req.getrequestdispathcer ("/jsps/abc.jsp"). Forward (REQ,RESP); -
Dipatcher Properties:
request– the default value to intercept only the user's new request.
forward– to intercept the forwarding.
include– to include interception.
Error-intercepts the errors.
<!--set to forward intercept--
<DISPATCHER>REQUEST[W1] </dispatcher>
<dispatcher>FORWARD</dispatcher>
7. Configure the initialization parameters of the filter
There is one method in the filter class: init (filterconfig config); The filtetrconfig contains the read initialization parameter value from Web. Xml.
<filter>
<filter-name>one</filter-name>
<filter-class>cn.itcast.filter.OneFilter</filter-class>
<init-param>
<param-name>name</param-name>
<param-value> Zhang San jack</param-value>
</init-param>
<init-param>
<param-name>age</param-name>
<param-value>88</param-value>
</init-param>
</filter>
Get the value in the filter class:
Private Filterconfig conf;
This method only executes once when Tomcat is started
Public void init (filterconfig conf) throws servletexception {
this. conf=conf;
System. err. println ("filter Initialized:" + this);
Get the value of name:
String name = Conf.getinitparameter ("name");
System. err. println ("name is:" +name);
System. err. println ("----------------------------");
Enumeration<string> en= conf.getinitparameternames ();
while (En.hasmoreelements ()) {
String paramname = En.nextelement ();
String val = conf.getinitparameter (paramname);
System. err. println (paramname+ "=" +val);
}
}
Filter Basics (47)