
1在文件删右键--》properties--》Resource--》Text file encoding--》other--》选择utf-8--》OK
2在JSP页面最上端加上:
<%@ page language="java" import="javautil" pageEncoding="gbk"%>
以下,是中文乱码的各种解决方法,要记住了,很有用,肯定有一种方法能帮你解决的。希望能帮到你。
URIENcoding 设成GBK或GB2312
2. 表单中或传递字符串:本来输入的汉字是正常的,但是提交后再显示出来是乱码,因为提交的一般是 ISO8859编码,所以显示的时候要转成GB2312编码:
String S=new String(rsgetString("news")getBytes("gb2312"),"ISO8859_1");
//rsgetString("news")为待转换的字符串
然后使用S字符串的值就可以了
3. 有的服务器端的语言环境如果设成简体中文的也可以解决此类问题
4. 插入数据库中的字符是乱码
看看数据库中支持的是何种编码方式,用类似2中的方式作一下转换即可。
5. 总之,用jsp开发,碰到乱码,你得分析是读的时候发生乱码,还是写的时候发生乱码,用2中的转换,基本就能解决问题,有些时候写的时候做一次转换,例如:
String S=new String(rsgetString("news")getBytes("gb2312"),"ISO8859_1");
//读的时候在转换回来
String S=new String(rsgetString("news")getBytes("ISO8859_1"),"GB2312");
或者把ISO8859-1和GB2312 的位置换一下,自己多试试,就能找到解决问题的办法。
将乱码问题分为三类JSP页面显示中文乱码;表单提交乱码;数据库应用乱码
1) JSP页面内输出中文时出现乱码
解决方案在JSP文件中使用page命令指定响应结果的MIME类型,如<%@ page language="java" contentType="text/html;charset=gb2312" %>
2)表单提交乱码
表单提交时(post和Get方法),使用requestgetParameter方法得到乱码,这是因为tomcat处理提交的参数时默认的是iso-8859-1,表单提交get和post处理乱码问题不同,下面分别说明。
(1)POST处理
对post提交的表单通过编写一个过滤器的方法来解决,过滤器在用户提交的数据被处理之前被调用,可以在这里改变参数的编码方式,过滤器的代码如下:
package cngovbeijingitutil;
import javaioIOException;
import javaxservletFilter;
import javaxservletFilterChain;
import javaxservletFilterConfig;
import javaxservletServletException;
import javaxservletServletRequest;
import javaxservletServletResponse;
public class SetCharacterEncodingFilter implements Filter {
/
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() {
thisencoding = null;
thisfilterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (requestgetCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
requestsetCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chaindoFilter(request, response);
}
/
Place this filter into service
@param filterConfig
The filter configuration object
/
public void init(FilterConfig filterConfig) throws ServletException {
thisfilterConfig = filterConfig;
thisencoding = filterConfiggetInitParameter("encoding");
String value = filterConfiggetInitParameter("ignore");
if (value == null) {
thisignore = true;
} else if (valueequalsIgnoreCase("true")) {
thisignore = true;
} else if (valueequalsIgnoreCase("yes")) {
thisignore = true;
} else {
thisignore = false;
}
}
protected String selectEncoding(ServletRequest request) {
return (thisencoding);
}
}
webxml文件加入过滤器
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
cngovbeijingitutilSetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
<!--gbk或者gb2312或者utf-8-->
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<servlet-name>/</servlet-name>
</filter-mapping>
注意filter元素要放在所有webxml元素之前。
(2) Get方法的处理
tomcat对post和get的处理方法不一样,所以过滤器不能解决get的乱码问题,它需要在其他地方设置。
打开<tomcat_home>\conf目录下serverxml文件,找到对8080端口进行服务的Connector组件的设置部分,给这个组件添加一个属性:URIEncoding="GBK"。修改后的Connector设置为:
<Connector port="8080" max>
假如说
String
str
=
你得到的中文;
你可以处理一下
str
=
new
String(strgetBytes(),
"utf-8");
或者
str
=
new
String(strgetBytes("iso-8859-1"),
"utf-8");或者
str
=
new
String(strgetBytes(),
"gb2312");或者
你自己写个过滤器
汉字编码问题。以下是我从网上搜索到的相关汉字编码问题的解决方案,希望能帮到你。1 表单提交的数据,用requestgetParameter(“xxx”)返回的字符串为乱码或者??
2 直接通过url如>
大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着您,我现在把我在JSP开发中遇到的中文乱码的问题及解决办法写出来供大家参考。
一、JSP页面显示乱码
下面的显示页面(displayjsp)就出现乱码:<html<head<titleJSP的中文处理</title
<meta >
以上就是关于jsp中汉字乱码全部的内容,包括:jsp中汉字乱码、jsp高手请进,我用jspsmartupload上传文件,但我表单获得的中文是乱码(UTF-8)的,如何解决、servlet通过表单方式获取jsp页面的中文字符为乱码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)