发送页面代码如下:
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!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=gbk">
<title>Insert title here</title>
</head>
<body>
<form action="get.jsp" method="get">
用户名:<input type="text" name="username" />
<input type="submit" value="提交" />
</form>
</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>
<%
String str1 = new String(request.getParameter("username").getBytes("gbk"), "UTF-8");
out.println(str1);
String str = new String(request.getParameter("username").getBytes("ISO-8859-1"), "gbk");
out.println(str);
str = new String(str.getBytes("gbk"),"UTF-8");
out.println(str);
%>
</body>
</html>如果两个页面的字符编码不变,如何才能在接收页面中正确显示中文?
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!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=gbk">
<title>Insert title here</title>
</head>
<body>
<form action="get.jsp" method="get">
用户名:<input type="text" name="username" />
<input type="submit" value="提交" />
</form>
</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>
<%
String str1 = new String(request.getParameter("username").getBytes("gbk"), "UTF-8");
out.println(str1);
String str = new String(request.getParameter("username").getBytes("ISO-8859-1"), "gbk");
out.println(str);
str = new String(str.getBytes("gbk"),"UTF-8");
out.println(str);
%>
</body>
</html>如果两个页面的字符编码不变,如何才能在接收页面中正确显示中文?
2.请详述编码转换方法,不要无用的泛泛说明。
out.println(str);
你的接受页面的编码方式确定是utf-8?
使用3楼的方法,结果还是乱码!(????? )确定是UTF-8!
源文件的编码方式一般来说都是操作系统的默认编码,txt中可以另存成其他的编码方式
就是想知道GET方法的解决办法,是不是得处理URL呢,如果要处理,具体又该怎么做呢?
设置成一样的看看
并且把接收页面设置一下请求编码
request.setCharacterEncoding("UTF-8");
11楼正解如果tomcat里不设置URIEncoding,有没有办法搞定呢?我试了下URLDecoder和URLEncoder,但均未成功!
pageEncoding="gbk"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>两个页面编码不同。
get 请求 会造成乱码问题。。要不换成 post 请求。要不就 在目标 页面得到参数值后转码。。
new String ("ss".getBytes("",""));
package com.shop.commons;import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class Chinese implements Filter { public void destroy() {
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
chain.doFilter(request, response);
} public void init(FilterConfig arg0) throws ServletException {
}}
在web.xml里配置一下就ok了!
<filter>
<filter-name>chinese</filter-name>
<filter-class>com.shop.commons.Chinese</filter-class>
</filter>
<filter-mapping>
<filter-name>chinese</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>