把这句String temp=new String(temp_t);
改成这样试试看String temp=new String(tem_t,"gb2312");
改成这样试试看String temp=new String(tem_t,"gb2312");
解决方案 »
- ssh,如果访问的action不存在,就跳转到的指定页面index.jsp
- 请问java 打印 如何设置页边距
- 求Apache POI的HWPF jar包
- 关于在JSP中嵌套javascript的问题
- 急招聘IT人员--工作地点杭州
- 相当简单的一个问题只是进来一下`
- 请教一个关于servlet 中out.flush无效的问题
- 狂狂狂狂推荐史上最好的与JBUILDER结合的JSP IDE:http://www.m7.com/products/enterprise.html
- jsp可以使用MS SQL数据库吗????
- com.github.tobato.fastdfs.exception.FdfsServerException: 错误码:2,错误信息:找不到节点或文件
- 表单问题,高手帮忙,再线等????
- 在jsp里怎么使用一个自己写的类???
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t,"gb2312");
return temp;
sign="欲下未下风悠扬,影落寒潭三两行";
goodfriend="新蓝";
象这种写死在jsp里面的中文在运行的时候是unicode编码的字符流。byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
你就把unicode编码的字符流用ISO-8859-1来解码(decode)成字节流,然后用gbk编码(encode)称字符流。(如果你是中文操作系统的话,默认charset就是gbk)。所以就会出现乱码。你的代码中只有newfriend需要用这个函数来转换,因为他是ISO-8859-1编码的字符流。
写一个过滤器:如SetCharacterEncodingFilter.java
package 所在包;
import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
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); }
}写完以后,在web.xml里面加上
加在<web-app>下面
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>包名.SetCharacterEncodingFilter</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>
就这么两步,如果你用转换方法来做的话.可能操作数据库的过程中还会出现乱码.这个方法不会出现.我也是用这个方法.
<%!
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t,“gbk”);
return temp;
}
catch(Exception e)
{
//to do nothing
}
return "null";
}
%>
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
//to do nothing
}
return "null";
}
这个函数的作用就是转化类型的:)