Filter

์ฝœ๋ผ๋ง˜ยท2023๋…„ 6์›” 27์ผ
0

JSP

๋ชฉ๋ก ๋ณด๊ธฐ
14/17
post-thumbnail

๐Ÿ Filter

  • ํŠน์ • ์ฃผ์†Œ๋กœ ์ ‘์†ํ•  ๋•Œ ์ด ํ•„ํ„ฐ๋ฅผ ๊ฑฐ์ณ์„œ ์ง€๋‚˜๊ฐ€๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค
  • web.xml์—์„œ ํ•„ํ„ฐ ๋“ฑ๋ก์„ ํ•œ ํ›„ ํŠน์ • URL์— ๋Œ€ํ•œ ๋งคํ•‘์„ ํ•ด๋†“์„ ์ˆ˜ ์žˆ๋‹ค
  • chain์œผ๋กœ ๋‹ค์Œ ํ•„ํ„ฐ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด
  • javax.servlet.Filter๋ฅผ import ํ•ด์•ผํ•œ๋‹ค

๐Ÿคบ Filter ๊ตฌํ˜„ํ•˜๊ธฐ

public class MyFilter1 implements Filter {
	@Override
		public void init(FilterConfig filterConfig) throws ServletException {
			// ํ•„ํ„ฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ณณ
		}	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// ํ•„ํ„ฐ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ ... 
		chain.doFilter(request, response);// chain์œผ๋กœ ๋‹ค์Œ ํ•„ํ„ฐ๋กœ ์ฟผ๋ฆฌ ์ „๋‹ฌ
	}	
}
  • doFilter ๋ฉ”์„œ๋“œ๋Š” ์š”์ฒญ์ด ์žˆ์„ ๋•Œ ๋งˆ๋‹ค ๋งค๋ฒˆ ์‹คํ–‰๋œ๋‹ค
    ์ฐธ๊ณ 

๐Ÿคบ Filter ์„ค์ •ํ•˜๊ธฐ

  • web.xml์—์„œ ๋ชจ๋“  ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์— ๋Œ€ํ•ด(*/) ์ธ์ฝ”๋”ฉํ•„ํ„ฐ๋ฅผ ์ง€๋‚˜๊ฐ€๋„๋ก ์„ค์ •ํ•œ๋‹ค
  • ์ธ์ฝ”๋”ฉ ํ•„ํ„ฐ๋ฅผ ์ง€๋‚˜๊ฐ€๋Š” ๋ชจ๋“  request๋Š” characterEncoding์„ EUC-KR๋กœ ์„ธํŒ…ํ•˜๋„๋ก ์„ค์ •ํ•œ๋‹ค

โœ” web.xml >>

  <filter>
  	<filter-name>characterEncodingFilter</filter-name>
  	<filter-class>chap10.filter.CharacterEncodingFilter</filter-class>
  </filter>

  <filter-mapping>
  	<filter-name>characterEncodingFilter</filter-name>
    <!-- ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ -->
  	<servlet-name>/*</servlet-name> 
  </filter-mapping>

โœ” CharacterEncodingFilter.java >>

public class CharacterEncodingFilter implements Filter {
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("์ธ์ฝ”๋”ฉ ํ•„ํ„ฐ๋ฅผ ์ง€๋‚˜๊ฐ");
		
		// ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•œ ์ธ์ฝ”๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค
		request.setCharacterEncoding("EUC-KR");
		response.setCharacterEncoding("EUC-KR");
		
		chain.doFilter(request, response);
	}
}

๐Ÿ Listener

  • ํŠน์ • ์ด๋ฒคํŠธ(ํŠน์ • ์‚ฌ๊ฑด)๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋‹ค๊ฐ€ ์‹คํ–‰๋˜๋Š” ์ปดํฌ๋„ŒํŠธ(๋ฉ”์„œ๋“œ๋‚˜ ํ•จ์ˆ˜)
  • ContextLoaderListener : Context ๋‚ด๋ถ€์˜ ๋‚ด์šฉ์— ๋ญ”๊ฐ€ ๋ณ€ํ™”๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ReLoadํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ

๐Ÿคบ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋กํ•˜๊ธฐ

โœ” web.xml >>

 <listener>
  	<listener-class>chap10.listener.MyServletRequestProcess</listener-class>
 </listener>
 <listener>
  	<listener-class>chap10.listener.ContextLoaderListener</listener-class>
  </listener>

โœ” MyServletRequestProcess.java >>

public class MyServletRequestProcess implements ServletRequestListener {
	@Override
	public void requestInitialized(ServletRequestEvent sre) {
		// ์š”์ฒญ ์ดˆ๊ธฐํ™” ์‹œ์ 
		//์š”์ฒญ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ์„ ์ ๋Š”๋‹ค
		
		// sre ๊ฐ์ฒด์— ServletRequest์™€ ServletContext๊ฐ€ ๋ชจ๋‘ ๋“ค์–ด์žˆ๋‹ค
		System.out.println("ํŒŒ๋ผ๋ฏธํ„ฐ : " + sre.getServletRequest().getParameter("flavor"));
        // ํŒŒ๋ผ๋ฏธํ„ฐ ๊บผ๋‚ด์„œ ์ฐ์–ด๋ณด๊ธฐ
	}	
	@Override
	public void requestDestroyed(ServletRequestEvent sre) {		
		//์š”์ฒญ ๋งˆ๋ฌด๋ฆฌ ๋ถ€๋ถ„์— ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ์„ ์ ๋Š”๋‹ค
	}
}

โœ” ContextLoaderListener.java >>

public class ContextLoaderListener implements ServletContextListener{
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("ContextLoaderListener : ์ปจํ…์ŠคํŠธ ๋‚ด๋ถ€์— ๋ญ”๊ฐ€ ๋ณ€ํ™”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค ...");
		System.out.println("ContextLoaderListener : ๋‹ค์‹œ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค");
	}
		@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("ContextLoaderListener : ServletContext Destroyed ... ");
	}
}
profile
์ฝœ๋ผ๋ง˜ ์ฝ”๋”ฉ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€