SP页面提交后中文字符出现乱码-
窗体底端
用户名: 密 码:
加入收藏 飞诺网 提交
Javascript系列教程 Javascript实例 Javascript技术文档
您当前的位置:飞诺网 >> 网页设计 >> Javascript >> Javascript技术文档
jsp页面提交中文乱码的几种解决方法集锦
www.diybl.com 时间 : 2010-11-16 作者:佚名 编辑:fnw 点击: 1366 [ 评论 ]
JSP页面提交后中文字符出现乱码-tomcat下中文乱码问题
JSP页面提交后中文字符出现乱码-tomcat下中文乱码问题项目和页面都已设成了UTF-8编码形式; 解决方法:1) 设定请求的字符编码request.setCharacterEncoding("UTF-8");
2) 指定过滤类
这些天开发一个项目,服务器是tomcat,操作系统是xp,采用的是MVC架构,模式是采用 Facade模式,总是出现乱码,自己也解决了好多天,同事也帮忙解决,也参考了网上众多网友的文章和意见,总算是搞定。但是好记性不如烂笔杆,所以特意记下,以防止自己遗忘,同时也给那些遇到同样问题的人提供一个好的参考途径:
(一) JSP页面上是中文,但是看的是后是乱码:
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
解决的办法就是在JSP页面的编码的地方
<%@ page language="java" contentType="text/html;charset=GBK" %>,因为Jsp转成Java文件时的编码问题,默认的话有的服务器是ISO-8859-1,如果一个JSP中直接输入了中文,Jsp把它当作 ISO8859-1来处理是肯定有问题的,这一点,我们可以通过查看Jasper所生成的Java中间文件来确认
(二) 当用Request对象获取客户提交的汉字代码的时候,会出现乱码: 解决的办法是:要配置一个filter,也就是一个Servelet的过滤器,代码如下: 显示代码
打印
01 import java.io.IOException;
02 import javax.servlet.Filter;
03 import javax.servlet.FilterChain;
04 import javax.servlet.FilterConfig;
05 import javax.servlet.ServletException;
06 import javax.servlet.ServletRequest;
07 import javax.servlet.ServletResponse;
08 import javax.servlet.UnavailableException;
09
10 /**
* Example filter that sets the character encoding to be used in parsing 11 the
12 * incoming request
13 */
14 public class SetCharacterEncodingFilter implements Filter { 15
16 /**
17 * Take this filter out of service.
18 */
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
19 public void destroy() {
20 }
21 /**
22 * Select and set (if specified) the character encoding to be used to
23 * interpret request parameters for this request. 24 */
public void doFilter(ServletRequest request, ServletResponse 25 response,
26 FilterChain chain)throws IOException, ServletException {
27
28 request.setCharacterEncoding("GBK");
29
30 // 传递控制到下一个过滤器
31 chain.doFilter(request, response);
32 }
33
public void init(FilterConfig filterConfig) throws ServletException 34 {
35 }
36 }
配置web.xml
显示代码
打印
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
1
2 Set Character Encoding 3 SetCharacterEncodingFilter 4
5
6 Set Character Encoding 7 /*
8
如果你的还是出现这种情况的话你就往下看看是不是你出现了第四中情况,你的Form提交的数据是
不是用get提交的,一般来说用post提交的话是没有问题的,如果是的话,你就看看第四中解决的
办法。
还有就是对含有汉字字符的信息进行处理,处理的代码是:
显示代码
打印
01 package dbJavaBean;
02
03 public class CodingConvert
04 {
05 public CodingConvert()
06 {
07 //
08 }
09 public String toGb(String uniStr){
10 String gbStr = "";
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
11 if(uniStr == null){
12 uniStr = "";
13 }
14 try{
15 byte[] tempByte = uniStr.getBytes("ISO8859_1"); 16 gbStr = new String(tempByte,"GB2312"); 17 }
18 catch(Exception ex){
19 }
20 return gbStr;
21 }
22
23 public String toUni(String gbStr){ 24 String uniStr = "";
25 if(gbStr == null){
26 gbStr = "";
27 }
28 try{
29 byte[] tempByte = gbStr.getBytes("GB2312"); 30 uniStr = new String(tempByte,"ISO8859_1"); 31 }catch(Exception ex){
32 }
33 return uniStr;
34 }
35 }
你也可以在直接的转换,首先你将获取的字符串用ISO-8859-1进行编码,然后将这个编码存放到一
个字节数组中,然后将这个数组转化成字符串对象就可以了,例如: 显示代码
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
打印
1 String str=request.getParameter(“girl”);
2 Byte B[]=str.getBytes(“ISO-8859-1”);
3 Str=new String(B);
通过上述转换的话,提交的任何信息都能正确的显示。
(三) 在Formget请求在服务端用request. getParameter(“name”)时返回的是乱码;按tomcat的做法设置Filter也没有用或者用 request.setCharacterEncoding("GBK");也不管用问题是出在处理参数传递的方法上:如果在servlet中用 doGet(HttpServletRequest request, HttpServletResponse response)
方法进行处理的话前面即使是写了:
显示代码
打印
1 request.setCharacterEncoding("GBK");
2 response.setContentType("text/html;charset=GBK");
也是不起作用的,返回的中文还是乱码~~~如果把这个函数改成
doPost(HttpServletRequest request, HttpServletResponse response)一切就OK了。 to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
同样,在用两个JSP页面处理
单输入之所以能显示中文是因为用的是post方法传递的,改成get方法依旧不行。
由此可见在servlet中用doGet()方法或是在JSP中用get方法进行处理要注意。这毕竟涉及到要通过浏览器传递参数信息,很有可能引起常用字符集的冲突或是不匹配。
解决的办法是:
1) 打开tomcat的server.xml文件,找到区块,加入如下一行:
URIEncoding=”GBK”
完整的应如下:
显示代码
打印
2)重启tomcat,一切OK。
需要加入的原因大家可以去研究 $TOMCAT_HOME/webapps/tomcat-docs/config/http.html下的这个文件就可以知道原因了。需要注意的是:这个地方如果你要是用UTF-8的时候在传递的过程中在Tomcat中也是要出现乱码的情况,如果不行的话就换别的字符集。
(四) JSP页面上有中文,按钮上面也有中文,但是通过服务器查看页面的时候出现乱码: 解决的办法是:首先在JSP文件中不应该直接包含本地化的消息文本,而是应该通过
标签从Resource Bundle中获得文本。应该把你的中文文本放到Application.properties文件中,这个文件放在WEB-INF/classes/* 下,例如我在页面里有姓名,年龄to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
两个label,我首先就是要建一个Application.properties,里面的内容应该是name=”姓名” age=”年龄”,然后我把这个文件放到WEB-INF/classes/properties/下,
接下来根据 Application.properties文件,对他进行编码转化,创建一个中文资源文件,假定名字是 Application_cn.properties。在JDK中提供了native2ascii命令,他能够实现字符编码的转换。在DOS环境中找到你放置Application.properties的这个文件的目录,在DOS环境中执行一下命令,将生成按GBK编码的中文资源文件 Application_cn.properties:
native2ascii ?encoding gbk Application.properties Application_cn.properties执行以上命令以后将生成如下内容的Application_cn.properties文件: name=u59d3u540d age=u5e74u9f84,在Struts-config.xml中配置:。 到这一步,基本上完成了一大半,接着你就要在JSP页面上
写 <%@ page language="java" contentType="text/html;charset=GBK" %>,到名字的那个label是要写,这样的化在页面上出现的时候就会出现中文的姓名,年龄这个也是一样,按钮上汉字的处理也是同样的。
(五) 写入到数据库是乱码:
解决的方法:要配置一个filter,也就是一个Servelet的过滤器,代码如同第二种时候一样。 如果你是通过JDBC直接链接数据库的时候,配置的代码如下:
jdbc:mysql://localhost:3306/workshopdb? useUnicode=true&characterEncoding=GBK,这样保证到数据库中的代码是不是乱码。
如果你是通过数据源链接的化你不能按照这样的写法了,首先你就要写在配置文件中,在tomcat 5.0.19中配置数据源的地方是在C:Tomcat 5.0confCatalinalocalhost这个下面,我建立的工程是workshop,放置的目录是webapp下面,workshop.xml 的配置文件如下: 显示代码
打印
01 02
03
05
06 09
10 11
12 factory
13 org.apache.commons.dbcp.BasicDataSourceFactory
14 15
16 maxActive
17 100
18
19
20 maxIdle
21 30
22
23
24
25
26 maxWait
27 10000
28
29
30
31 username
32 root
33
34
35 password
36
37
38
39 40 41 driverClassName 42 com.mysql.jdbc.Driver to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
43
44
45 url
4 47
48
49
50
粗体的地方要特别的注意,和JDBC直接链接的时候是有区别的,如果你是配置正确的化,当你输入中文的时候到数据库中就是中文了,有一点要注意的是你在显示数据的页面也是要用<%@ page language="java" contentType="text/html;charset=GBK" %>这行代码的。需要注意的是有的前台的人员在写代码的是后用Dreamver写的,写了一个Form的时候把他改成了一个jsp,这样有一个地方要注意了,那就是在Dreamver中Action的提交方式是request的,你需要把他该过来,因为在jsp的提交的过程中紧紧就是POST和 GET两种方式,但是这两种方式提交的代码在编码方面还是有很大不同的,这个在后面的地方进行说明。3
以上就是我在开发系统中解决中文的问题,不知道能不能解决大家的问题,时间匆忙,没有及时完善,文笔也不是很好,有些地方估计是词不达意。大家可以给我意见,希望能共同进步。
出售舆情监控系统
如果图片或页面不能正常显示请点击这里
【文章投稿】【收藏此页】【飞诺社区】【发表评论】【关闭】
上一篇:JSP 中文显示乱码解决
下一篇: 重新学习 Hibernate fetch lazy cascade inverse 关键字
Javascript技术文档推荐文章
, JavaScript 中变态的 parseInt
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
, jQuery TextBox自动完成条
, JavaScript中IOC【Inversion of Control】模式和AOP模式、观察者模式的交融
, 不再限于页面脚本 JavaScript挺入服务器端开发语言序列 , 运用jQuery 在阅读器中处理 XML
, array_key_exists() 函数
, CXF全接触(六) --- 搭建简单的Web服务环境 , 简单抽屉效果的实现
, 1.3 不用临时变量交换值
, 配置tomcat6.0,tomcat5.5的admin组件教程收藏 , 圆角矩形的html+css实现
, js实现的一个效果
, javascript编程必备_JS语法字典第1/2页 , mysql 的load data infile - 星星的故事 - CSDN博客 , JS 字符串连接[性能比较]
, javascript2ù×?cookie_??è?ó?DT??′ú?? 暂无数据~
请您留言
昵称:
验证码:
注册会员 会员登录
也许你对以下文章感兴趣
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
, 前端优化方案-JavaScript 优化方案
, javascript对象的property和prototype是这样一种关系 , Javascript 浅拷贝、深拷贝的实现代码 , 重温js的几个字符串函数
, MyEclipse下开发Web Service
, 可浮动QQ在线客服
, PHP调用javascript的注意事项
, 语义化 H1 标签
频道地图
Javascript
, Javascript系列教程
, Javascript实例
, Javascript技术文档
FrontPage
Dreamweaver
html
Javascript
css
网页制作技巧
网页特效
色彩原理
设计理念
web技术文档 Javascript技术文档相关文章
, JSF学习,导航
, JavaScript颜色板调用
, Javascript速查手册
, JavaScript实现Map数据结构,及JS中的单例
, 解决JSP数据输入中文乱码问题
, 用javascript进行xml的转换
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
, jstl+el(JFIS)
, 用JavaScript调用JSF的Action, ActionListener , JavaScript 里如何把字符转换成数字型
, 用javascript实现input框日期时间格式化输入 , JS对select动态添加options操作[IE&FireFox兼容] , 用javascript做删除时的提示信息
, JSP中将多个文件打包下载代码
, Javascript身份证号校验
, Java程序员要掌握的十个JSP中的标签库
, JSP分页显示
, jspSmartUpload组件上传下载详解(全攻略,全集) , JavaScript操作cookie
, Java class loader(1)
, 几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比 , 体验javaFX 1.0
, Eclipse 报 “Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ”错误的解决办法
, java工具(最新)
, 贴几个代码给大家参考 —— Javascript脚本公共操作类,顺便上海地区.Net求职
Javascript技术文档热门文章
, javascript 实现打印,打印预览,打印设置
, JS实现图片切换效果
, 写一篇完整的Extjs Grid的简易翻页
, JS实现弹出层锁定窗口(改进版)
, JavaScript 弹出对话框3种方式
, 一个挺酷的JS导航栏
, JavaScript解析XML实现多级级联下拉列表
, javascript常用代码
, JavaScript不间断连续图片滚动效果
, javascript判断checkbox是否被选中
, English | 关于我们 | 诚聘英才 | 联系我们 | 网站大事 | 友情链接 | 广告服务 | 网站地图
, Copyright?2008-2011 飞诺网[diybl.Com] 版权所有
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
飞诺网 鲁ICP备10012328号
IT技术从现在起飞
to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit