现在的位置: 首页 > 综合 > 正文

关于页面编码一点小结

2013年09月01日 ⁄ 综合 ⁄ 共 7040字 ⁄ 字号 评论关闭

JSP页面通过<%page contentType 属性指定生产网页的文件格式和编码字符集,

如 contentType="text/html;charset=GBK "

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCtype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> 收集参数的表单页 </title>
    <meta name="website" content="http://www.crazyit.org" />
</head>
<body>
<form id="form1" method="post" action="request1.jsp">
用户名:<br/>
<input type="text" name="name"><hr/>
性别:<br/>
男:<input type="radio" name="gender" value="男">
女:<input type="radio" name="gender" value="女"><hr/>
喜欢的颜色:<br/>
红:<input type="checkbox" name="color" value="红">
绿:<input type="checkbox" name="color" value="绿">
蓝:<input type="checkbox" name="color" value="蓝"><hr/>
来自的国家:<br/>
<select name="country">
    <option value="中国">中国</option>
    <option value="美国">美国</option>
    <option value="俄罗斯">俄罗斯</option>
</select><hr/>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>

request1.jsp:

%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> 获取请求头/请求参数 </title>
    <meta name="website" content="http://www.crazyit.org" />
</head>
<body>
<%
//获取所有请求头的名称
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements())
{
    String headerName = headerNames.nextElement();
    //获取每个请求、及其对应的值
    out.println(
        headerName + "-->" + request.getHeader(headerName) + "<br/>");
}
out.println("<hr/>");
//设置解码方式,对于简体中文,使用gb2312解码
request.setCharacterEncoding("gb2312");
//下面依次获取表单域的值
String name = request.getParameter("name");
String gender = request.getParameter("gender");
//如果某个请求参数有多个值,将使用该方法获取多个值
String[] color = request.getParameterValues("color");
String national = request.getParameter("country");
%>
<!-- 下面依次输出表单域的值 -->
您的名字:<%=name%><hr/>
您的性别:<%=gender%><hr/>
<!-- 输出复选框获取的数组值 -->
您喜欢的颜色:<%for(String c : color)
{out.println(c + " ");}%><hr/>
您来自的国家:<%=national%><hr/>
</body>
</html>

正常输出:不出现中文乱码

accept-->image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel, */*
referer-->http://localhost:8080/jspObject/form.jsp
accept-language-->zh-cn
content-type-->application/x-www-form-urlencoded
accept-encoding-->gzip, deflate
user-agent-->Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
host-->localhost:8080
content-length-->91
connection-->Keep-Alive
cache-control-->no-cache
cookie-->JSESSIONID=F00D118994EB31E0FA53C55435EAD8F2


您的名字:张三


您的性别:男


您喜欢的颜色:红 绿 蓝


您来自的国家:中国

当charset改为UTF-8 时

<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
<!DOCtype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> 收集参数的表单页 </title>
    <meta name="website" content="http://www.crazyit.org" />
</head>
<body>
<form id="form1" method="post" action="request1.jsp">
用户名:<br/>
<input type="text" name="name"><hr/>
性别:<br/>
男:<input type="radio" name="gender" value="男">
女:<input type="radio" name="gender" value="女"><hr/>
喜欢的颜色:<br/>
红:<input type="checkbox" name="color" value="红">
绿:<input type="checkbox" name="color" value="绿">
蓝:<input type="checkbox" name="color" value="蓝"><hr/>
来自的国家:<br/>
<select name="country">
    <option value="中国">中国</option>
    <option value="美国">美国</option>
    <option value="俄罗斯">俄罗斯</option>
</select><hr/>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>

请求解码为gb2312,将出现乱码  request.setCharacterEncoding("gb2312");

<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> 获取请求头/请求参数 </title>
    <meta name="website" content="http://www.crazyit.org" />
</head>
<body>
<%
//获取所有请求头的名称
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements())
{
    String headerName = headerNames.nextElement();
    //获取每个请求、及其对应的值
    out.println(
        headerName + "-->" + request.getHeader(headerName) + "<br/>");
}
out.println("<hr/>");
//设置解码方式,对于简体中文,使用gb2312解码
request.setCharacterEncoding("gb2312");
//下面依次获取表单域的值
String name = request.getParameter("name");
String gender = request.getParameter("gender");
//如果某个请求参数有多个值,将使用该方法获取多个值
String[] color = request.getParameterValues("color");
String national = request.getParameter("country");
%>
<!-- 下面依次输出表单域的值 -->
您的名字:<%=name%><hr/>
您的性别:<%=gender%><hr/>
<!-- 输出复选框获取的数组值 -->
您喜欢的颜色:<%for(String c : color)
{out.println(c + " ");}%><hr/>
您来自的国家:<%=national%><hr/>
</body>
</html>

accept-->image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel, */*
referer-->http://localhost:8080/jspObject/form.jsp
accept-language-->zh-cn
content-type-->application/x-www-form-urlencoded
accept-encoding-->gzip, deflate
user-agent-->Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
host-->localhost:8080
content-length-->99
connection-->Keep-Alive
cache-control-->no-cache
cookie-->JSESSIONID=F00D118994EB31E0FA53C55435EAD8F2


您的名字:寮��


您的性别:��


您喜欢的颜色:绾� 缁�


您来自的国家:涓��

此时请求解码应该用 与发生请求参数页面的编码格式相同的编码字符集才不会出现乱码。

<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> 获取请求头/请求参数 </title>
    <meta name="website" content="http://www.crazyit.org" />
</head>
<body>
<%
//获取所有请求头的名称
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements())
{
    String headerName = headerNames.nextElement();
    //获取每个请求、及其对应的值
    out.println(
        headerName + "-->" + request.getHeader(headerName) + "<br/>");
}
out.println("<hr/>");
//设置解码方式,对于简体中文,使用gb2312解码
request.setCharacterEncoding("UTF-8");
//下面依次获取表单域的值
String name = request.getParameter("name");
String gender = request.getParameter("gender");
//如果某个请求参数有多个值,将使用该方法获取多个值
String[] color = request.getParameterValues("color");
String national = request.getParameter("country");
%>
<!-- 下面依次输出表单域的值 -->
您的名字:<%=name%><hr/>
您的性别:<%=gender%><hr/>
<!-- 输出复选框获取的数组值 -->
您喜欢的颜色:<%for(String c : color)
{out.println(c + " ");}%><hr/>
您来自的国家:<%=national%><hr/>
</body>
</html>

accept-->image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel, */*
referer-->http://localhost:8080/jspObject/form.jsp
accept-language-->zh-cn
content-type-->application/x-www-form-urlencoded
accept-encoding-->gzip, deflate
user-agent-->Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
host-->localhost:8080
content-length-->115
connection-->Keep-Alive
cache-control-->no-cache
cookie-->JSESSIONID=F00D118994EB31E0FA53C55435EAD8F2


您的名字:李四


您的性别:男


您喜欢的颜色:红 绿 蓝


您来自的国家:中国

结论:

解码的编码应该与页面的charset 指定的编码字符集一致,才能保证中文不出乱码!

抱歉!评论已关闭.