JSP网页的超链接传递中文参数乱码问题(在IE中的问题)
出问题的情况:
在IE6中打开formtest02.jsp,先输入连续三个相同的汉字,如:啊啊啊,提交表单,中文显示正常,然后点击超链接传递中文参数,在out.jsp中显示,在out.jsp中的结果是:啊啊?(前两个汉字正常,最后一个是乱码);
如果输入连续4个相同汉字,所有显示都正常,没有乱码。
如果输入1个汉字,点链接后又是乱码。
总结:输入奇数个汉字,最后一个汉字是乱码;输入偶数个汉字,结果正常。这个问题是怎么回事?请大侠们赐教。谢谢
表单页:formtest02.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'formtest02.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">表单中文处理</div>
<form name="form1" method="post" action="<%=basePath%>/demobasics/formtest02.jsp" >
<input type="text" name="text1" />
<input type="submit" value="提交"/>
</form>
<br/>
<a target="_blank" href="<%=basePath%>/demobasics/out.jsp?good=<%=request.getParameter("text1") %>">链结中文处理</a>
<%
out.print(request.getParameter("text1"));
out.print(request.getParameter("good"));
%>
</body>
</html>
点超链接后的页面:out.jsp
<%@ page language="java" import="java.util.*" pageEncoding="iso-8859-1" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'out.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<%
out.println(new String(request.getParameter("good").getBytes("ISO-8859-1"),"utf-8"));
%>
</body>
</html>
过滤器:CharacterEncodingFilter.java
package servlet;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class CharacterEncodingFilter implements Filter
{
private String encoding;
private boolean ignore;
public CharacterEncodingFilter()
{
encoding = "utf-8";
ignore = false;
}
public void init(FilterConfig config)
{
if (config.getInitParameter("encoding") != null)
encoding = config.getInitParameter("encoding");
if (config.getInitParameter("ignore") != null)
ignore = (new Boolean(config.getInitParameter("ignore"))).booleanValue();
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
if (!ignore)
{
req.setCharacterEncoding(encoding);
res.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void destroy()
{
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>setEncoding</filter-name>
<filter-class>servlet.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setEncoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>setEncoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!-- 过滤servlet -->
<filter-mapping>
<filter-name>setEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>setEncoding</filter-name>
<url-pattern>*.*</url-pattern>
</filter-mapping>
</web-app>
[解决办法]
把你的url用encodeURI()函数包起来,再提交就可以了