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.

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

package com.drp.filter;

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 {

	public void destroy() {}

	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");

	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


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 

  <filter-mapping><!--  thisfilter For alljsp Pages are filtered -->

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


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


Only has filtering effect on specified servlets


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

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字,当前共 发送