新建.jsp文件,charset和pageEncoding默认是ISO-8859-1,这样的话访问页面时会出现乱码,解决办法:将charset和pageEncoding改为UTF-8(或者GBK/GB2312/GB18030,如果想具体了解几种编码的前世今生可参考另一篇文章:网页编码就是那点事).
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body> </body>
</html>
修改后的代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">alert("中文!");</script>
中文!
</body>
</html>
总结:修改编码格式为UTF-8