Servlet๊ณผ JSP ํ•„ํ„ฐ๋Š” ์„œ๋ธ”๋ฆฟ๊ณผ JSPํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” Java ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

  • ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ์˜ ์š”์ฒญ์ด ๋ฐฑ์—”๋“œ๋กœ ๊ฐ€๊ธฐ ์ „์— ๊ฐ€๋กœ์ฑ„๊ธฐ ์œ„ํ•ด์„œ

  • ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ์˜ ์‘๋‹ต์ด ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ด์ง€๊ธฐ ์ „์— ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ

์•„๋ž˜๋Š” ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํ•„ํ„ฐ๋“ค์˜ ์ข…๋ฅ˜์ž…๋‹ˆ๋‹ค.

  • Authentication Filters.

  • Data compression Filters

  • Encryption Filters .

  • Filters that trigger resource access events.

  • Image Conversion Filters .

  • Logging and Auditing Filters.

  • MIME-TYPE Chain Filters.

  • Tokenizing Filters .

  • XSL/T Filters That Transform XML Content.

Filter๋Š” deployment descriptor ํŒŒ์ผ์ธ web.xml์„ ํ†ตํ•ด์„œ ๋””ํ”Œ๋กœ์ด ๋˜๊ณ  servlet ์ด๋‚˜ JSP์— ๋งคํ•‘์ด ๋ฉ๋‹ˆ๋‹ค. web.xml ํŒŒ์ผ์€ <Tomcat-installation-directory>\conf ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ์ฐพ์œผ์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JSP ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•  ๋•Œ, ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ฐ ํ•„ํ„ฐ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹น์‹ ์ด deployment descriptor์— ์„ ์–ธํ•œ ํ•„ํ„ฐ๋“ค์„ ๋ง์ด์ฃ . ๊ทธ ํ•„ํ„ฐ๋“ค์€ ์ •์˜๋œ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. 


Servlet Filter Methods:

ํ•„ํ„ฐ๋Š” ๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ ๊ทธ๋ƒฅ Java ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.  javax.servlet.Filter ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์ฃ . The javax.servlet.Filter interface ๋Š” ์•„๋ž˜์˜ ์„ธ๊ฐ€์ง€ ๋ฉ”์†Œ๋“œ๋ฅผ ์„ ์–ธํ•˜๊ณ ์žˆ์Šต๋‹ˆ๋‹ค.

S.N.Method & Description
1public void doFilter (ServletRequest, ServletResponse, FilterChain)
This method is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
2public void init(FilterConfig filterConfig)
This method is called by the web container to indicate to a filter that it is being placed into service.
3public void destroy()
This method is called by the web container to indicate to a filter that it is being taken out of service.


JSP Filter ์˜ˆ์ œ

์•„๋ž˜๋Š” JSP Filter ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ๋ฌด์—‡์„ ํ•˜๋Š”๊ณ  ํ•˜๋‹ˆ, ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ ๋•Œ๋งˆ๋‹ค ํด๋ผ์ด์–ธํŠธ์˜ IP์ฃผ์†Œ๋ฅผ ํ”„๋ฆฐํŠธํ•˜๊ณ  ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋ฟŒ๋ ค์ค๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ JSP Filter์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•์„ ์ตํž ์ˆ˜ ์žˆ์„๊ฑฐ์—์š”.  

// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; // Implements Filter class public class LogFilter implements Filter { public void init(FilterConfig config) throws ServletException{ // Get init parameter String testParam = config.getInitParameter("test-param"); //Print the init parameter System.out.println("Test Param: " + testParam); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException { // Get the IP address of client machine. String ipAddress = request.getRemoteAddr(); // Log the IP address and current timestamp. System.out.println("IP "+ ipAddress + ", Time " + new Date().toString()); // Pass request back down the filter chain chain.doFilter(request,response); } public void destroy( ){ /* Called before the Filter instance is removed from service by the web container*/ } }

์ด์ œ LogFilter.java๋ฅผ ์ปดํŒŒ์ผํ•˜๊ณ  LogFilter.class ํŒŒ์ผ์„ <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes ๋””๋ ‰ํ† ๋ฆฌ์— ๋„ฃ์–ด์ฃผ์„ธ์š”. ๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์—์„œ ๋งคํ•‘์‹œ์ผœ์ฃผ๋Š” ์ž‘์—…์„ ๋” ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. 


JSP Filter Mapping in Web.xml:

web.xml ์—์„œ ํ•„ํ„ฐ๋ฅผ ๋งคํ•‘ํ•  ๋•Œ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

<filter> <filter-name>LogFilter</filter-name> <filter-class>LogFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

์œ„ ์†Œ์Šค์ฒ˜๋Ÿผ ๋งคํ•‘ํ•˜๋ฉด ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ๊ณผ JSP์— ์ ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค.  url pattern์—์„œ /* ์ด ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ๋ชจ๋“  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ด๊ณณ์— ํŠน์ • servlet ์ด๋‚˜ JSP๋ฅผ ๋„ฃ์–ด ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋ง์ด์ฃ .

์ด์ œ JSPํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•˜๋ฉด ์›น์„œ๋ฒ„ ๋กœ๊ทธ์—์„œ ์ƒ์„ฑ๋œ ๋กœ๊ทธ๋ฅผ ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  Log4J ๋กœ๊ฑฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹ค๋ฅธ ํŒŒ์ผ์— ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•  ์ˆ˜๋„ ์žˆ๋‹ค๋Š”์ ๋„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.


Filter ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉํ•˜๊ธฐ

์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ํ•„ํ„ฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ฐ๊ฐ ๋งคํ•‘์„ ํ•˜๋Š”๊ฑฐ์ฃ . 

<filter> <filter-name>LogFilter</filter-name> <filter-class>LogFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param> </filter> <filter> <filter-name>AuthenFilter</filter-name> <filter-class>AuthenFilter</filter-class> <init-param> <param-name>test-param</param-name> <param-value>Initialization Paramter</param-value> </init-param> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>AuthenFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

Filters Application Order:

ํ•„ํ„ฐ์˜ ์ ์šฉ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์ •์˜๋œ ํ•„ํ„ฐ๊ฐ€ ๋จผ์ € ์ ์šฉ์ด ๋˜๋Š”๊ฑฐ์ฃ . ์œ„ ์˜ˆ์ œ์—์„œ๋Š” LogFilter๊ฐ€ AuthenFilter๋ณด๋‹ค ์šฐ์„  ์ ์šฉ์ด ๋˜๋Š”๊ฑฐ๊ณ  ์•„๋ž˜ ์˜ˆ์ œ์—์„œ๋Š” AuthenFilter๊ฐ€ LogFilter๋ณด๋‹ค ์šฐ์„  ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

<filter-mapping> <filter-name>AuthenFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_writing_filters.htm