๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
FOR SW ENGINEER๐Ÿ‘ฉ‍๐Ÿ’ป/JSP

JSP - ํ•„ํ„ฐ(Filter)

by ์”จ๋‹ˆ 2021. 12. 15.
728x90

ํ•„ํ„ฐ(Filter)

  • HTTP ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค.
  • ํ•„ํ„ฐ๋Š” ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ ์กด์žฌํ•˜๋ฉฐ ํด๋ผ์ด์–ธํŠธ์—์„œ ์˜ค๋Š” ์š”์ฒญ(request)๊ณผ ์ตœ์ข… ์ž์› (jsp, servlet ๋“ฑ) ์‚ฌ์ด์— ์œ„์น˜ํ•ด ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์ •๋ณด ์•Œ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Œ. ๋ฐ˜๋Œ€๋กœ ์ตœ์ข… ์ž์›๊ณผ ํด๋ผ์ด์–ธํŠธ๋กœ ๊ฐ€๋Š” ์‘๋‹ต(response)์‚ฌ์ด์— ์œ„์น˜ํ•ด ์ตœ์ข… ์ž์›์˜ ์š”์ฒญ ๊ฒฐ๊ณผ ์•Œ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ์Œ.
  • ํ•„ํ„ฐ์‚ฌ์šฉ ex) JSP/์„œ๋ธ”๋ฆฟ ๋“ฑ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ด์ „์— ์š”์ฒญ์ด ์˜ฌ๋ฐ”๋ฅธ์ง€ ๋˜๋Š” ์ž์›์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๊ฐ€์กŒ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฏธ๋ฆฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ. JSP๋‚˜ ์„œ๋ธ”๋ฆฟ์ด ์ƒ์„ฑํ•œ ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜์ทจ์†Œํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Œ.

 

1. ํ•„ํ„ฐ(filter) ์ธํ„ฐํŽ˜์ด์Šค

1. public void init(FilterConfig filterConfig) throws ServletException : ํ•„ํ„ฐ ์ดˆ๊ธฐํ™” ํ• ๋•Œ ํ˜ธ์ถœ๋จ.

2. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException : ํ•„ํ„ฐ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰. chain์ด์šฉํ•ด ์ฒด์ธ์˜ ๋‹ค์Œ ํ•„ํ„ฐ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Œ.

3. public void destroy() : ํ•„ํ„ฐ๊ฐ€ ์›น ์ปจํ…Œ์ด๋„ˆ์—์„œ ์‚ญ์ œ๋  ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.

 

-->ํ•„ํ„ฐ์˜ ์—ญํ• ์„ ํ•˜๋Š” ๋ฉ”์„œ๋“œ doFilter() : ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ์ž์›์„ ์š”์ฒญํ–ˆ์„ ๋•Œ ๊ทธ ์ž์› ์‚ฌ์ด์— ํ•„ํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ํ•„ํ„ฐ ๊ฐ์ฒด์˜ doFilter()๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ , doFilter()๋ฉ”์„œ๋“œ๋Š” ์š”์ฒญ์ด ์žˆ์„ ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ์‹คํ–‰๋จ, 

 

2. ํ•„ํ„ฐ ๊ธฐ๋ณธ ๊ตฌ์กฐ

- ํ•„ํ„ฐ๋Š” ํด๋ผ์ด์–ธํŠธ์™€ ์ž์› ์‚ฌ์ด์— ์œ„์น˜ํ•˜๊ณ  ์žˆ๋‹ค.

- ์ž์›์ด ๋ฐ›๋Š” ์ •๋ณด: ํ•„ํ„ฐ๊ฐ€ ๋ณ€๊ฒฝํ•œ ์š”์ฒญ์ •๋ณด, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋Š” ์‘๋‹ต ์ •๋ณด: ํ•„ํ„ฐ๊ฐ€ ๋ณ€๊ฒฝํ•œ ์‘๋‹ต ์ •๋ณด

 

3. ํ•„ํ„ฐ ์ฒด์ธ ๊ตฌ์„ฑ

- ํ•œ๊ฐœ์˜ ํ•„ํ„ฐ๋งŒ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ฉฐ, ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•„ํ„ฐ๊ฐ€ ๋ชจ์—ฌ ํ•„ํ„ฐ ์ฒด์ธ(Chain)์„ ํ˜•์„ฑํ•จ. ์œ„์˜ ๊ทธ๋ฆผ์€ ํ•„ํ„ฐ์ฒด์ธ์˜ ๊ตฌ์กฐ๋ฅผ ๋‚˜ํƒ€๋‚ธ๊ฒƒ.

- ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•„ํ„ฐ๋ฅผ ๊ฑฐ์ณ ์‘๋‹ต๊ณผ ์š”์ฒญ์„ ๋ฐ›์Œ. (์ •๋ณด๋ฟ๋งŒ์•„๋‹ˆ๋ผ ํ๋ฆ„๋„ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ)

- ํ๋ฆ„์„ ๋ณ€๊ฒฝํ•˜์—ฌ, ๋ชจ๋“  ํ•„ํ„ฐ๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๊ณ ๋„ ํด๋ผ์ด์–ธํŠธ๋‚˜ ์ž์›์œผ๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜๋„ ์žˆ์Œ.

 

4. ์ „ํ˜•์ ์ธ ํ•„ํ„ฐ ๊ตฌํ˜„๋ฐฉ๋ฒ•

public class FirstFilter implements Filter{
	
    public void init(FilterConfig filterConfig) throws ServletException{
    	//ํ•„ํ„ฐ ์ดˆ๊ธฐํ™” ์ž‘์—…
    }
    
    public void doFilter(ServletRequest request,
    			ServletResponse response,
                	FilterChain chain)
                    throws IOException, ServletException{
    	//1. request ํ”ผ๋ผ๋ฏธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์š”์ฒญ์˜ ํ•„ํ„ฐ ์ž‘์—… ์ˆ˜ํ–‰
        //RequestํŒŒ๋ผ์ด๋จธ ์ด์šฉํ•ด ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ํ•„ํ„ฐ๋งํ•จ.
        
        //2. ์ฒด์ธ์˜ ๋‹ค์Œ ํ•„ํ„ฐ ์ฒ˜๋ฆฌ
        //Chain.doFilterํ˜ธ์ถœ, ์š”์ฒญ์˜ ํ•„ํ„ฐ๋ง ๊ฒฐ๊ณผ ๋‹ค์Œ ํ•„ํ„ฐ์— ์ „๋‹ฌํ•จ.
        chain.doFilter(request, response);
        
        //3. response๋ฅผ ์ด์šฉํ•ด ์‘๋‹ต์˜ ํ•„ํ„ฐ๋ง ์ž‘์—… ์ˆ˜ํ–‰
        //์ฒด์ธ์„ ํ†ตํ•ด ์ „๋‹ฌ๋œ ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝํ•ด ๊ฒฐ๊ณผ ํด๋ผ์ด์–ธํŠธ์— ์ „์†กํ•จ.
    }
    
    public void destroy(){
    //์ฃผ๋กœ ํ•„ํ„ฐ๊ฐ€ ์‚ฌ์šฉํ•œ ์ž์›์„ ๋ฐ˜๋‚ฉ
    }
}

- ํ•„ํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ, ํ•„ํ„ฐ ๊ฐ์ฒด๋Š” doFilter()๋ฉ”์„œ๋“œ ํ˜ธ์ถœ. doFilter() ๋ฉ”์„œ๋“œ๋Š” ์š”์ฒญ์ด ์žˆ์„ ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ์‹คํ–‰๋จ.

- doFilter() ๋ฉ”์„œ๋“œ ๋‚ด FilterChain์„ ์‚ฌ์šฉํ•ด์„œ ํ•„ํ„ฐ๋Š” ์ฒด์ธ์— ์žˆ๋Š” ๋‹ค์Œ ํ•„ํ„ฐ์— ๋ณ€๊ฒฝํ•œ ์š”์ฒญ/์‘๋‹ต์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Œ.

 

5. ํ•„ํ„ฐ ์‚ฌ์šฉ์˜ˆ์‹œ

โ–ถ web.xml์ด์šฉํ•ด ํ•„ํ„ฐ ์„ค์ •

- ์ฝ˜์†”์ฐฝ์—์„œ์˜ ์ถœ๋ ฅ๊ฒฐ๊ณผ๋ฅผํ†ตํ•ด, f03filter์˜ doFilter๋ฅผ ํ†ต๊ณผํ–ˆ์Œ์„ ํ™•์ธํ• ์ˆ˜ ์žˆ์Œ

- <filter>ํƒœ๊ทธ : ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉํ•  ํ•„ํ„ฐ ์ง€์ •

- <filter-mapping>ํƒœ๊ทธ : ํŠน์ • ์ž์›์— ๋Œ€ํ•ด ์–ด๋–ค ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ• ์ง€ ์ง€์ •.

 

โ–ถ@WebFilter์• ๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ & Servlet๋™์‹œ ์‚ฌ์šฉ ์˜ˆ์‹œ

- Servlet์ด ์š”์ฒญ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ „ Filter๋ฅผ ๊ฑฐ์นœ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์‹œ

filter
Servlet

 

6. ์—ฌ๋Ÿฌ๊ฐœ์˜ ์„œ๋ธ”๋ฆฟ๊ณผ, ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•„ํ„ฐ

- ํ•˜๋‚˜์˜ ํ•„ํ„ฐ๋ฅผ ๊ฑฐ์ณ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ณ ,

web.xml์„ํ†ตํ•ด filter ์„ค์ •
filter์ฝ”๋“œ
/filterex2/s01 ์ฒซ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ
/filterex2/s01 ๋‘๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ

 

- ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•„ํ„ฐ๋ฅผ ๊ฑฐ์ณ ํ•˜๋‚˜์˜ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Œ.

Web.xmlํ†ตํ•ด ํ•„ํ„ฐ ์„ค์ •
/filterex3 ๊ฒฝ๋กœ๋กœ ํ†ตํ•˜๋Š” ์ฒซ๋ฒˆ์จฐ ํ•„ํ„ฐ
/filterex3 ๊ฒฝ๋กœ๋กœ ํ†ตํ•˜๋Š” ๋‘๋ฒˆ์จฐ ํ•„ํ„ฐ
/filter03์„ ๊ฑฐ์น˜๋Š” ๋‘๊ฐœ์˜ filer๋ฅผ ๊ฑฐ์น˜๋Š” Servlet

 

7. ์บ๋ฆญํ„ฐ ์ธ์ฝ”๋”ฉ ํ•„ํ„ฐ

- ๊ธฐ๋ณธ JSP์—์„œ์˜ ์ธ์ฝ”๋”ฉ ์ฝ”๋“œ

<% request.setCharacterEncoding("utf-8"); %>

 

- Filter์—์„œ์˜ ์ธ์ฝ”๋”ฉ ์ฝ”๋“œ

public class CharacterEncodingFilter implements Filter{
	private String encoding;
    
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
    		throws IOException, ServletException{
       //setCharacterEncoding()๋ฉ”์„œ๋“œ ์ด์šฉํ•ด ์š”์ฒญ ์บ๋ฆญํ„ฐ ์ธ์ฝ”๋”ฉ ์„ค์ •
       req.setCharacterEncoding(encoding);
       chain.doFilter(req, res);
    }
    
    @Override
    public void init(FilterConfig config) throws ServletException{
    	//"encoding" ์ดˆ๊ธฐํ™” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ด์šฉํ•ด ์„ค์ •.
    	encoding = config.getInitParameter("encoding");
        if(encoding == null){
        	encoding = "UTF-8";
        }
    }
    
    @Override
    public void destroy(){
    }
}

web.xml์— ์œ„๊ฐ™์€ ์„ค์ •๋„ ์ถ”๊ฐ€

 

728x90