๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

Servlet Filter ์‚ฌ์šฉํ•˜๊ธฐ

by Leica 2020. 2. 11.
๋ฐ˜์‘ํ˜•

Servlet Filter ์‚ฌ์šฉํ•˜๊ธฐ

Filter์˜ ๊ฐœ๋…๊ณผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

ํ•„ํ„ฐ๋Š” ์„œ๋ธ”๋ฆฟ ์‹คํ–‰ ์ „, ํ›„์— ์–ด๋–ค ์ž‘์—…์„ ํ•˜๊ณ ์ž ํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ์˜ ์•”ํ˜ธ๋ฅผ ํ•ด์ œํ•œ๋‹ค๊ฑฐ๋‚˜, ์„œ๋ธ”๋ฆฟ์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ์ž์›์„ ๋ฏธ๋ฆฌ ์ค€๋น„ํ•˜๊ฑฐ๋‚˜, ์„œ๋ธ”๋ฆฟ์ด ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธด๋‹ค๊ฑฐ๋‚˜ ํ•˜๋Š” ์ž‘์—…์„ ํ•„ํ„ฐ๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ•„ํ„ฐ์˜ ์‹คํ–‰

 

๋งŒ์•ฝ ๊ทธ๋Ÿฐ ์ž‘์—…๋“ค์„ ์„œ๋ธ”๋ฆฟ์— ๋‹ด๋Š”๋‹ค๋ฉด ํ•„์š”ํ•œ ์„œ๋ธ”๋ฆฟ๋งˆ๋‹ค ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•ด์•ผํ•˜๊ณ  ํ•„์š”๊ฐ€ ์—†์–ด์ง€๋ฉด ๊ทธ ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ ์„œ๋ธ”๋ฆฟ์„ ๋ชจ๋‘ ์ฐพ์•„์„œ ์‚ญ์ œํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ๊ฐ€ ๋งค์šฐ ๋ฒˆ๊ฑฐ๋กœ์›Œ์ง„๋‹ค.

 

ํ•„ํ„ฐ๋กœ POST Request Parameter ๋ฌธ์ž ์ง‘ํ•ฉ ์„ค์ •ํ•˜๊ธฐ

HTTP POST Request์˜ Message Body ์˜ˆ

POST ์š”์ฒญ์˜ ๊ฒฝ์šฐ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ message body์— ์žˆ๊ณ  ์„œ๋ธ”๋ฆฟ์—์„œ ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด๋ ค๋ฉด getParameter()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ์— ํ•œ๊ธ€๊ณผ ๊ฐ™์€ ๋ฉ€ํ‹ฐ ๋ฐ”์ดํŠธ ๋ฌธ์ž๊ฐ€ ์žˆ์„ ๋•Œ ๊ทธ๋ƒฅ getParameter()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ•œ๊ธ€์ด ๊นจ์ง„๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด setCharacterEncoding()์„ ํ˜ธ์ถœํ•ด์„œ message body์˜ ๋ฐ์ดํ„ฐ์˜ ๋ฌธ์ž ์ง‘ํ•ฉ์„ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

 

request.setCharacterEncoding("UTF-8");

 

๊ทธ๋Ÿฌ๋‚˜ POST ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋ธ”๋ฆฟ ๋งˆ๋‹ค ์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๋ฒˆ๊ฑฐ๋กœ์šด ์ž‘์—…์ด๋‹ค. ์ด ๋•Œ ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ„๋‹จํžˆ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ•„ํ„ฐ ๋งŒ๋“ค๊ธฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class CharacterEncodingFilter implements Filter {
    FilterConfig config;
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.config = filterConfig;
    }
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding(config.getInitParameter("encoding"));
        filterChain.doFilter(servletRequest, servletResponse);
    }
 
    @Override
    public void destroy() { }
}
cs

ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  javax.servlet.Filter ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. Filter ์ธํ„ฐํŽ˜์ด์Šค์— ์„ ์–ธ๋œ init(), doFilter(), destroy()๋Š” ํ•„ํ„ฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ์™€ ๊ด€๋ จ๋œ ๋ฉ”์†Œ๋“œ์ด๋‹ค.

 

(1) init()

init() ๋ฉ”์†Œ๋“œ๋Š” ํ•„ํ„ฐ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ์ค€๋น„ ์ž‘์—…์„ ์œ„ํ•ด ๋”ฑ ํ•œ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค. ์„œ๋ธ”๋ฆฟ์˜ init()๊ณผ ๊ฐ™์€ ์šฉ๋„์ด๋‹ค. ์ด ๋ฉ”์†Œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” FilterConfig์˜ ์ธ์Šคํ„ด์Šค์ด๋‹ค. ์ด ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด ํ•„ํ„ฐ ์ดˆ๊ธฐํ™” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊บผ๋‚ผ์ˆ˜ ์žˆ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ๋Š” doFilter()์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ config์— ์ €์žฅํ•œ๋‹ค.

 

(2) doFilter()

ํ•„ํ„ฐ์™€ ๋งคํ•‘๋œ URL์— ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ๋•Œ๋งˆ๋‹ค doFilter()๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ์— ํ•„ํ„ฐ๊ฐ€ ํ•  ์ž‘์—…์„ ์ž‘์„ฑํ•œ๋‹ค.

filterChain์€ ๋‹ค์Œ ํ•„ํ„ฐ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  filterChain.doFilter()๋Š” ๋‹ค์Œ ํ•„ํ„ฐ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๋‹ค์Œ ํ•„ํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด ๋‚ด๋ถ€์ ์œผ๋กœ ์„œ๋ธ”๋ฆฟ์˜ service()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

์„œ๋ธ”๋ฆฟ์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— ์ฒ˜๋ฆฌํ•  ์ž‘์—…์€ filterChain.doFilter() ์ด์ „์—, ์„œ๋ธ”๋ฆฟ์ด ์‹คํ–‰๋œ ํ›„์— ์ฒ˜๋ฆฌํ•  ์ž‘์—…์€ filterChain.doFilter() ์ดํ›„์— ์ž‘์„ฑํ•œ๋‹ค.

 

๋ณธ ์˜ˆ์ œ์—์„œ๋Š” ์„œ๋ธ”๋ฆฟ์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— request parameter์˜ ๋ฌธ์ž ์ง‘ํ•ฉ์„ ๋จผ์ € ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด filterChain.doFilter() ์ด์ „์— setCharacterEncoding()์„ ํ˜ธ์ถœํ–ˆ๋‹ค.

 

(3) destroy()

์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋Š” ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ข…๋ฃŒํ•˜๊ธฐ ์ „์— ํ•„ํ„ฐ๋“ค์— ๋Œ€ํ•ด destroy()๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋งˆ๋ฌด๋ฆฌ ์ž‘์—…์„ ํ•œ๋‹ค. ๋ณธ ์˜ˆ์ œ๋Š” ํŠน๋ณ„ํžˆ ๋งˆ๋ฌด๋ฆฌํ•  ์ž‘์—…์ด ์—†์œผ๋ฏ€๋กœ ๋นˆ ๋ฉ”์†Œ๋“œ๋กœ ๋‘”๋‹ค.

 

ํ•„ํ„ฐ์˜ ๊ตฌ๋™ ๊ณผ์ •

โ‘  ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋Š” ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•  ๋•Œ DDํŒŒ์ผ(web.xml)์— ๋“ฑ๋ก๋œ ํ•„ํ„ฐ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  init()์„ ํ˜ธ์ถœํ•œ๋‹ค.

โ‘ก ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ํ•ด๋‹นํ•˜๋Š” ํ•„ํ„ฐ์˜ doFilter()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

โ‘ข doFilter()์—์„œ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ณ  ๋‹ค์Œ ํ•„ํ„ฐ์˜ doFilter()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

โ‘ฃ ๋งˆ์ง€๋ง‰ ํ•„ํ„ฐ๊นŒ์ง€ โ‘ข์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

โ‘ค ๋งˆ์ง€๋ง‰ ํ•„ํ„ฐ๋Š” ์„œ๋ธ”๋ฆฟ์˜ service()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

โ‘ฅ ์„œ๋ธ”๋ฆฟ์˜ service()๊ฐ€ ๋๋‚˜๋ฉด service()๋ฅผ ํ˜ธ์ถœํ–ˆ๋˜ ์ด์ „ ํ•„ํ„ฐ๋กœ ๋Œ์•„๊ฐ„๋‹ค.

โ‘ฆ ๋ฐ˜๋ณตํ•ด์„œ ์ œ์ผ ์ฒ˜์Œ ํ˜ธ์ถœ๋๋˜ ํ•„ํ„ฐ๊นŒ์ง€ ๋Œ์•„๊ฐ„๋‹ค.

โ‘ง ๋งˆ์ง€๋ง‰์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋‚ธ๋‹ค.

 

 

ํ•„ํ„ฐ ๋ฐฐ์น˜(Deployment)

ํ•„ํ„ฐ์˜ ๋ฐฐ์น˜ ๋ฐฉ๋ฒ•์€ web.xml์— ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ annotation์— ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

 

web.xml์— ๋ฐฐ์น˜ ์ •๋ณด ์„ค์ •

์„œ๋ธ”๋ฆฟ์˜ ๋ฐฐ์น˜ ์ •๋ณด๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ์œ ์‚ฌํ•˜๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- ํ•„ํ„ฐ ์„ ์–ธ -->
<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>com.atoz_develop.spms.filters.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
 
<!-- ํ•„ํ„ฐ URL ๋งคํ•‘-->
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
cs

<filter-name> : ํ•„ํ„ฐ ๋ณ„์นญ ์„ค์ •

<filter-class> : ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ํฌํ•จํ•œ ํ•„ํ„ฐ ํด๋ž˜์Šค ์ž‘์„ฑ

<init-param> : ํ•„ํ„ฐ๊ฐ€ ์‚ฌ์šฉํ•  ์ •์  ๋ฐ์ดํ„ฐ ์„ค์ •

<filter-mapping> : ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  URL ๋งคํ•‘

    <filter-name> : ํ•„ํ„ฐ ๋ณ„์นญ ์ง€์ •

    <url-pattern> : ํ•„ํ„ฐ๊ฐ€ ์ ์šฉ๋˜์–ด์•ผ ํ•˜๋Š” URL ์ง€์ •. /*์™€ ๊ฐ™์ด ์ง€์ •ํ•˜๋ฉด ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด ํ•„ํ„ฐ ์ ์šฉ

 

์–ด๋…ธํ…Œ์ด์…˜์— ๋ฐฐ์น˜ ์ •๋ณด ์„ค์ •

1
2
3
4
5
6
@WebFilter(
        urlPatterns = "/*",
        initParams = {
                @WebInitParam(name="encoding", value="UTF-8")
        }
)
cs

ํ•„ํ„ฐ ํด๋ž˜์Šค์— ์œ„์™€ ๊ฐ™์ด @WebFilter ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

urlPatterns์— ์ง€์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฐ’์€ ์œ„ web.xml๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์—์„œ <filter-mapping>๋ถ€์™€ ๋™์ผํ•˜๋‹ค.

 

์ด๋ ‡๊ฒŒ ํ•„ํ„ฐ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์ ์šฉํ•˜๋ฉด POST ์š”์ฒญ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊บผ๋‚ด๋Š” ๋ถ€๋ถ„์—์„œ setCharacterEncoding() ํ˜ธ์ถœ๋ถ€๋ฅผ ๋ชจ๋‘ ์ œ๊ฑฐํ•ด๋„ ํ•œ๊ธ€์ด ๊นจ์ง€์ง€ ์•Š๋Š”๋‹ค.

 

ํ•„ํ„ฐ ์ ์šฉ ์‚ฌ๋ก€

์ด๋ ‡๊ฒŒ ํ•„ํ„ฐ๋ฅผ ์ด์šฉํ•ด ์„œ๋ธ”๋ฆฟ ์‹คํ–‰ ์ „, ํ›„์— ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ ์šฉ ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์‚ฌ์ „ ์ž‘์—…(์„œ๋ธ”๋ฆฟ ์‹คํ–‰ ์ „) ์‚ฌํ›„ ์ž‘์—…(์„œ๋ธ”๋ฆฟ ์‹คํ–‰ ํ›„)
๋ฌธ์ž ์ง‘ํ•ฉ ์„ค์ • ์‘๋‹ต ๋ฐ์ดํ„ฐ ์••์ถ•
์••์ถ• ํ•ด์ œ ์‘๋‹ต ๋ฐ์ดํ„ฐ ์•”ํ˜ธํ™”
์•”ํ˜ธํ™”๋œ ๋ฐ์ดํ„ฐ์˜ ๋ณตํ˜ธํ™” ์‘๋‹ต ๋ฐ์ดํ„ฐ ํ˜•์‹ ๋ณ€ํ™˜
๋กœ๊ทธ ์ž‘์„ฑ  
์‚ฌ์šฉ์ž ๊ฒ€์ฆ  
์‚ฌ์šฉ์ž ๊ถŒํ•œ ํ™•์ธ  

 

References

์—ดํ˜ˆ๊ฐ•์˜ ์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์›Œํฌ๋ถ(ํ”„๋ฆฌ๋ ‰, 2016, ์—„์ง„์˜)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€