자바 필터, post 난 장 판 문제 해결

2543 단어
웹. xml 에 코드 추가

    
        SpringEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    

CharacterEncoding Filter 의 코드
public class CharacterEncodingFilter extends OncePerRequestFilter {

    private String encoding;

    private boolean forceEncoding = false;


    /**
     * Set the encoding to use for requests. This encoding will be passed into a
     * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
     * 

Whether this encoding will override existing request encodings * (and whether it will be applied as default response encoding as well) * depends on the {@link #setForceEncoding "forceEncoding"} flag. */ public void setEncoding(String encoding) { this.encoding = encoding; } /** * Set whether the configured {@link #setEncoding encoding} of this filter * is supposed to override existing request and response encodings. *

Default is "false", i.e. do not modify the encoding if * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()} * returns a non-null value. Switch this to "true" to enforce the specified * encoding in any case, applying it as default response encoding as well. *

Note that the response encoding will only be set on Servlet 2.4+ * containers, since Servlet 2.3 did not provide a facility for setting * a default response encoding. */ public void setForceEncoding(boolean forceEncoding) { this.forceEncoding = forceEncoding; } @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding) { response.setCharacterEncoding(this.encoding); } } filterChain.doFilter(request, response); } }

좋은 웹페이지 즐겨찾기