六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 18|回复: 0

Java程序员从笨鸟到菜鸟之(二十三)常见乱码解决以及javaBean基础知识

[复制链接]

升级  84%

56

主题

56

主题

56

主题

秀才

Rank: 2

积分
176
 楼主| 发表于 2013-2-3 11:20:00 | 显示全部楼层 |阅读模式
 
 乱码问题应该是做javaWeb开发人员都遇到过的问题吧,这个问题当时还影响了我学习java的想法,甚至有过想放弃的想法,没办法,当时年轻,呵呵。其实产生乱码问题的原因有很多,解决乱码的问题也有很多,现在就一一来看一下:

出现乱码的地方大致可以分为以下三种:

1 jsp页面中 
2 jsp页面之间相互传参的参数
3 与数据库中数据的存取
解决方案大致可以分为三种:

  1 出现在jsp页面中,是由于没有设置jsp页面的中文字符编码。
  2 出现在jsp页面之间相互传参,是由于参数没有设置正确的字符编码。
  3 以上2个问题解决了,那么存到数据库中,自然就不存在乱码。除非你对存入到数据库里的数据再次进行编码。

具体的解决方法:

1.在表单页面头部设置字符编码为utf-8
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

加上这句解决jsp页面中的中文乱码显示,tomcat编译完后向客户端输出的html文件不是采用中文编码,所以会导致乱码产生。


 2.设置页面请求和回应的编码:

 <%request.setCharacterEncoding("utf-8");

  response.setCharacterEncoding("utf-8"); %>

加上这句解决jsp页面中的中文参数传递乱码。把浏览器默认使用的编码设置为“UTF-8”发送请求参数。

3.String(request.getParameter("name").getBytes("ISO8859_1"),"utf-8");这句的意思是,把传来的参数全部编码转换成utf-8,这样做的缺点是每次传来一个参数都要这样写,很麻烦。

同样可通过设置server.xml配置文件来实现。

<div style="font-family: Consolas, 'Courier New', Courier, mono, serif; background-color: #e7e5dc; width: 1097px; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; line-height: 25px; text-align: left;" class="dp-highlighter bg_html"><div style="padding-left: 45px;" class="bar"><div style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; line-height: normal; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c;" class="tools">[html] view plaincopy<div style="width: 17px; height: 17px;">
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表