注册 登录
编程论坛 J2EE论坛

今天刚研究JSP乱码问题,按照大家总结的经验一步步的做,但是始终解决不了。

Grace_TT 发布于 2006-06-29 18:12, 1051 次点击
我是在MyEclipse+Eclipse+tomcat5.0 中写的程序,
在一个.jsp中添加了:<%@ page contentType="text/html; charset=GBK" pageEncoding="GBK"%>
和:<%request.setCharacterEncoding("GB2312");%>
然后我保存文件,在保存的过程中,MyEclipse就会询问我:The encoding (ISO-8859-1)cannot convert some characters(such as the one in position 567).Press 'OK' to save anyway(and some characters will be convert '?' in the saved file),or press 'cansel' to return to the editor.

我只能点击OK。然后部署它,可是这个.jsp文件在Tomcat下里面的中文全都是乱码了,我用IE执行它,永远都显示的是乱码。

这要怎么做呢,难道你们都不用MyEclipse+Eclipse+tomcat5.0来编写.jsp吗?
12 回复
#2
可可℃乐2006-06-30 09:34
在写入数据库之前对来自表单的信息做字符集的转换。
String wordName=new String(request.getParameter("wordName").getBytes("iso-8859-1"),"gb2312") ;
你试试这样行不行。
#3
Grace_TT2006-06-30 09:47
谢谢你呀,
不过要一个一个的实现呀,那不累死了!
#4
xiao_20082006-07-05 21:29
加个过滤器
#5
神vLinux飘飘2006-07-05 23:17
自从有了过滤器,我还真没把数据库的乱码当一回事
#6
水影月圆2006-07-08 17:10
什么过滤器
乱码问题怎么解决?每次显示都是????? 难看死了!
#7
孤雁飘雪2006-07-08 21:04
<%@page language="java" contentType="text/html;charset=GB2312"%>

直接写这个就可以了!!!
#8
神vLinux飘飘2006-07-08 22:19
过滤器代码
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;
import javax.servlet.UnavailableException;

public class SetEncodingFilter
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)) {
request.setCharacterEncoding(selectEncoding(request));
}
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") || value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}


protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}

public FilterConfig getFilterConfig() {
return filterConfig;
}

public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

}



web.xml关于过滤器的配置
<filter>
<filter-name>SetEncodingFilter</filter-name>
<filter-class>enova.util.SetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>

[此贴子已经被作者于2006-7-8 22:22:59编辑过]

#9
gady1002006-07-10 17:16
,学习中啊
#10
球球2006-07-11 13:37
加入:
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
试试,我这样可以。
#11
孤雁飘雪2006-07-13 15:49

还可以编写一个类函数
public class ISOtoGb2312
{
public static String convert(String str)
{
try
{
byte[] bytesStr=str.getBytes("ISO-8859-1");
return new String(bytesStr,"gb2312");
}
catch(Exception ex)
{
return str;
}
}
}
然后在jsp页面里面调用这个类函数就可以了!!

#12
Grace_TT2006-07-24 20:29
以下是引用神vLinux飘飘在2006-7-8 22:19:27的发言:
过滤器代码
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;
import javax.servlet.UnavailableException;

public class SetEncodingFilter
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)) {
request.setCharacterEncoding(selectEncoding(request));
}
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") || value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}


protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}

public FilterConfig getFilterConfig() {
return filterConfig;
}

public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

}



web.xml关于过滤器的配置
<filter>
<filter-name>SetEncodingFilter</filter-name>
<filter-class>enova.util.SetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>



dispatcher 起了什么作用?

过滤器只能解决数据库的乱码呢,还是所有的乱码都能解决?

[此贴子已经被作者于2006-7-24 20:30:59编辑过]

#13
神vLinux飘飘2006-07-24 20:35
所有乱码一次性解决,因为乱码的根源在于WEB向应用程序传递中文数据的时候会产生乱码导致的。MYSQL的乱码可以通过连接MYSQL的URL解决,最好统一用UTF-8
1