虚拟主机网站建设步骤?,怎么在网上做推广,做食品的采购员常用网站,好网站建设公司地址我今天花了很多时间来弄清楚如何在运行在JBoss上的JSF应用程序中#xff08;使用JBoss 7 Final#xff09;强制正确解码编码的字符。 当您有例如通过URL传递中文字符时#xff0c;就会出现此问题。 假设您有指点事件#xff0c;编码为#xff05;E6#xff05;8C#xf… 我今天花了很多时间来弄清楚如何在运行在JBoss上的JSF应用程序中使用JBoss 7 Final强制正确解码编码的字符。 当您有例如通过URL传递中文字符时就会出现此问题。 假设您有指点事件编码为E68C87E4BA8B。 令人惊讶但是这些字符以指事的形式到达服务器端。 服务器使用ISO-8859-1自动解码它们。 因此如果您尝试像这样自己解码则没关系 FacesContext fc FacesContext.getCurrentInstance();
String param fc.getExternalContext().getRequestParameterMap().get(name);
String decodedParam java.net.URLDecoder.decode(param, UTF-8); 这无济于事因为字符已经被错误地解码并且您从请求参数映射中获得了已经被错误解码的字符。 如果您在页面上也没有帮助 meta http-equivContent-Type contenttext/html; charsetUTF-8/ 要克服此错误您需要做两件事特殊的字符编码过滤器和JBoss的standalone.xml中的配置。 过滤器应同时为请求和响应设置配置的编码。 public class CharacterEncodingFilter implements Filter {/** The default character encoding to set for request / response. */private String encoding null;/** The filter configuration object. */private FilterConfig filterConfig;/** Should a character encoding specified by the client be ignored? */private boolean ignore true;public void destroy() {encoding null;filterConfig null;}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException {// conditionally select and set the character encoding to be usedif ((ignore || (request.getCharacterEncoding() null)) (encoding ! null)) {request.setCharacterEncoding(encoding);response.setCharacterEncoding(encoding);}// pass control on to the next filterchain.doFilter(request, response);}public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig filterConfig;this.encoding filterConfig.getInitParameter(encoding);String value filterConfig.getInitParameter(ignore);this.ignore ((value null) || value.equalsIgnoreCase(true) || value.equalsIgnoreCase(yes));}
} 注意如果仅设置请求的编码则无济于事。 您还应该通过response.setCharacterEncodingencoding将其设置为响应。 web.xml中的配置看起来像 filterfilter-nameCharacterEncodingFilter/filter-namefilter-classxyz.mypackage.CharacterEncodingFilter/filter-classinit-paramdescriptionoverride any encodings from client/descriptionparam-nameignore/param-nameparam-valuetrue/param-value/init-paraminit-paramdescriptionthe encoding to use/descriptionparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-param
/filter
filter-mappingfilter-nameCharacterEncodingFilter/filter-nameurl-pattern*.jsf/url-pattern
/filter-mapping 现在您必须在关闭extensions标记之后直接将以下系统属性添加到standalone.xml中 system-propertiesproperty nameorg.apache.catalina.connector.URI_ENCODING valueUTF-8/property nameorg.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING valuetrue/
/system-properties 从文档中 org.apache.catalina.connector.URI_ENCODING指定xx解码URL后用于解码URI字节的字符编码。 如果未指定将使用ISO-8859-1。 org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING指定是否应将contentType中指定的编码用于URI查询参数而不是使用org.apache.catalina.connector.URI_ENCODING。 出现此设置是为了与Tomcat 4.1.x兼容其中在contentType中指定的编码或使用Request.setCharacterEncoding方法显式设置的编码也用于URL中的参数。 默认值为false。 现在JBoss看起来为响应设置了字符编码并使用它来解码URL参数。 希望此信息可以帮助您节省时间。 参考 在JBoss中 我们的JCG合作伙伴 Oleg Varaksin在软件开发博客Thoughts上 对服务器端URL参数的正确解码 。 翻译自: https://www.javacodegeeks.com/2013/07/proper-decoding-of-url-parameters-on-the-server-side-in-jboss.html