JiwonDev

#17 Servlet Filter

by JiwonDev

# Filter

์„œ๋ธ”๋ฆฟ 2.3๋ถ€ํ„ฐ ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์œผ๋กœ ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €)๊ฐ€ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋ƒˆ์„ ๋•Œ, ์š”์ฒญ์ด ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๊ฐ€๊ธฐ์ „์— ํ•„ํ„ฐ๋ง์„ ํ•˜๋Š” ๊ธฐ์ˆ ์ด๋‹ค. JSP/Servlet ์ŠคํŽ™์—์„œ๋Š” javax.servlet.Filter ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ตฌํ˜„ํ•˜์—ฌ web.xml์— ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

Filter๋„ ์‹ฑ๊ธ€ํ†ค์„ ๊ตฌํ˜„ํ•˜๋‚˜์š”? (X)

// Filter ์ธํ„ฐํŽ˜์ด์Šค๋Š” Servlet๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
package javax.servlet;
import java.io.IOException;
public interface Filter {
// ์‹ฑ๊ธ€ํ„ด์„ ๊ตฌํ˜„ํ•˜์ง€๋Š” ์•Š์œผ๋‚˜, ๋ณดํ†ต 1๊ฐœ์˜ ๊ฐ์ฒด๋งŒ์„ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
// web.xml์˜ ์„ค์ •์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ๋‹ค.
public void init(FilterConfig filterConfig) throws ServletException;
// ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด, ์š”์ฒญ/์‘๋‹ต์„ ์กฐ์ž‘ํ•˜๊ณ 
// FilterChain์„ ์ด์šฉํ•ด ์กฐ์ž‘ ์ดํ›„ ์›๋ž˜ ๋ชฉ์ ์ง€์ธ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค.
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException;
public void destroy();
}
public class LoggingFilter implements Filter {
//ํ•„ํ„ฐ๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ดˆ๊ธฐํ™”์‹œ ์‚ฌ์šฉ
// config ๊ฐ์ฒด์—์„œ ํ•„ํ„ฐ ์ •๋ณด, ์ฃผ์–ด์ง„ ํŒŒ๋ผ๋ฉ”ํƒ€, ์„œ๋ธ”๋ฆฟ ์ •๋ณด๋“ฑ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
public void init(FilterConfig config) throws ServletException {
System.out.println("ํ•„ํ„ฐ ์ดˆ๊ธฐํ™” ๋จ");
}
//์š”์ฒญ์‹œ๋งˆ๋‹ค ํ•„ํ„ฐ๊ฐ€ ์‹คํ–‰ํ•  ๋ฉ”์„œ๋“œ
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("์š”์ฒญ์ด ํ•„ํ„ฐ์—์„œ ์ฒ˜๋ฆฌ๋จ");
chain.doFilter(request, response); // ์กฐ์ž‘์„ ์™„๋ฃŒํ•œ ์š”์ฒญ/์‘๋‹ต์„ ๋ฐ˜ํ™˜.
System.out.println("์‘๋‹ต์ด ํ•„ํ„ฐ์—์„œ ์ฒ˜๋ฆฌ๋จ");
}
//ํ•„ํ„ฐ๊ฐ€ ์ œ๊ฑฐ๋  ๋•Œ ์‹คํ–‰
public void destroy() {
System.out.println("ํ•„ํ„ฐ ์ œ๊ฑฐ๋จ.");
}
}
// web.xml
<filter>
<filter-name>loggingFilter</filter-name>
<filter-class>test.LoggingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loggingFilter1</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>loggingFilter2</filter-name>
<url-pattern>/๋“ฑ๋กํ• url1</url-pattern>
<url-pattern>/๋“ฑ๋กํ• url2</url-pattern>
<url-pattern>/url3</url-pattern>
<servlet-name>๋“ฑ๋กํ•œ ์„œ๋ธ”๋ฆฟ ์ด๋ฆ„</servlet-name>
</filter-mapping>

์ฐธ๊ณ ๋กœ filter-mapping์€ servlet-mapping๊ณผ ๋™์ผํ•˜๋ฉฐ, [ *.jsp ]๋กœ ๋งคํ•‘ํ•˜๋ฉด ๋ชจ๋“  jsp ์š”์ฒญ์‹œ ํ•ด๋‹น ํ•„ํ„ฐ๊ฐ€ ์ž‘๋™ํ•œ๋‹ค. ๋“ฑ๋ก๋œ ํ•„ํ„ฐ๋Š” ์ ์€ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋œ๋‹ค. (๋‹จ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ํ•„ํ„ฐ๋ฅผ ์ง€์ •ํ•œ ๊ฒฝ์šฐ์—๋Š” ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.)


# ์™œ ํ•„ํ„ฐ๊ฐ€ ํ•„์š”ํ•˜์ฃ ?

์›น ์ปดํฌ๋„ŒํŠธ(Servlet, JSP)์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ์ผ์„ ํ•„ํ„ฐ๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ UTF-8๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž. ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ์›น ์ปดํฌ๋„ŒํŠธ์—์„œ ๋™์ผํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

request.setCharacterEncoding("utf-8");
String param = request.getParameter("param");

์ฝ”๋“œ์˜ ์ค‘๋ณต๋„ ๋ฌธ์ œ์ง€๋งŒ, ๋งŒ์•ฝ ์ „์ฒด ์ปดํฌ๋„ŒํŠธ์— ๋Œ€ํ•ด UTF-8์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ์ธ์ฝ”๋”ฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ์œ ์ง€๋ณด์ˆ˜๋‚˜ ๊ฐœ๋ฐœ ์ธก๋ฉด์—์„œ๋„ ์ข‹์ง€ ์•Š๊ธฐ์— ํ•„ํ„ฐ๋ผ๋Š” ๊ฐœ๋…์„ ์ด์šฉํ•ด ๊ณตํ†ต๋œ ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


# ํ•„ํ„ฐ์˜ ์ฃผ์š” ๊ธฐ๋Šฅ

๋กœ๊ทธ์ธ ์—ฌ๋ถ€ํ™•์ธ, ์š”์ฒญ/์‘๋‹ต ๋กœ๊ทธ๊ธฐ๋ก, ๋ฐ์ดํ„ฐ ์˜ค๋ฅ˜์ฒ˜๋ฆฌ, ๋ฐ์ดํ„ฐ ์••์ถ•&๋ณ€ํ™˜ (์ธ์ฝ”๋”ฉ)๋“ฑ์„ ๋ณดํ†ต ํ•„ํ„ฐ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

  • ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋˜๊ธฐ ์ „์— ์š”์ฒญ(request)๋ฅผ ๊ฐ€๋กœ์ฑ„์–ด ์กฐ์ž‘ํ•œ๋‹ค.
  • ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋˜๊ธฐ ์ „์— ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„์–ด ์ ๊ฒ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋˜๊ธฐ ์ „์— HTTP ์š”์ฒญ์˜ ํ—ค๋”๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋œ ์ดํ›„ ์‘๋‹ต(response)๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์ „์— ๊ฐ€๋กœ์ฑ„์–ด ์กฐ์ž‘ํ•œ๋‹ค.
  • ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋œ ์ดํ›„ ์‘๋‹ต ํ—ค๋”๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

# @WebFilter ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ํ•„ํ„ฐ ์„ค์ •ํ•˜๊ธฐ

์„œ๋ธ”๋ฆฟ ์ŠคํŽ™ 3.0 (Tomcat 7.0 ์ด์ƒ)๋ถ€ํ„ฐ ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•œ ๋งตํ•‘(web.xml ์„ค์ •)์„ ์ง€์›ํ•˜๋Š”๋ฐ, ํ•„ํ„ฐ๋„ ๋น„์Šทํ•˜๊ฒŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•„ํ„ฐ ์‹คํ–‰์ˆœ์„œ๋Š” ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋ฐ, ๋งŒ์•ฝ ์ˆœ์„œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด ํ•„ํ„ฐ ์ด๋ฆ„๋งŒ ๋“ฑ๋กํ•ด๋‘๊ณ ๊ธฐ์กด์˜ web.xml ์—์„œ ๋“ฑ๋กํ•˜๋˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

// web.xml์— ํ•„ํ„ฐ๋ฅผ ๋“ฑ๋กํ•˜๋Š”๊ฒƒ๊ณผ ๋™์ผํ•œ ์ฝ”๋“œ์ด๋‹ค.
@WebFilter(filterName ="loggingFilter")
public class LoggingFilter3 implements Filter {...}
// ๋งคํ•‘์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
@WebFilter("*.jsp")
public class LoggingFilter1 implements Filter {...}
@WebFilter("/target") // ๋งคํ•‘ URL ์ž…๋ ฅ
public class LoggingFilter2 implements Filter {...}
// URL ํŒจํ„ด ์ง€์ •. ์—ฌ๋Ÿฌ๊ฐœ์ผ ๋• {}์‚ฌ์šฉ
@WebFilter(urlPatterns= {"/target", "/target2"})
public class LoggingFilter3 implements Filter {...}
// ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„œ๋ธ”๋ฆฟ ์ง€์ •. ์—ฌ๋Ÿฌ๊ฐœ์ผ ๋• {} ์‚ฌ์šฉ
@WebFilter(servletNames= {"boardController", "boardController2"})
public class LoggingFilter4 implements Filter {...}

 

# ํ•„ํ„ฐ์— ์ž…๋ ฅ ๊ฐ’ ๋„ฃ๊ธฐ

ํ•„์š”ํ•˜๋‹ค๋ฉด ํ•„ํ„ฐ๋ฅผ ์‹คํ–‰ํ•  ๋–„ ์ž…๋ ฅ ํŒŒ๋ผ๋ฉ”ํƒ€๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

<filter>
<filter-name>helloFilter</filter-name>
<filter-class>test.HelloFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>encoding2</param-name>
<param-value>hi hello ์•ˆ๋…•!</param-value>
</init-param>
</filter>
@WebFilter(
value = {"/target", "/target2"},
initParams = @WebInitParam(name = "encoding", value = "UTF-8")
)
public class EncodingFilter1 implements Filter {...}
// ์ž…๋ ฅํ•˜๊ณ  ์‹ถ์€ ํŒŒ๋ผ๋ฉ”ํƒ€๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ๊ฒฝ์šฐ
@WebFilter(
initParams = {
@WebInitParam(name = "encoding", value = "UTF-8"),
@WebInitParam(name = "encoding2", value = "EUC-KR")
}
)
public class EncodingFilter2 implements Filter {...}

 

์ด๋ ‡๊ฒŒ ์ฃผ์–ด์ง„ ํŒŒ๋ผ๋ฉ”ํƒ€๋Š” Filter ์ธํ„ฐํŽ˜์ด์Šค์•ˆ์—์žˆ๋Š” init() ๋ฉ”์„œ๋“œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

public void init(FilterConfig config) throws ServletException {
String encoding1 = config.getInitParameter("encoding1");
String encoding2 = config.getInitParameter("encoding2");
if(encoindg1 != null){
// ํ•„ํ„ฐ๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ํ•  ์ž‘์—…..
}
}

 

 

# @WebFilter ์˜ load-on-startup ์†์„ฑ 

์„œ๋ธ”๋ฆฟ์€ ๋ธŒ๋ผ์šฐ์ €์˜ ์ตœ์ดˆ ์š”์ฒญ์‹œ์— init()๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ WAS ๋ฉ”๋ชจ๋ฆฌ์— ๊ฐ์ฒด๋ฅผ ๋กœ๋“œํ•œ๋‹ค.

๊ทธ๋ž˜์„œ ์„œ๋ฒ„๋ฅผ ์ œ์ผ ์ฒ˜์Œ์— ๊ฐ€๋™ํ•  ๋•Œ, ์†๋„๊ฐ€ ์ƒ๋‹นํžˆ ๋Š๋ฆฐ๋ฐ ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด load-on-startup ์†์„ฑ์„ ์ด์šฉ ํ•˜์—ฌ ํ˜ธ์ถœ ๋˜๊ธฐ์ „์— ๋ฏธ๋ฆฌ ์ดˆ๊ธฐํ™”(=๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌ) ํ•ด๋‘˜ ์ˆ˜ ์žˆ๋‹ค. 

  • ํ†ฐ์บฃ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์„ ๋ฏธ๋ฆฌ ์ดˆ๊ธฐํ™” ํ•ด๋‘”๋‹ค.
  • ์ง€์ •ํ•œ ์ˆซ์ž๋Š” ์šฐ์„ ์ˆœ์œ„๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ์ž‘์€์ˆซ์ž๋ถ€ํ„ฐ ๋จผ์ € ์ดˆ๊ธฐํ™” ๋œ๋‹ค. 
  • ํ•ด๋‹น ์˜ต์…˜์˜ ๊ธฐ๋ณธ ๊ฐ’์€ 0์œผ๋กœ, ์ง€์ •ํ•œ ์ˆซ์ž๊ฐ€ 0 ๋ณด๋‹ค ํฌ๋ฉด ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ ์„œ๋ธ”๋ฆฟ์ด ์ดˆ๊ธฐํ™” ๋˜๋Š” ๊ตฌ์กฐ.
web.xml์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์†์„ฑ๋ถ€์—ฌ๊ฐ€๋Šฅ.
<servlet>
<servelt-name>loadConfig</servelt-name>
<sevlert-class>LoadAppconfig</sevlert-class>
<load-on-startup> 1 </load-on-startup>
</servlet>
// ์„œ๋ธ”๋ฆฟ ์ŠคํŽ™ 3.0๋ถ€ํ„ฐ ์ œ๊ณตํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•.
@WebServlet(name = "loadAppConfig", urlPatterns = { "/loadConfig" }, loadOnStartup = 1)
public class LoadAppConfig extends HttpServlet {
private ServletContext context;
@Override
public void init(ServletConfig config) throws ServletException {
// loadOnStartup ์†์„ฑ ๋•Œ๋ฌธ์— init()์ด ์„œ๋ฒ„์‹œ์ž‘๊ณผ ํ•จ๊ป˜ ๊ฐ™์ด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
...
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
...
}
}
๋ธ”๋กœ๊ทธ์˜ ํ”„๋กœํ•„ ์‚ฌ์ง„

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ