Filter and (interceptor)

        First of all, as we all know, filters are supported by the ervlet container, and interceptors are supported by the Spring container.

Let's talk about Servlet (small service program) = Service+Applet (small application program):

         A servlet is a Java class called by other Java programs and cannot run independently. The life cycle of a servlet is controlled by the servlet container and is divided into three stages: initialization, running, and destruction. The life cycle of a servlet is in the encyclopedia. In a nutshell, it is called by the web container. When the client sends a request, the servlet object is instantiated and initialized, and then the Service() method is called without doPost() or doGetI() or other Finally destroyed. {It should be noted that the servlet is generally only initialized once, that is, when the request is made again, the service() method is not directly called by init()}.

         Use in eclipse as follows:

         See the attachment , the version 4.4 is now used, it seems that the web.xml does not have the previous servlet-mapping, servlet-name and url. Please point out if it is wrong. Thank you.

         After the Servlet is finished, it is time to say the Filter (filter):

<filter>
    <filter-name>mainPageFilter</filter-name>
    <filter-class>com.meihf.hjoyblog.filter.MainPageFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>mainPageFilter</filter-name>
    <url-pattern>/main/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
  </filter-mapping>

           class corresponds to the class directory under your project, and url-pattern is to intercept requests. The newly added <dispatcher> tags after version 2.4 are REQUEST, FORWARD, INCLUDE, ERROR respectively interpreted as: requests from the client, requests from forward, requests from include and <error-page> For incoming requests, the default is REQUEST.

           

package com.hanyx.hjoyblog.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpUtils;

import org.springframework.web.filter.OncePerRequestFilter;

public class MainPageFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain fc)
			throws ServletException, IOException {
		@SuppressWarnings("deprecation")
		CharSequence requestPath = HttpUtils.getRequestURL(request);
		    int start = requestPath.toString().lastIndexOf("main")+4;
			String subPath = (String) requestPath.subSequence(start,requestPath.length());
			request.setAttribute("subPath", "/HJoyBlog/main"+subPath);
			request.getRequestDispatcher("/main").forward(request, response);
		}
	}

 

      The class of the interceptor you wrote inherits OncePerRequestFilter, why is this?

Because to ensure that a request only passes through the interceptor once and does not need to be repeated.

 

       Let's talk about interceptor (interceptor) is to use emission to manage this class. Interceptors can only work on action requests, while filters can work on almost all requests. In addition, in the life cycle of the action, the interceptor can be called multiple times, while the filter is only called once when it is initialized.

       Interceptors are operations that do some business logic. How to use the following code:

       

      Let it be empty first, and the code here has not been written yet. If you configure it, write an interceptor in the same way, and then configure it in struts.xml. I will post it when I write it.

      In the future, the interceptor of SpringMVC will also be added, thank you. Please point out what is wrong.

 

 


          

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989480&siteId=291194637