Skip to content

Instantly share code, notes, and snippets.

@ezhov-da
Last active March 10, 2019 12:20
Show Gist options
  • Save ezhov-da/50e5d5a2ce2e591068f06f2f2b1fdd7e to your computer and use it in GitHub Desktop.
Save ezhov-da/50e5d5a2ce2e591068f06f2f2b1fdd7e to your computer and use it in GitHub Desktop.
java фильтр для сервлета
http://www.skipy.ru/technics/encodings_webapp.html
[code:]java[:code]
import java.io.*;
import javax.servlet.*;
/**
* FormEncodingSetterFilter
*
* @author Eugene Matyushkin
*/
public class FormEncodingSetterFilter implements Filter{
private static final String FILTERABLE_CONTENT_TYPE="application/x-www-form-urlencoded";
private static final String ENCODING_DEFAULT = "UTF-8";
private static final String ENCODING_INIT_PARAM_NAME = "encoding";
private String encoding;
public void destroy(){
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws ServletException, IOException{
String contentType = req.getContentType();
if (contentType != null && contentType.startsWith(FILTERABLE_CONTENT_TYPE))
req.setCharacterEncoding(encoding);
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException{
encoding = config.getInitParameter(ENCODING_INIT_PARAM_NAME);
if (encoding == null)
encoding = ENCODING_DEFAULT;
}
}
[/code]
[code:]xml[:code]
<filter>
<filter-name>FormEncodingSetterFilter</filter-name>
<filter-class>ru.skipy.juga_ru.web.i18n_tutorial.FormEncodingSetterFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FormEncodingSetterFilter</filter-name>
<url-pattern>/i18n-tutorial/*</url-pattern>
</filter-mapping>
[/code]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment