81,092
社区成员
发帖
与我相关
我的任务
分享
<%@ 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>
<%@ 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>
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);
}
}
});
}
<%--
我的设置不正确吗?编码作我是知道的,我的问题是:Method=POST发送,接收数据时为什么不是UTF8,而是ISO8859_1...
--%>
<!-- 请问 #P2 为何要重建字符串?以前听说POST传参只需设置request编码,为什么是ISO8859_1?-->
<p id="p2"><%=new String(request.getParameter("submit").getBytes("ISO8859_1"), "UTF-8")%></p>