JSP POST传参(乱码) 为何是ISO8859_1

Admonis 2011-12-28 11:12:19
A 页面:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<% response.setCharacterEncoding("UTF-8"); %>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<form action="B.jsp" method="post">
<input type="submit" name="submit" value="中文" />
</form>



B 页面:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<% request.setCharacterEncoding("UTF-8"); %>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<!-- 请问 #P1 为何乱码 -->
<p id="p1"><%=request.getParameter("submit")%></p>

<!-- 请问 #P2 为何要重建字符串?以前听说POST传参只需设置request编码,为什么是ISO8859_1?-->
<p id="p2"><%=new String(request.getParameter("submit").getBytes("ISO8859_1"), "UTF-8")%></p>
...全文
489 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Admonis 2011-12-30
  • 打赏
  • 举报
回复
String nonUTF8 = ((HttpServletRequest) servletRequest).getParameter("NON_UTF8");
Admonis 2011-12-30
  • 打赏
  • 举报
回复
问题解决了,没把Filter代码贴出来,Sorry!


public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
String nonUTF8 = ((HttpServletRequest) servletRequest).getParameter("NON_UTF8");

servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setContentType("text/html;charset=UTF-8");

filterChain.doFilter(servletRequest,
new HttpServletResponseWrapper((HttpServletResponse) servletResponse) {
public void setContentType(String s) {
if (s.length() > "text/html".length() && s.charAt(0) == 't' && s.startsWith("text/html")) {
//do nothing. This call could be trying to set the charset to another charset.
}
else {
super.setContentType(s);
}
}

});
}


原因是,我在request.setCharacterEncoding("UTF-8"); 之前
获取过一次request参数(request.getParameter("XXXX"); 此时在还没设置过编码,Tomcat会设置默认Post请求参数编码为ISO8859_1,那么你再下面再过滤成UTF-8无效了,还是ISO-8859-1

zhouyusunquan 2011-12-28
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<% request.setCharacterEncoding("UTF-8"); %>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />


你首先要搞清楚这几个编码的作用

http://apps.hi.baidu.com/share/detail/33081081
zhouyusunquan 2011-12-28
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<% request.setCharacterEncoding("UTF-8"); %>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />


你首先要搞清楚这几个编码的作用

http://apps.hi.baidu.com/share/detail/33081081
朝花夕拾 2011-12-28
  • 打赏
  • 举报
回复
为什么是8859-1

request.getParameter("submit")就可以了啊。
安特矮油 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 li498833284 的回复:]
浏览器会先转到tomcat中 tomcat 配置文件中的默认编码格式 就是 iso88591
[/Quote]
+
Admonis 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 justsmilence 的回复:]

+1引用 6 楼 li498833284 的回复:

浏览器会先转到tomcat中 tomcat 配置文件中的默认编码格式 就是 iso88591
[/Quote]

Tomcat中默认编码好像是 URIEncoding 的编码,指的是Get访问方式。。
EEXXTTJJSS 2011-12-28
  • 打赏
  • 举报
回复
在service.xml修改编码格式
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/>
  • 打赏
  • 举报
回复
+1[Quote=引用 6 楼 li498833284 的回复:]

浏览器会先转到tomcat中 tomcat 配置文件中的默认编码格式 就是 iso88591
[/Quote]
li498833284 2011-12-28
  • 打赏
  • 举报
回复
浏览器会先转到tomcat中 tomcat 配置文件中的默认编码格式 就是 iso88591
teemai 2011-12-28
  • 打赏
  • 举报
回复
引用:
<%--
我的设置不正确吗?编码作我是知道的,我的问题是:Method=POST发送,接收数据时为什么不是UTF8,而是ISO8859_1...
--%>

<!-- 请问 #P2 为何要重建字符串?以前听说POST传参只需设置request编码,为什么是ISO8859_1?-->
<p id="p2"><%=new String(request.getParameter("submit").getBytes("ISO8859_1"), "UTF-8")%></p>

如果不指定编码的话,默认编码就是ISO8859-1。

看下这篇文章:
http://cosmo1987.iteye.com/blog/1116959
Admonis 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhouyusunquan 的回复:]
你首先要搞清楚这几个编码的作用

http://apps.hi.baidu.com/share/detail/33081081
[/Quote]


<%--
我的设置不正确吗?编码作我是知道的,我的问题是:Method=POST发送,接收数据时为什么不是UTF8,而是ISO8859_1...
--%>

<!-- 请问 #P2 为何要重建字符串?以前听说POST传参只需设置request编码,为什么是ISO8859_1?-->
<p id="p2"><%=new String(request.getParameter("submit").getBytes("ISO8859_1"), "UTF-8")%></p>

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧