Article ¿ä¾à:
Web tier Design Guideline ¹× Servlet 2.3 APIÀÇ Filter
interfaceÀÇ »ç¿ë ¿ëµµ ¹× Example ¼Ò°³
Article ³»¿ë:
Servlet 2.3 spec¿¡¼ Ãß°¡µÈ Filter interface´Â Web
component¿¡ ´ëÇÑ RequestÇϱâÀü, ResponseÈÄ¿¡ ±â´ÉÀÇ È®Àå¹× º¯°æÀ» Áö¿øÇϱâ À§ÇÑ
interfaceÀÌ´Ù. ÀÌÀü±îÁö, Servlet/JSP¸¦ °³¹ßÇÒ¶§, Äڵ带 Àç»ç¿ëÇϱâ À§ÇÑ ¹æ¹ý, Áï
°øÅëÀûÀÎ ±â´ÉÀ» ÇϳªÀÇ ¼Ò½ºÆÄÀÏ·Î °ü¸®Çϱâ À§ÇØ, »ó¼ÓÀ» ÀÌ¿ëÇϰųª,
RequestDispatcherÀÇ include(),forward()¸¦ ÀÌ¿ëÇß´Ù. ȤÀº Web
tier design guidelineÀ¸·Î Àç½ÃµÈ Model -¥±¸¦ ÀÌ¿ëÇϱ⵵ Çß´Ù.
Model
-¥° À§ÀÇ Model -¥°Àº °¢°¢ÀÇ jsp¸¦
requestÇϸé, jsp´Â presetationÀ» ´ã´çÇÏ°í, JSP Bean, Custom Tag
Handler°¡ Businsess logicÀ» ´ã´çÇÏ°Ô µÇ´Â °ÍÀÌ´Ù. ¿©±â¼ Äڵ带 Àç»ç¿ëÇϰųª
°øÅëÀûÀÎ ±â´É ÅëÇÕ °ü¸® Çϱâ À§Çؼ´Â RequestDistpatcherÀÇ include(),
forward()¸¦ ÀÌ¿ëÇÏ´Â °ÍÀÌ´Ù. ±×¸®°í, ServletÀº ±ÇÇÑ check, logging°ú °°Àº
ȸé°ú´Â °ü·Ã ¾ø´Â ºÎºÐÀÇ ÄÄÆ÷³ÍÆ®·Î ÀÌ¿ëÀÌ µÇ¾î Áö´Â °ÍÀÌ´Ù.
Model
-¥± Model -¥±´Â Facade PatternÀÌ´Ù.
ÄÚµåÀÇ Àç »ç¿ë¼º, °øÅëÀû ±â´ÉÀ» ÅëÇÕ °ü¸®Çϱâ À§ÇÑ DesignÀ̶ó ÇÒ¼ö ÀÖ°Ú´Ù. ¸ðµç
Request´Â Front ControllerÀÎ Conversational Controller°¡ ¸ÕÀú
¹Þ¾Æ °øÅëÀûÀÎ ±â´ÉÀ» ó¸®ÇÏ°Ô µÈ´Ù. ±×¸®°í, ÀÌ Front Controller´Â ½ÇÀç ÆĶó¹ÌÅÍ·Î
³Ñ¾î¿À´Â request¸¦ dispatch(Action)ÇÏ¿© Presentation°ú business
logicÀ» ó¸®ÇÏ°Ô µÇ´Â °ÍÀÌ°í, response¸¦ modifyÇϰųª, °øÅëÀûÀÎ
presentationÀ» À§ÇØ JSP,servletÀ» includeÇÒ¼öµµ ÀÖ´Ù.
À§¿¡¼ °£´ÜÇÏ°Ô ¼³¸íÇÑ ÀÌ·¯ÇÑ guideline°ú´Â º°µµ·Î Servlet2.3¿¡¼´Â Filter¶ó´Â
interface¸¦ Á¦°øÇÑ´Ù. Model -¥±¿Í °°Àº DesignÀ» ¼Õ½±°Ô ±¸ÇöÇÒ¼ö ÀÖ°í, °³¹ßµÈ
¼Ò½º¸¦ º¯°æÇÏÁö ¾Ê°í, Security, Logging, CRMÀ» À§ÇÑ request data
ºÐ¼®µîÀÇ ±â´ÉÀ» ¼Õ½±°Ô È®ÀåÇϱâ À§ÇÑ ±â´ÉÀ¸·Î½á Filter interface¸¦ »ç¿ëÇÒ¼ö ÀÖ´Ù.
Filter´Â Web Container¿¡ ÀÇÇØ Áö¿øµÇ¾î Áö´Â °ÍÀ¸·Î, RequestÀü,
ResponseÈÄÀÇ ¾î¶² ±â´ÉÀ» Ãß°¡ÇÒ¼ö ÀÖ´Ù. ´ÙÀ½Àº FilterÀÇ »ç¿ë¿¹ÀÌ´Ù.
1) Authentication Filters
2) Logging and Auditing Filters
3) Image conversion Filters
4) Data compression Filters
5) Encryption Filters
6) Tokenizing Filters
7) Filters that trigger resource access events
8) XSL/T filters
9) Mime-type chain Filter
Filter¸¦ °ü·ÃµÈ interface´Â ´ÙÀ½°ú °°´Ù.
Filter : ƯÁ¤ Resource¿¡ ´ëÇÑ Request³ª, Resource·Î ºÎÅÍÀÇ
response¿¡ Ãß°¡ÀûÀÎ Filtering ÀÛ¾÷À» ¼öÇàÇϱâ À§ÇÑ object¸¦ Á¤ÀÇ Çϱâ À§ÇÑ
interfaceÀÌ´Ù. Web containerÀÇ configuration¿¡ ÀÇÇØ Filter
class¿Í Resource°¡ ¼³Á¤µÈ´Ù. ÀÌ·¸°Ô ¼³Á¤µÇ¸é, ¼³Á¤µÈ resource¿¡ ´ëÇÑ
request´Â FilterÀÇ doFilter() ¸Þ¼Òµå°¡ ½ÇÇàÀÌ µÇ¾î Áø´Ù.
FilterConfig : Servlet container¿¡ ÀÇÇØ ¸¸µé¾î Áö´Â filter
Configuration objectÀÌ´Ù. ServletContext°´Ã¼¸¦ ¾ò¾î ³¾¼ö ÀÖ°í, web
Container¿¡ ¼³Á¤µÈ Á¤º¸(name, init parameter)µéÀ» ¾ò¾î ³¾¼ö ÀÖ´Ù.
FilterChain : FilterÀÇ doFilter() ¸Þ¼ÒµåÀÇ parameter·Î
Container¿¡ ÀÇÇØ »ý¼ºµÇ¾î ³Ñ¾î¿À´Â °´Ã¼·Î, chainÀ» Çü¼ºÇϱâ À§ÇØ, Áï Filter¸¦
½ÇÇàÇÏ°í ´ÙÀ½ Filter·Î dispatchÇϱâÀ§ÇØ Á¦°øµÇ´Â °´Ã¼ÀÌ´Ù. FilterÀÇ Á¦ÀÏ ³¡¿¡´Â
°á±¹ requestÇÑ resource¸¦ dispatchÇØ¾ß ÇÑ´Ù.
ÀÚ
±×·³.. Filter°¡ ½ÇÀç ¾î¶»°Ô µ¿ÀÛÀ» ÇÏ´ÂÁö ¾Ë¾Æ º¸±âÀ§ÇØ °£´ÜÇÑ ¼Ò½º¿Í ¼³Á¤¹æ¹ýÀ» »ìÆì
º¸°Ú´Ù.
package specular.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletFilter implements Filter
{
private FilterConfig filterConfig;
private ServletContext context;
//FilterConfig °´Ã¼¸¦ ÃʱâÈ ÇϱâÀ§ÇÑ callback method
//ÃʱâÈ Àâ¾÷À» ±¸Çö ÇÑ´Ù.
public void setFilterConfig(FilterConfig filterConfig)
{
System.out.println("ServletFilter.setFilterConfig()");
this.filterConfig = filterConfig;
this.context = filterConfig.getServletContext();
//init parameter ¾ò±â
System.out.println(filterConfig.getInitParameter("info"));
}
public FilterConfig getFilterConfig(){
return this.filterConfig;
}
//¼³Á¤µÈ Resource¿¡ ´ëÇÑ request½Ã ½ÇÇàµÈ´Ù.
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws java.io.IOException, ServletException
{
//Request½Ã °øÅë±â´É ±¸Çö
System.out.println("ServletFilter.doFilter() : start");
//½ÇÀç Reousrce¸¦ dispatch
chain.doFilter(request,response);
//Response½Ã °øÅë±â´É ±¸Çö
System.out.println("ServletFilter.doFilter() : end");
}
}
|
Å×½ºÆ® ȯ°æÀº Servlet 2.3 API¸¦ ±¸ÇöÇÑ Servlet/JSP container¸¦
ÀÌ¿ëÇØ¾ß Çϴµ¥, Àú´Â Resin2.0 beta¸¦ »ç¿ëÇß½À´Ï´Ù. ResinÀÇ doc\WEB-INF\
µð·ºÅ丮 ¹Ø¿¡ ´ÙÀ½°ú °°ÀÌ web.xml ÆÄÀÏÀº ¸¸µç´Ù.
<web-app>
<filter-mapping url-pattern='/*'
filter-name='specular.servlet.ServletFilter'>
<init-param info='Servlet Filter Class'/>
</filter-mapping>
</web-app> |
À§ÀÇ ¼³Á¤»çÇ×À» º¸¸é, document root ¹ØÀÇ ¸ðµç resource¿¡ ´ëÇÑ
request´Â specular.servlet.ServletFilter class¿¡ ÀÇÇØ µ¿À۵ȴٴÂ
°ÍÀ» ¼³Á¤Çß°í, ±×¸®°í, init parameter¸¦ Ãß°¡·Î Çϳª ¼³Á¤Çß´Ù. web
container¸¶´Ù ÀÌ ¼³Á¤»çÇ×Àº ¾à°£ÀÇ Â÷ÀÌ°¡ ÀÖ´Ù.
Å×½ºÆ® °á°ú´Â ´ÙÀ½°ú
°°´Ù.
1. resin start
D:\web\resin2.0\bin>httpd
Resin 2.0.b2 (built Tue Apr 17 09:41:58 PDT 2001)
Copyright(c) 1998-2001 Caucho Technology. All rights reserved.
Starting Resin on Mon, 09 Jul 2001 11:08:43 +0900 (GMT+09:00)
http listening to *:8080
srun listening to 127.0.0.1:6802
| 2. hello.jsp
<%
System.out.println("Hello Wrold");
%> | 3.
http://localhost:8080/hello.jsp RequestÈÄ console Ãâ·Â °á°ú
D:\web\resin2.0\bin>httpd
Resin 2.0.b2 (built Tue Apr 17 09:41:58 PDT 2001)
Copyright(c) 1998-2001 Caucho Technology. All rights reserved.
Starting Resin on Mon, 09 Jul 2001 11:22:19 +0900 (GMT+09:00)
http listening to *:8080
srun listening to 127.0.0.1:6802
ServletFilter.setFilterConfig()
Servlet Filter Class
ServletFilter.doFilter() : start
Hello Wrold
ServletFilter.doFilter() : end | ˤ
°á°ú¸¦ º¸¸é, hello.jsp¿¡ ´ëÇÑ request°¡ ServletFilter class¿¡ ÀÇÇØ
󸮰¡ µÇ´Â °ÍÀ» È®ÀÎÇÒ ¼ö ÀÖ´Ù. Filter¸¦ ÅëÇØ Web tier´ÜÀÇ °³¹ßµÈ Äڵ带 º¯°æÇÏÁö
¾Ê°í, ±â´ÉÀÇ Ãß°¡ ¹× º¯°æÀÌ °¡´ÉÇÏ°í, °øÅëÀûÀÎ ±â´ÉÀ» Filter¸¦ ÅëÇØ ÅëÇÕ °ü¸® ÇÒ¼öÀÖ°ÚÁÒ.
2001.07.09 written by
Jeon HongSeong |