参数传进去的时候就用URLEncode.encode方法转码下 或者js的urlencode 比如如果你用java的需要转码的参数是String term = "2011春"就 term = URLEncode.encode(term,"utf-8") 解码类似 js的转码网上搜下 类似的
to JavaEthan 应该是<%=new String(request.getParameter("term").getBytes("ISO-8859-1"),"utf-8")%>吧 ,还是不行,page1.jsp的内容如下: This is next page. request.getCharacterEncoding()=UTF-8 request.getParameter("term")=2011?
to zhouyuqwert 用js转码搞定了,<script language="JavaScript"> function toNextPage(){ window.location = encodeURI("<%=path%>/page1.jsp?term=2011春"); } </script> 对应得到的url是“http://localhost:8080/test/page1.jsp?term=2011%E6%98%A5”。谢谢啊!! 但是这样还是治标不治本,我应该在什么地方设置一下呢?各位大牛~~
This is first page. -- 13 <br> request.getCharacterEncoding()=<%out.println(request.getCharacterEncoding());%><br> 1:<%=path%> <input type="submit" value="go next page with parameters" onClick="toNextPage();" /> </body> </html>page1.jsp: <%@ page contentType="text/html;" pageEncoding="UTF-8"%> <html> <head> <title>next page</title> </head>
<body> This is next page. <br> request.getCharacterEncoding()=<%out.println(request.getCharacterEncoding());%><br> request.getParameter("term")=<%=request.getParameter("term")%><br> </body> </html> URL:为这样 http://localhost:8080/jspTest/page1.jsp?term=2011春夏秋冬 我的是IE8
已经尝试过您的方法,也是不行,我在页面里打出编码格式均是UTF-8
(<%out.println(request.getCharacterEncoding());%>)
能否具体一些?
我现在主要是两个疑问,
1、跳转过后的page1的url是http://localhost:8080/test/page1.jsp?term=2011春
而不是平常见到的会把汉字转换成%...。
2、我的参数“term=2011春”和“term=2011春夏秋冬”为什么后者不乱码而前者乱码,这个问题相当恼火了~~
比如如果你用java的需要转码的参数是String term = "2011春"就 term = URLEncode.encode(term,"utf-8")
解码类似
js的转码网上搜下 类似的
应该是<%=new String(request.getParameter("term").getBytes("ISO-8859-1"),"utf-8")%>吧
,还是不行,page1.jsp的内容如下:
This is next page.
request.getCharacterEncoding()=UTF-8
request.getParameter("term")=2011?
用js转码搞定了,<script language="JavaScript">
function toNextPage(){
window.location = encodeURI("<%=path%>/page1.jsp?term=2011春");
}
</script>
对应得到的url是“http://localhost:8080/test/page1.jsp?term=2011%E6%98%A5”。谢谢啊!!
但是这样还是治标不治本,我应该在什么地方设置一下呢?各位大牛~~
在获取参数前加
request.setCharacterEncoding("GBK");
要说get方式获取的话,有时候是会出现乱码的情况,
这不是因为你的代码写错了,可能是容器本身的问题,
比如TOMCAT,所以你的去看看你的配置文件
<%@ page contentType="text/html" pageEncoding="UTF-8"%><%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>
<html>
<head> </head>
<body>
<script type="text/javascript">
function toNextPage()
{
window.location = "/jspTest/page1.jsp?term=2011春夏秋冬";
}
</script>
This is first page. -- 13
<br>
request.getCharacterEncoding()=<%out.println(request.getCharacterEncoding());%><br>
1:<%=path%>
<input type="submit" value="go next page with parameters"
onClick="toNextPage();" />
</body>
</html>page1.jsp:
<%@ page contentType="text/html;" pageEncoding="UTF-8"%>
<html>
<head>
<title>next page</title>
</head>
<body>
This is next page. <br>
request.getCharacterEncoding()=<%out.println(request.getCharacterEncoding());%><br>
request.getParameter("term")=<%=request.getParameter("term")%><br>
</body>
</html>
URL:为这样 http://localhost:8080/jspTest/page1.jsp?term=2011春夏秋冬
我的是IE8
<%=new String(request.getParameter("term").getBytes("ISO-8859-1"),"utf-8")%>
把这个加到jsp页
不过貌似有些浏览器就帮做了,如果是中文URL浏览器会转码
比如你用Firefox试试,www.baidu.com/?a=中文
http://www.baidu.com/?a=中文