本人采用JSP+Struts开发一软件,在输入界面中如果输入中文“——”符号保存后,数据库保证正确,且查询结果显示正确,唯独单击直接在界面中不做任何修改再次保存后,Action接收到该项内容为“?D?D”,保存到数据库数据格式也是此类错误数据。目前只发现该符号有问题,其他中文字没问题。插图附件!
解决方案 »
- java集成google checkout 求大虾们帮帮忙?
- 一个表可以没有主键么?
- 如何在JAVA中利用元数据验证从Sql server中获得的数据并根据元数据生成XML
- JSP里面调用一个用VC写的控件的问题
- 完咧,这次真的碰到鬼咧,隔一天代码就出错了,谁好心救救我吧。
- 请用过jetty的高手帮忙解释一下。
- 古老的错误:jsp调用sqlserver,在线等
- 实现自定义报表,求推荐一种技术
- 请问,如何在页面打开前显示“此叶面正在加载...”这样的话
- 一个关于JRUN STUDIO DEBUG 的火急问题??
- 关于java.lang.NumberFormatException: null
- 求助一个正则表达示
http://blog.csdn.net/lip009/archive/2006/09/07/1192022.aspx
public class EncodingFilter implements Filter { public void destroy() {
// TODO Auto-generated method stub
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}}
然后在XML里面配置就好了
JSP页面传过来的值默认编码是ISO-8859-1
package com.common;import javax.servlet.*;
import java.io.IOException;public class SetCharacterEncodingFilter implements Filter { // ----------------------------------------------------- Instance Variables /**
* 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; // --------------------------------------------------------- Public Methods /**
* 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 { // Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
} // Pass control on to the next filter
chain.doFilter(request, response); } /**
* Place this filter into service.
*
* @param filterConfig
* The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false; } // ------------------------------------------------------ Protected Methods /**
* 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); }}// EOC*******************web.xml****************************<filter-name>Set Character Encoding</filter-name>
<filter-class>com.common.SetCharacterEncodingFilter</filter-class><init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param><init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param></filter><filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<%=java.net.URLEncoder.encode(str,"UTF-8")%>
那就是GB2312字符集编码问题了,GB2312字符集只包含7000左右个常用中文字符
这个时候就只能用GBK编码了,GBK包含常用的20000个中文字符.
呵呵,我当时也是这犯了毛病,结果弄了一天才搞明白