JSPページエンコーディングフィルタについて(注記構成)
1396 ワード
srcの下にFilterフィルタを追加
主に2点です.
1つ目は注記の構成です:@WebFilter(filterName="encodingFilter"、urlPatterns="/*)
2つ目はdoFilterメソッドでフォーマットを指定することです(UTF-8の例).
request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");chain.doFilter(request, response);
package com.kevin.Filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
@WebFilter(filterName="encodingFilter",urlPatterns="/*")
public class EncodingFilter implements Filter {
String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy");
}
}
主に2点です.
1つ目は注記の構成です:@WebFilter(filterName="encodingFilter"、urlPatterns="/*)
2つ目はdoFilterメソッドでフォーマットを指定することです(UTF-8の例).
request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");chain.doFilter(request, response);