Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between interceptor and filter in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Java interceptor and filter what is the difference between the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will gain after reading this Java interceptor and filter article, let's take a look.

1. Filter (filter)

The filter is between the client and the Web resources (Servlet, JSP, HTML). Requests and responses between the client and the Web resources are filtered through the filter. For example, if the 192.10.10.1 address is forbidden in the filter, then when the client makes a request to access 192.10.10.1, the response the client gets after the filter is a prompt that the IP forbids access. In java web, the request,response you pass in filters out some information in advance, or sets some parameters in advance, and then inputs the action of servlet or struts for business logic, such as filtering out illegal url (address requests that are not login.do, if users do not log in), or uniformly setting the character set before passing in the action of servlet or struts, or removing some illegal characters

2. Interceptor (interceptor)

Interceptor is a kind of aspect-oriented programming (AOP Aspect-Oriented Programming), while aspect-oriented is to separate the common services of multiple modules, such as rights management and logging services, which are used in multiple modules, so they can be packaged into a reusable module. The specific implementation of these general services is completed through the interceptor. For example, when the user client accesses some secret modules, the interceptor of permission review should be used to review the permissions. Determine whether the user has the authority to do this operation before it can be executed downwards. In aspect-oriented programming, you can call a method before your service or a method, or call a method after a method. For example, a dynamic proxy is a simple implementation of the interceptor, print a string before you call the method (or do other business logic operations), print a string after you call the method, or even do business logic operations when you throw an exception.

III. The difference between interceptor and filter

1. The interceptor is based on java's reflection mechanism, while the filter is based on function callback (chain of responsibilities).

2. The filter depends on the servlet container, while the interceptor does not depend on the servlet container

3. Interceptors can only work on action requests, while filters can work on almost all requests

4. Interceptors can access objects in the action context and value stack, but filters cannot

5. In the life cycle of action, interceptors can be called many times, while filters can only be called once when the container is initialized

Order of execution: before filtering-before intercepting-Action processing-after intercepting-after filtering. Personally, I think that filtering is a horizontal process, first filtering the content submitted by the client (for example, the processing that unlogged-in users cannot access the internal page); after filtering, the interceptor will check the verification of the data submitted by the user, do some preliminary data processing, and then send the processed data to the corresponding Action After the Action processing returns, the interceptor can do other processes (I haven't thought of what to do yet) and then return up to the subsequent actions of the filter.

_.

IV. Detailed explanation

Interceptor: aspect-oriented programming is to call a method before your service or a method, or call a method after a method such as a dynamic proxy is a simple implementation of the interceptor, print a string before you call the method (or do other business logic operations), print a string after you call the method, or even do business logic operations when you throw an exception.

Let's look at the difference between a filter and an interceptor through an example:

Use interceptor to filter jsp pages in the / admin directory

Here is the Interceptor class I implemented:

Package com.test.news.util;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import com.test.news.action.AdminLoginAction;/*** @ author chaoyin*/public class AccessInterceptor extends AbstractInterceptor {private static final long serialVersionUID =-4291195782860785705L; @ Override public String intercept (ActionInvocation actionInvocation) throws Exception {ActionContext actionContext = actionInvocation.getInvocationContext (); Map session = actionContext.getSession () / / except login action Object action = actionInvocation.getAction (); if (action instanceof AdminLoginAction) {return actionInvocation.invoke ();} / check session if (session.get ("user") = = null) {return "logout";} return actionInvocation.invoke (); / / go on}}

Filter: in java web, the request,response you pass in filters out some information in advance, or sets some parameters in advance, and then inputs the action of servlet or struts for business logic, such as filtering out illegal url (address requests that are not login.do, if users do not log in), or uniformly setting the character set before passing in the action of servlet or struts, or removing some illegal characters.

Use the filter to filter the jsp page under the / admin directory. First, configure the filter in web.xml:

Access filter com.test.news.util.AccessFilter access filter / admin/*

The following is the implementation class for filtering:

Package com.test.news.util;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;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession Public class AccessFilter implements Filter {/ * * @ author chaoyin*/ public void destroy () {} public void doFilter (ServletRequest arg0, ServletResponse arg1, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; HttpSession session = request.getSession () If (session.getAttribute ("user") = = null & & request.getRequestURI (). IndexOf ("login.jsp") = =-1) {response.sendRedirect ("login.jsp"); return;} filterChain.doFilter (arg0, arg1) } public void init (FilterConfig arg0) throws ServletException {}} this is the end of the article on "what is the difference between interceptors and filters in Java". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what is the difference between interceptors and filters in Java". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report