cool hit counter java filter - use and configuration of filter_Intefrankly

java filter - use and configuration of filter


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/46276529

The filter is used in the process of doing DRP. The other day Chang and Huan introduced us to the idea of AOP in a small conference room, and when we used filter we felt they were both particularly similar.

As shown below.

In javaweb development, request and response are two essential objects that are generated by the web server after each client request is received. The filter can process requests before they reach the servlet and responses when they leave the servlet, respectively. filterOften used to filter sensitive words on websites、 Setting the character set、 Comparison of logs, etc.“ public nature” in the processing of events in the。

In our normal programming, each line is called sequentially from top to bottom, while the filter is "cut" across each complete call. It can be configured to filter on arbitrary code paths.

Here in transcoded/ Setting the character set as an example, A brief look at how to usefilter。

Create Filter

CharsetEncodingFilter.java

package com.drp.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;

/** adoptedfilter standardize treatmentfilter
 * @author Danny
 *
 */
public class CharsetEncodingFilter implements Filter {

	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("CharsetEncodingFilter---->>>>Filter commencement");
		request.setCharacterEncoding("utf-8");    // Setting the character set
		chain.doFilter(request, response);    // Continued implementation
                System.out.println("CharsetEncodingFilter---->>>>Filter close");
	}

	@Override
	public void init(FilterConfig config) throws ServletException {}

}

This filter CharsetEncodingFilter implements the javax.servlet.Filter interface (any filter filter must also implement this interface); when the server creates this filter and initializes the init method, read the configuration file and store it in the FilterConfigure object; implement the doFilter method to set the charset encoding to utf-8 before the request reaches the jsp, the role of the chain.doFiler() method is to jump to the intercepted servlet to continue execution after the request object is processed, and to return here after the execution, the following chain.doFilter() can also process the response object.

Configuring the filter

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <filter>
  	<filter-name>CharsetEncodingFilter</filter-name>
  	<filter-class>com.drp.filter.CharsetEncodingFilter</filter-class>
  </filter>
  <filter-mapping><!--  thisfilter For alljsp Pages are filtered -->
  	<filter-name>CharsetEncodingFilter</filter-name>
  	<url-pattern>*.jsp</url-pattern>
  </filter-mapping>
</web-app>

this The role played by the configuration of the Branch is, thisfilter For alljsp The pages all have Setting the character set effects, When each visit to thejsp page time, All will be implementedfilter hit the targetdoFilter() approach, reason this Per visitjsp Before and after page, Both can be found in theConsole see indoFilter method output of the “CharsetEncodingFilter---->>>>Begin ” harmony “CharsetEncodingFilter---->>>>End ”。

In addition to this there are several other ways of configuring the filter scope.

Filtering effect on all objects

<filter-mapping>
  	<filter-name>CharsetEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
</filter-mapping>

There is a filtering effect on all objects under sysmgr, either servlet or jsp

<filter-mapping>
  	<filter-name>CharsetEncodingFilter</filter-name>
  	<url-pattern>/sysmgr/*</url-pattern>
</filter-mapping>

Only has filtering effect on specified servlets

<filter-mapping>
  	<filter-name>CharsetEncodingFilter</filter-name>
  	<url-pattern>/servlet/LoginServlet</url-pattern>
</filter-mapping>

until this, No matter which one is visitedjsp pages, are pre-set with their character sets, No need to set up each page separately。


Recommended>>
1、Jiangyin to plain logistics special line 18001512693 return truck return truck
2、China will build a map of big data on geographic information of government affairs
3、The original has a variety of spell three when spell three cheat auxiliary seethrough hang
4、Scratch Junior Programming
5、Chinese New Year Red Packet Giveaway Bebop Coins for Bebop

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号