inotgo.com

Step 1 : Filter concept   
Step 2 : to write FirstFilter   
Step 3 : to configure web.xml   
Step 4 : Visit the page   
Step 5 : init() method   
Step 6 : Filter Startup failed   

Filter Like a sentry post , The user's request needs to go through Filter
And there can be multiple filters
Filter concept
Develop a simple FirstFilter, Used to print user access ip Address and page visited

HttpServletRequest request = (HttpServletRequest) req;

doFilter() Method req The type of the parameter is ServletRequest, Need to convert to HttpServletRequest Types make it easy to call certain methods ( reference resources request Common methods )

String ip = request.getRemoteAddr();

Get the name of the incoming user ip address

String url = request.getRequestURL().toString();

Get the page address visited by the user

System.out.printf("%s %s Visited %s%n", date, ip, url);

Print it out on the console

chain.doFilter(request, response);

filter Release , Indicates to continue running the next filter , Or finally visit some servlet,jsp,html wait
 to write  FirstFilter
package filter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; 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 FirstFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String ip = request.getRemoteAddr(); String url = request.getRequestURL().toString(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d = new Date(); String date = sdf.format(d); System.out.printf("%s %s Visited %s%n", date, ip, url); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { } }
stay web.xml In filter Configuration of , and servlet The configuration is very similar

<url-pattern>/*</url-pattern>

Indicates that all accesses will be filtered

If configured as
<url-pattern>*.jsp</url-pattern>

It means only filtering jsp
<filter> <filter-name>FirstFilter</filter-name> <filter-class>filter.FirstFilter</filter-class> </filter> <filter-mapping> <filter-name>FirstFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
	<filter>
		<filter-name>FirstFilter</filter-name>
		<filter-class>filter.FirstFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>FirstFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
Restart tomcat, Access information of all users , Can be printed out
 Visit the page
With Servlet Need to configure Self starting Will follow tomcat Start and execute init() The method is different .

Filter certain Will follow tomcat The start-up is self starting .
init()  method
package filter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; 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 FirstFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String ip = request.getRemoteAddr(); String url = request.getRequestURL().toString(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d = new Date(); String date = sdf.format(d); System.out.printf("%s %s Visited %s%n", date, ip, url); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("First Filter init()"); } }
Filter yes web Application is a very important link , If Filter Startup failed , Or there is a compilation error , Not only this Filter out of commission , whole web The application will fail to start , The user cannot access the page

At startup tomcat In the process , You will also see such words :

serious : Context [] startup failed due to previous errors

This is often used to prompt Filter Startup failed
Filter Startup failed
package filter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; 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 FirstFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String ip = request.getRemoteAddr(); String url = request.getRequestURL().toString(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d = new Date(); String date = sdf.format(d); System.out.printf("%s %s Visited %s%n", date, ip, url); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println(" Deliberately causing an anomaly "); Object o = null; o.toString(); } }


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2021-01-03 add Filter The client cannot jump after
henYuan

about J2EE-Filter-Hello Filter Your questions



add Filter The client cannot jump after , Server jump can ; Everything is normal before you add it
 Loading
response.sendRedirect("listHero");
 
               


1 One answer

mumu123123
Answer time :2021-10-04
The code is wrong



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





2020-07-18 about ip address
Dream hacker

about J2EE-Filter-Hello Filter Your questions
Why? tomcat The console displays ip The address is the same as that of my host ip The address is different




1 One answer

Ah Ju
Answer time :2020-08-28
Check whether the virtual machine is used or the computer has multiple network cards



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-07-01 The files are normal , but tomcat Cannot display automatically after startup FirstFilter.java In file doFilter() Method content
2020-06-26 Why print twice init?
2020-06-19 Yes Filter Then , Start tomcat Always reporting errors


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 21 Previous questions , please Click to view

Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about J2EE-Filter-Hello Filter Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot