如用Servlet的话,我倒是有一招,你自已在JSP页面上试试吧在doGet()或doPost()中写
1. request.setCharacterEncoding("GB2312") 这是Servlet从JSP读到数据后,再向数据库添加信息的中文支持
2. response.setContentType("text/html; charset=GB2312") 这是Servlet向JSP写数据时用的中文支持这样就可支持中文了
1. request.setCharacterEncoding("GB2312") 这是Servlet从JSP读到数据后,再向数据库添加信息的中文支持
2. response.setContentType("text/html; charset=GB2312") 这是Servlet向JSP写数据时用的中文支持这样就可支持中文了
解决方案 »
- No setter found for property 'sessionFactoroy' in class 'com.iBBS.dao.impl.UserI
- 在jsp中写分页问题
- 怎么用JSP关闭和开启Windows显示器
- rs.last()和jakarta poi?.............
- Eclipse下log4j的配置问题
- 关于struts的问题
- 装了oracle,tomcat却不好使了?
- 重新开帖问:如何让下拉框可以输入?
- 继续问个BC问题哈!怎么把script中的值不提交传给JSP??或者是页面中的值传给JSP?
- 关于我的Servlet的错误
- 谁有jawin?有兴趣的讨论一下
- 求Java 用户权限管理的最佳方案及实现思想!
{
public static String trans(String chi)
{
String result = null;
byte temp [];
try
{
temp=chi.getBytes("iso-8859-1");
result = new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println (e.toString());
}
return result;
}
} 在每次将数据写到数据库之前trans(result)一下,result是你要写的数据
String nameInstadOf = new String(name.getBytes("iso-8859-1"),"GBK");
此时所得到的nameInstadOf插入数据库后将可解决乱码的问题!
request.setCharacterEncoding("GBK")
就可以了
stm.setString(1,msg.getTitle());
stm.setString(2,msg.getName());
stm.setDate(3,new java.sql.Date(new java.util.Date().getTime()));
if((msg.getEmail()).length()==0)
stm.setString(5,null);
else stm.setString(5,msg.getEmail());
stm.setString(4,msg.getContent());
String title=result.getString("title");
改成
String title=trans(result.getString("title"));
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() { this.encoding = null;
this.filterConfig = null; }
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response); }
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 String selectEncoding(ServletRequest request) { return (this.encoding); }
将这个SetCharacterEncoding.java文件编译一下!并在web.xml文件中配置:
<filter> <filter-name>SetCharacterEncoding</filter-name>
<filter-class>Filters.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name>
<param-value>GB2312</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern> </filter-mapping>