π₯ [Spring] Filter μ μ λ°μ μΈ ν΅μ νλ¦
Preview
CookieλSessionμ΄ λ§λ€μ΄μ§ λ μλμΌλ‘ μμ±μ΄ λλ€.
SessionμClientκ° μμ²ν λ λ§λ€μ΄μ§λ€.
Cookieλ λ§΅ λ°©μμΌλ‘ μ μ₯μ΄ λλ©°valueλStringμΌλ‘ λ§λ€μ΄μ§κ² λμ΄μλ€.μ΄λ μλμΌλ‘ λ§λ€μ΄μ§λ
CookieμλJSESSIONIDλΌλκ² μ‘΄μ¬νλλ°λ°μ΄ν° (VO β¦) λ
Sessionμ μ μ₯νκ³ μ¬μ©μλ₯Ό ꡬλΆνλJSESSIONIDλ μΏ ν€μ μ μ₯νλ€.
μ 체μ μΈ νλ¦
- 컨ν
μ΄λλ μ²μμ μ£Όλ¬Έμ(DD)λ₯Ό μ½μ΄λ€μ΄λ©° (web.xml) μ¬κΈ°μλ
Servletκ³Ό κ΄λ ¨λ λ§΅ν μ λ³΄κ° λ΄κ²¨μλ€. - κ·Έ ν
Servletμ΄ λ¨Όμ λ§λ€μ΄μ§λκ² μλλΌServletContextκ° λ¨Όμ μμ±λλ€. - κ·Έ λ€μ
Servletμ΄ λ§λ€μ΄μ§λ€. (μμ±μ νΈμΆ) 0μ λ§λ€κ³init()μ νΈμΆ,0μ μΈμκ°μΌλ‘ λ£λλ€.μ¬κΈ°κΉμ§
Ready onμνμ΄λ€. (Clientκ° μμ²νκΈ° μ μλ² μ€λΉ μνμ΄λ€.)Clientκ° μμ²νλ©΄request,responseκ°μ²΄κ° λ§λ€μ΄μ§λ€. λνSessionμ΄ λ§λ€μ΄μ§κ³JSESSIONIDκ°λ§ μ μ₯ν μΏ ν€λ λ§λ€μ΄μ§λ€.- κ·Έ λ€μ
Service()κ° νΈμΆλκ³ ,doGet(),doPost()κ° νΈμΆλλ€. μ΄ λrequest,responseκ° μΈμκ°μΌλ‘ λ€μ΄κ°λ€. - μμ²μ μννλ©΄(μλ΅νλ©΄)
req,res,threadκ° μ£½λλ€. μμ²ν λ λ§λ€ λ°λ³΅λλ€.
Filter
μλ²λ‘ μμ²μ 보λ΄κ³ μλ΅μ ν λ 곡ν΅μ μΌλ‘ μ²λ¦¬(μλ°©ν₯ μ²λ¦¬)ν λΆλΆμ κ΄ν λ‘μ§μ filter λ‘ μ²λ¦¬νλ€.
μλ₯Ό λ€μ΄ νκΈ μ²λ¦¬ λ±β¦
νκΈ μ²λ¦¬λ Client μμ Server λ‘ μμ²ν λ , Server μμ Client λ‘ μλ΅ν λ λͺ¨λ μ²λ¦¬ν΄μ€μΌ νλ€.
λ°λΌμ Controller μμ μ²λ¦¬ν΄μ£Όλ κ²μ΄ μλλΌ Filter μμ ν΄μ£Όλ κ²μ΄ μ’λ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class EncodingFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
//곡ν΅μ μΌλ‘ μ²λ¦¬ν΄μΌ ν λ‘μ§... λͺ¨λ μλΈλ¦Ώ μμ
//νκΈμ²λ¦¬
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//μλ²μμμ λ€μ μ»΄ν¬λνΈ νλ‘κ·Έλ¨μ κ³μμ μΌλ‘ νν°λ§ν κ³΅ν΅ λ‘μ§μ΄ μ μ©λλ€.
//μ΄ λΆλΆμ λΉ λ¨λ¦¬λ©΄ 곡ν΅λ‘μ§μ νν°λ§μ μ μλ§ νκΈ° λλ¬Έμ μ€μ§μ μΌλ‘ μ²λ¦¬λμ§ μλλ€. λ°λμ λ£μ΄μΌνλ€.
chain.doFilter(request, response);
}

