Dùng bộ lọc request cho tất cả các trang jsp và servlet
Dùng bộ lọc request cho tất cả các trang jsp và servlet.
==============================
CharacterEncodingFilter.java
==============================
public class CharacterEncodingFilter implements Filter {
private FilterConfig fc;
/** Creates a new instance of CharacterEncodingFilter */
public CharacterEncodingFilter() {
}
public void init(FilterConfig filterConfig) throws ServletException {
this.fc = filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");
chain.doFilter(request, response);
//do it again, since JSPs will set it to the default
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");
}
public void destroy() {
this.fc = null;
}
}
==============================
web.xml
sau đó sẽ khai báo dùng trong các form cần, tôi khai báo dùng cho /servlet/
==============================
...
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
...
0 Responses to "Dùng bộ lọc request cho tất cả các trang jsp và servlet"
Đăng nhận xét