Struts中解决汉字编码问题
来源:互联网 发布:mac ps字体怎么安装 编辑:程序博客网 时间:2024/05/29 01:56
1.修改web.xml文件,在<web-app>标签后增加如下内容.
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>xx.xx.SetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.创建类文件(SetEncodingFilter.java)
package xx.xx; import java.io.IOException; import javax.servlet.Filter; /** /** /** /** /** /** this.filterConfig = filterConfig; /** /** /**
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
* Set All HttpRequest Encoding
*
*/
public class SetEncodingFilter implements Filter {
/**
* The default character encoding to set for requests that pass through this
* filter.
*/
protected String encoding = null;
* The filter configuration object we are associated with. If this value is
* null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request
* The servlet request we are processing
* @param result
* The servlet response we are creating
* @param chain
* The filter chain we are processing
*
* @exception IOException
* if an input/output error occurs
* @exception ServletException
* if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(selectEncoding(request));
}
chain.doFilter(request, response);
}
* Place this filter into service.
*
* @param filterConfig
* The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true")
|| value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>. <p> The default implementation unconditionally returns
* the value configured by the <strong>encoding</strong> initialization
* parameter for this filter.
*
* @param request
* The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
* Returns the filterConfig.
*
* @return FilterConfig
*/
public FilterConfig getFilterConfig() {
return filterConfig;
}
* Sets the filterConfig.
*
* @param filterConfig
* The filterConfig to set
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
- Struts中解决汉字编码问题
- Struts+Hibernate中解决汉字编码问题
- Struts+Hibernate中解决汉字编码
- 关于在struts 框架中使用Filter过滤器解决汉字编码问题
- 关于在struts 框架中使用Filter过滤器解决汉字编码问题
- 关于在struts 框架中使用Filter过滤器解决汉字编码问题
- Struts+Hibernate模板开发笔记---解决汉字编码问题
- 关于Servlet和JNI中汉字编码问题的解决
- URL汉字编码问题(及乱码解决)
- URL汉字编码问题(及乱码解决)
- URL汉字编码问题(及乱码解决)
- URL汉字编码问题(及乱码解决)
- 汉字编码问题
- 汉字编码问题
- 汉字编码问题
- 汉字编码问题
- 汉字编码问题
- 汉字编码问题
- jboss配置 mysql数据库连接池
- Tomcat 的数据库连接池设置与应用(Mysql篇)(1)
- Tomcat 的数据库连接池设置与应用(Mysql篇)(2)
- 针对事务型数据库设计小结
- 熊过留印---爱心篇
- Struts中解决汉字编码问题
- 经济学小知识
- php4和5的一个区别
- 夜好长
- VS2005运行程序时出现“没有找到MSVCP80D.dll"错误的解决方法
- 在CSDN的第一篇Blog
- java如何存取image字段
- Oracle10g的AWR性能优化工具介绍
- powerDesigner(10)使用解疑一例.在check model后,出现警告!