Filters: In the Java Web, your incoming request,response filters out some of the information ahead of time, or sets some parameters ahead of time, and then passes in the servlet or struts action for business logic. such as filtering out illegal URLs (not login.do address requests, if the user does not log on to filter out), or in the servlet or struts before the action of the uniform set of character sets, or to remove some illegal characters
Interceptors: Is programming in the face of the plane is in your service or a method, before calling a method, or call a method after a method such as a dynamic proxy is a simple implementation of the Interceptor, before you call the method to print out the string (or other business logic operation), You can also print out a string after you invoke the method, even if you do business logic when you throw an exception.
The difference between interceptors and filters:
1. Interceptors are based on the reflection mechanism of Java, and filters are based on function callbacks.
2. Interceptors are not dependent on the servlet container, and the filter relies on the servlet container.
3. Interceptors only work on action requests, while filters can work on almost all requests.
4. The interceptor can access the action context, the object in the value stack, and the filter cannot be accessed.
5. The interceptor can be called multiple times during the life cycle of the action, and the filter can only be called once when the container is initialized
Execution order: Pre-filter-before-intercept-action handling-after interception-after filtering. Individuals think that filtering is a horizontal process, the first to filter the content submitted by the client (for example, the user does not have access to the internal page processing); After filtering, the interceptor will check the validation of user submitted data, do some pre-processing, and then send the processed data to the corresponding action After the action process is returned, the interceptor can also do other procedures (what is not expected to be done), and then return up to the filter for subsequent operations.
Aspect-oriented programming (AOP is the acronym for Aspect Oriented program), we know that object-oriented features are inheritance, polymorphism, and encapsulation. Encapsulation requires that functions be dispersed into different objects, which is often referred to as assignment of responsibilities in software design. In fact, let's say that different classes are designed to have different methods. So the code is scattered in a class. The benefit of this is to reduce the complexity of the code and make the class reusable.
However, it is also found that the code duplication is increased while the code is dispersed. What do you mean? For example, in two classes, we may need to log in each method. In terms of object-oriented design, we have to include the contents of the log in the methods of two classes. Perhaps they are exactly the same, but it is because object-oriented design makes it impossible for a class to be associated with a class, but not to unify the duplicated code.
Someone might say, well, we can write this code in a separate class-independent method, and then call it in these two classes. However, in this way, these two classes are coupled with the independent classes we mentioned above, and the changes will affect these two classes. So, is there any way that we can randomly add code when we need it? At run time, the programming idea of dynamically cutting code into the class, at the specified location, is the aspect-oriented programming.
In general, the code snippet in which we cut into the specified class is called a slice, and which class to cut into and which method is called the pointcut. With AOP, we can extract several classes of common code into a slice, and then cut into the object when needed, thus altering its original behavior.
In this way, AOP is really just a complement to OOP. OOP distinguishes a class from a landscape, while AOP adds a specific code to the object vertically. With the Aop,oop became three-dimensional. If you add a time dimension, AOP makes OOP from the original two dimensions into three-dimensional, from the plane to three-dimensional. Technically, AOP is basically implemented through proxy mechanisms.
AOP can be a milestone in programming history, and it is a useful complement to OOP programming.
The following example looks at the difference between filters and interceptors:
Using interceptors to filter JSP pages in the/admin directory
<package name= "Newsdemo" extends= "Struts-default"
Namespace= "/admin" >
<interceptors>
<interceptor name= "auth" class= "Com.test.news.util.AccessInterceptor"/>
<interceptor-stack name= "Authstack" >
<interceptor-ref name= "Auth"/>
</interceptor-stack>
</interceptors>
<!--action--
<action name= "newsadminview!*" class= "Newsaction"
Method= "{1}" >
<interceptor-ref name= "Defaultstack"/>
<interceptor-ref name= "Authstack"/>
Here is the Interceptor Class I implemented:
public class Accessinterceptor extends Abstractinterceptor
{
Private static final long serialversionuid = -4291195782860785705l;
Public String Intercept (Actioninvocation actioninvocation) throws Exception
{
Actioncontext Actioncontext = Actioninvocation.getinvocationcontext ();
Map session = Actioncontext.getsession ();
Object action = Actioninvocation.getaction ();
if (Action instanceof adminloginaction)
{
return Actioninvocation.invoke ();
}
if (Session.get ("user") = = null)
{
Return "Logout";
}
return Actioninvocation.invoke ();
}
}
Filter filters
Using filters to filter the JSP pages in the/admin directory, first filter configuration in Web. xml:
<filter>
<filter-name>access filter</filter-name>
<filter-class> Com.test.news.util.AccessFilter </filter-class>
</filter>
<filter-mapping>
<filter-name>access filter</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
The following is a filtered implementation class:
public class Accessfilter implements Filter
{
public void DoFilter (ServletRequest req, Servletresponse resp, Filterchain filterchain) throws IOException, Servletexception
{
HttpServletRequest request = (httpservletrequest) req;
HttpServletResponse response = (httpservletresponse) resp;
HttpSession session = Request.getsession ();
if (Session.getattribute ("user") = = null
&& Request.getrequesturi (). IndexOf ("login.jsp") = =-1)
{
Response.sendredirect ("login.jsp");
Return
}
Filterchain.dofilter (req, resp);
}
}
The difference between filters and interceptors