str=new String(str.getBytes("ISO8859_1"));
上面两种方法不能混用,意思就是要么就是GBK,要么就是ISO8859_1,从里到外都一样就好了。数据库JDBC的中文问题,一般只要按照数据库指定的编码进行转换,比如按照ISO8859_1读,ISO8859_1写
上面两种方法不能混用,意思就是要么就是GBK,要么就是ISO8859_1,从里到外都一样就好了。数据库JDBC的中文问题,一般只要按照数据库指定的编码进行转换,比如按照ISO8859_1读,ISO8859_1写
解决方案 »
- 求解。
- 概率问题:70%的概率执行情况A,30%的情况执行B
- 这什么意思???问题在那儿
- 初次使用无状态sessionbean的问题
- tomcat 5.0.28+hibernate2 问题,都一个月了,再解决不了明天就下岗,各位好心人,帮帮忙吧。
- tag file中的dynamic-attributes怎么使用?
- 急 急 急 使用java中的什么控件可以完成图形输入功能——java构件高手请进,
- 关于<<J2EE构建企业级应用解决方案>>中的一个例子
- 折磨了一天,无法解决的java.lang.OutOfMemoryError: PermGen space
- spring 3.0 和spring3.2的定时触发器包怎么不一样了
- 100分初级问题,在jbuilder如何建立一个工程,开发一个jsp tomcat
- 一个网友对《Java 2核心技术(第6版)》的评价
编码从web.xml配置里读取import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class EncodingFilter
implements Filter { private FilterConfig config = null;
// default to ASCII
private String targetEncoding = "ASCII"; public void init(FilterConfig config) throws ServletException {
this.config = config;
this.targetEncoding = config.getInitParameter("encoding");
} public void destroy() {
config = null;
targetEncoding = null;
} public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) srequest;
request.setCharacterEncoding(targetEncoding);
// move on to the next
chain.doFilter(srequest, sresponse);
}}
加到<web-app>标签里面
<filter>
<filter-name>EncodingFilter</filter-name>
<display-name>EncodingFilter</display-name>
<description>Encoding Filter for Chance OA</description>
<filter-class>com.dmk.chance.common.system.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>loginURL</param-name>
<param-value>/oa/system/login/login.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>EnclodingFilter.java文件
package com.dmk.chance.common.system;/**
* <p>Title: JSP中文编码过滤器</p>
* <p>Description: 1.JSP中文显示乱码的解决。统一编码过滤
* 2.登录过滤</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author billyw
* @version 1.0
*
* History : Mon Apr 12 09:08:30 2004 wxy
*
*/import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class EncodingFilter
implements Filter { private FilterConfig config = null;
// default to GBK
private String targetEncoding = "GBK";
private String loginURL=""; public void init(FilterConfig config) throws ServletException {
this.config = config;
this.targetEncoding = config.getInitParameter("encoding");
this.loginURL=config.getInitParameter("loginURL");
} public void destroy() {
config = null;
targetEncoding = null;
} public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) srequest;
HttpServletResponse response=(HttpServletResponse) sresponse;
request.setCharacterEncoding(targetEncoding);
String rURL=request.getRequestURI();
if(rURL.toLowerCase().indexOf("login")==-1 && request.getSession().getAttribute("UserBean")==null){
response.sendRedirect(request.getContextPath()+loginURL);
return;
}
chain.doFilter(srequest, sresponse);
}
}
public String[][] getTableData(String strSQL)
{
String[][] result=null;
ResultSet rs=null;
ResultSetMetaData rsmd=null;
int columnCount,rowCount=0;
try{
rs = getResultSet(strSQL,true);
//得到结果集有多少行和多少列
rs.last();
rowCount =rs.getRow();
if(rowCount==0) { return result; }
rsmd = rs.getMetaData();
columnCount = rsmd.getColumnCount();
result = new String[rowCount][columnCount];
//填充返回值数组
String strTmp;
boolean flag = rs.first();
while(flag)
{
for(int i=0 ; i < result.length ; i++)
{
for(int j=1; j <= result[0].length ; j++)
{
strTmp = rs.getString(j);
strTmp = new String(strTmp.getBytes("ISO8859_1"),"gb2312");
if(strTmp==null){result[i][j-1] = "";}
else{ result[i][j-1] = strTmp.trim();}
}
flag = rs.next();
}
}
}
catch(Exception e){}
finally{
try{
if(rs!=null) { rs.close();}
closeConnection();
}
catch(Exception e){}
}
return result;
}
<%if (TZdata != null){
for(int k=0 ; k < TZdata.length ; k++){%>
<a href="javascript:openwindow('gsdt/tz_view.jsp?id=<%=TZdata[k][0]%>')" title="发布日期:<%=TZdata[k][2].substring(0,10)%>"> <%=new String(TZdata[k][1].getBytes("ISO8859_1"),"gb2312")%></a>
<%out.println("<br><br>");}}%>
但结果不是问号了,是@@之类的乱码了
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html; charset=gb2312</mime-type>
</mime-mapping>
<mime-mapping>
<extension>html</extension>
<mime-type>text/html; charset=gb2312</mime-type>
</mime-mapping>
这个就行了
小蜜蜂的,可是,由于编码习惯不同我经常使用:
str = new String(str.getBytes("8859_1"),"gb2312");
还要提醒搂主,千万不要对从数据中提取的值直接进行转换,从数据库中取出的值,只要暂时不显示就不要转换,应该在最前端进行转换,也就是jsp页面要显示的地方,这点与楼上的某位仁兄比较接近你出现的??变成了更乱的乱码可能是因为你进行多次转换造成的后果,这种情况我以前经常遇到。能力有限,最好能够帮到你。