Filter six: url-pattern setting filter range

table of Contents

One: Introduce problems:

Two: Introduction to url-pattern setting filter range:

Three: Case demonstration

0. Preparation content:

1. Situation 1: When the url-pattern in web.xml is /test.jsp:

2. Situation 2: When the url-pattern in web.xml is /servlet/*:

Four: Emphasis on the explanation: Supplementary explanation for the part of "Three: Case Demonstration": Several points that need attention

1. The first point to note, the url of the servlet is set to "/", which can serve as the root path. At this time, when the url-pattern of the filter is set to "/", the filter can intercept the Servlet

2. The second point to note: the default home page index.jsp will invalidate the things in the first point

3. Facing the two points of attention of 1 and 2, a good practice

4. Compound url-pattern: filter multiple urls at the same time

Five: Set the filter range: the way of writing annotations


One: Introduce problems:

        Usually url-pattern is set to "/*"; that is, all requests are filtered; but when there are special requirements, that is, how to do when changing the filtering range?


Two: Introduction to url-pattern setting filter range:


Three: Case demonstration

0. Preparation content:

Case: Including UrlPatternFilter filter class; Servlet class SampleServlet1; test.jsp;

test.jsp: The visited url is: localhost:8080/url-pattern/test.jsp

SampleServlet1 this Servlet: The url to visit is: localhost:8080/url-pattern/servlet/sample1

package com.imooc.filter;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SampleServlet1
 */
@WebServlet("/servlet/sample1")
public class SampleServlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SampleServlet1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 仅为演示目的,这儿打印类名
		response.getWriter().println("This is"+this.getClass().getSimpleName());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Filter class: UrlPatternFilter: The main purpose of this blog is url-pattern, so in the doFilter method of this filter, the requested url is printed:

package com.imooc.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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UrlPatternFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		// 这儿打印拦截到的url
		System.out.println("拦截到"+req.getRequestURL()); // getRequestURL选用的是返回值为StringBuffer的那一个
		chain.doFilter(request, response);
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}

1. Situation 1: When the url-pattern in web.xml is /test.jsp:

When the url-pattern in web.xml is /test.jsp: it is an accurate match of resources at this time.

When accessing test.jsp:

But if, when accessing SampleServlet1: 

     

2. Situation 2: When the url-pattern in web.xml is /servlet/*:

If you want the filter to intercept localhost:8080/url-pattern/servlet/sample1, the url-pattern should be set as follows:

Effect: Found that localhost:8080/url-pattern/servlet/sample1 can be intercepted at this time;

Note: The url value in the <url-pattern></url-pattern> tag, for example, only write /servlet/* instead of /url-pattern/servlet/*, that is, it omits the context path!


Four: Emphasis on the explanation: Supplementary explanation for the part of "Three: Case Demonstration": Several points that need attention

/: Mapping problem: Several error-prone problems that need to be explained! ! !

1. The first point to note, the url of the servlet is set to "/", which can serve as the root path. At this time, when the url-pattern of the filter is set to "/", the filter can intercept the Servlet

First, create SampleServlet2: the url of this servlet is "/", which means that this Servlet should be set as the default homepage of the project, that is, when you directly visit localhost:8080/url-pattern/, you will visit this Servlet, and then this Servlet will go to a specific front-end interface (html or jsp, etc.);

At the same time, the url-pattern of the filter in web.xml is set to: "/", in order to access SampleServlet2, the filter can intercept this request:

If you visit: localhost:8080/url-pattern/: do not add any other resources later, it directly visits SampleServlet2 (that is, at this time, SampleServlet2 seems to be the "default homepage" of the application (actually, you can't say that)) ; At the same time, the filter also intercepted this request;

However, it should be noted that when this "/" maps the root path of the web application, it only works on Servlet! And: the default home page index.jsp will make it invalid! ! !

…………………………………………………… 

2. The second point to note: the default home page index.jsp will invalidate the things in the first point

If you are in the project at this time, add an index.jsp: the default home page

So at this time, when accessing localhost:8080/url-pattern/, which one of SampleServlet2 and index.jsp takes effect?

At this point, visit localhost:8080/url-pattern/ again: When no other resources are added later, the default homepage index.jsp is accessed instead of SampleServlet2 ; at the same time, it is not filtered in the console of the program. Record intercepted by the device.

Question: That is, the url of SampleServlet2 is configured as "/", that is, the root path is set, and the filter url-pattern is configured as "/" in web.xml; why visit localhost:8080/url-pattern/ At that time, not only did the SampleServlet2 not be accessed, but the filter did not intercept localhost:8080/url-pattern/? ? ?

Reason: This involves a low-level mechanism of Tomcat. Although the "/" (root path, that is, localhost:8080/url-pattern/) is accessed in the url, it is because the web.xml is configured with the default home page (that is, index .html, index.jsp), in fact, when accessing the root path of localhost:8080/url-pattern/, it implicitly jumps to index.jsp; at the same time, the filter does not intercept index.jsp;

At the same time, it is found that the priority of the default homepage is higher than that of the Servlet (url is set to "/"). Once the default homepage is set (such as index.jsp), the path above will be set to "/" SampleServlet2 is invalid, that is, "the default homepage index.jsp will invalidate the function of "/" as mentioned above .

If you already have index.jsp and you want the filter to intercept index.jsp (default homepage), then the url-pattern of the filter needs to be set to: *.jsp, or /index.jsp;

……………………………………………………

3. Facing the two points of attention of 1 and 2, a good practice

In order to avoid the above problems, a good approach is: in actual project development, try not to set the default homepage such as index.jsp; instead, set the servlet with url as "/", and use this servlet mapped to the root path to jump around. Go to the specified page to display. This can ensure that the Servlet (with the url set to "/") will not fail, and can manually control which page we want to display;! ! ! ! ! ! ! ! ! !

……………………………………………………

4. Compound url-pattern: filter multiple urls at the same time

For the filter, you want to filter the default root path, filter all JSPs, and filter all URLs prefixed by Servlet. What should we do with this composite?

Results: found that access to the default homepage index.jsp was also blocked by the filter at this time: very simple, because index.jsp is the default homepage. When the browser visits localhost:8080/url-pattern/, it will naturally visit index. .jsp; However, because *.jsp is added to the filter, index.jsp will naturally be intercepted.


Five: Set the filter range: the way of writing annotations

 

Guess you like

Origin blog.csdn.net/csucsgoat/article/details/114273595