1.Jsp页面要设置
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
这样才能写入中文字符。
2.request.setCharacterEncoding(charset);必须写在第一次使用request.getParameter()之前,这样才能保证参数是按照已经设置的字符编码来获取。
response.setCharacterEncoding(charset);必须写在PrintWriter out = request.getWriter()之前,这样才能保证out按照已经设置的字符编码来进行字符输出。
3.其中的2并不能解决所有的问题,对于post请求,无论是“获取参数环节”还是“输出环节"都是没问题的;对于get请求,"输出环节"没有问题,但是"获取参数环节"依然出现中文乱码,所以在输出时直接将乱码输出了。
原因是post请求和get请求存放参数位置是不同的:
post方式参数存放在请求数据包的消息体中。get方式参数存放在请求数据包的请求行的URI字段中,以?开始以param=value¶me2=value2的形式附加在URI字段之后。而request.setCharacterEncoding(charset); 只对消息体中的数据起作用,对于URI字段中的参数不起作用,我们通常通过下面的代码来完成编码转换:
String paramValue = request.getParameter("paramName");
paramValue = new String(paramValue.trim().getBytes("ISO-8859-1"), charset);
但是这样就必须每次都要修改,所以
解决方法:在tomcat_home\conf\server.xml 中的Connector元素中设置URIEncoding属性为合适的字符编码
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"
/>
4.jsp页面中使用properties文件,注意properties文件在src的根目录下
<%
String path = getClass().getResource("/").getPath();//获得src的目录
InputStream in = new FileInputStream(path + "confg.properties");//创建文件输入流
Properties property = new Properties();
property.load(in);
int count = Integer.parseInt(property.getProperty("count"));
%>
5.properties不能写中文这类的占两个字节的字符,否则回报错误,some character cannot be mapped using “ISO8859-1” character encoding的提示 ,这是因为我们的文件默认的保存格式是ISO8859-1的,而这种格式只能放西欧文字,放入中文等就会出现问题,即使放进去也会在读的时候出现乱码,所以我们要想解决问题,必须,首先修改文件保存类型为UTF-8,这个怎么修改不写了,还是罗嗦两句吧,在要修改的文件中按键alt+enter出来property窗口,修改Text file Encoding就好了。
这样这首我们在存储的时候存储正确,只要在读的时候读取正确就OK了,我们可以用
new String(value.getBytes("ISO8859-1"), encoding);
其中value就是要修改的字符串了。
String device = new String(property.getProperty("device" + i).getBytes("ISO8859-1"), "UTF-8") ;
out.print("<option value=\"" + device + "\">" + device + "</option>");
写个工具转换类也可以
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class PropertiesUtil {
/**
* 指定编码获取properties文件中的属性值(解决中文乱码问题)
* @param properties java.util.Properties
* @param key 属性key
* @return value
*/
public static String getProperty(Properties properties, String key,
String encoding) throws UnsupportedEncodingException {
if (properties == null)
return null;
// 如果此时value是中文,则应该是乱码
String value = properties.getProperty(key);
if (value == null)
return null;
// 编码转换,从ISO8859-1转向指定编码
value = new String(value.getBytes("ISO8859-1"), encoding);
return value;
}
}
如果你的应用创建中使用的系统默认编码,则如下转化:
PropertiesUtil.getProperty(properties, "TestKey",
System.getProperty("file.encoding"));
6.Tomcate中空格路径错误
<%
String path = getClass().getResource("/").getPath();
out.println(getClass().getResource("/").getPath());
out.println(getClass().getClassLoader().getResource("/").getFile());
InputStream in = new FileInputStream(path + "confg.properties");
Properties property = new Properties();
property.load(in);
int count = Integer.parseInt(property.getProperty("count"));
%>
输出的路径为下边
/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/CPE/WEB-INF/classes/
程序运行正常,但是一一打war包,放到Tomcate下就报错
同样输出路径为
/D:/Program%20Files/apache-tomcat-6.0.16/webapps/CPE/WEB-INF/classes/
愣是说
D:\Program%20Files\apache-tomcat-6.0.16\webapps\CPE\WEB-INF\classes\confg.properties 这个文件找不到。
解决方法,把Tomcate放到没有空格的路径之下,运行正常起来。
分享到:
相关推荐
关于处理Javaweb中中文乱码的问题文.pdf
中文乱码javaWEB开发各种解决中文乱码问题
关于处理Javaweb中中文乱码的问题[收集].pdf
教学案例:解决中文输出乱码问题 1.所属课程: Java Web开发技术 2.相关技能点 HttpServletResponse setCharacterEncoding(String charset) setHeader(String name,String value) setContentType(String type) 2.训练...
介绍javaweb项目中出现乱码问题的解决方案。并介绍如何统一开发环境、应用服务器、本机浏览器的编码格式。
可以解决 javaweb乱码的完整的 方案
JavaWeb中字节乱码filter解决办法,其中包括tomcat各个版本进行字符设置.
中文乱码问题是JavaWeb开发中最常见的问题之一,解决的方法也有很多种,其中,过滤器是最常用的方法之一。提供了过滤器的源码和详细配置。
解决JavaWeb开发中Jsp存储读取MySQL数据中文乱码的问题.pdf
利用过滤器解决JavaWeb(JSP)的乱码问题
JavaWeb开发中的中文乱码问题分析及解决方案.docx
JavaWeb乱码问题终极解决方案.docx
JavaWeb开发技术-解决中文输出乱码问题.pptx
超全面javaweb第4天-_05_xml的中文乱码问题解决
讲述javaweb开发中的jsp乱码问题 get、post的区别 http协议的图解
在JavaWeb应用开发常见的问题中,中文的乱码问题在页面中没有统一的方法解决,造成程序员浪费大量的时间与精力且代码效率不高,因此提出了新的乱码算法,并设计了通用的乱码处理器,提高了程序员的工作效率。
JavaWeb的各种中文乱码终极解决方法 JavaWeb的各种中文乱码终极解决方法
乱码透析
利用过滤器解决JavaWeb(JSP)乱码问题