Filter as the name implies, struts in Javaweb leverages this mechanism to achieve view and business separation.
Here to configure your own filter to understand the underlying struts.
Add the following in Web. xml:
<filter> <filter-name>myfilter</filter-name> <filter-class> sun.fifter.myfifter</filter-class> </filter> <filter-mapping> < filter-name>myfilter</filter-name> <url-pattern>*</url-pattern> </ Filter-mapping>
This means that as long as the type of the URL is *, the myfilter will be adjusted,
Then define the Myfilter class:
Package Sun.fifter;import Java.io.ioexception;import javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;import Javax.tools.JavaCompiler; Public classMyfifter implements javax.servlet.filter{ PublicMyfifter () {//TODO auto-generated Constructor stub} @Override Public voiddestroy () {//TODO auto-generated Method StubSystem. out. println ("Destroy"); } @Override Public voidDoFilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2) throws IOException, Servletexceptio n {//TODO auto-generated Method StubSystem. out. println ("DoFilter"); } @Override Public voidInit (Filterconfig arg0) throws Servletexception {//TODO auto-generated Method StubSystem. out. println ("Init"); }}
So at the time of the call: http://localhost:8080/WebProject1/index.html
The console will be output;
Java Web Custom Filter