servlet接收get方式的中文参数,显示乱码,试了很多方式都不行呢!!!源码如下:
jsp主要代码页面<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<script type="text/javascript">
function getResult(){
var url = "teac_servlet/valid?name=张三";
if (window.XMLHttpRequest) { 
req = new XMLHttpRequest(); 
}
else if (window.ActiveXObject){ 
req = new ActiveXObject("Microsoft.XMLHTTP"); 
}  if(req){ 
req.open("GET",url, true); 
req.send(null); 

}
</script>servlet源码:
public class UserValidate extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {

String strName = request.getParameter("name"); System.out.println("strName:"+strName);
//打印出的strName为????????????

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}}
打印出的strName为????????????

解决方案 »

  1.   

    url:encodeURI(encodeURI(str));
    action:URLDecoder.decode(str,"UTF-8")
      

  2.   

    doGet加:
    response.setContentType("text/html;charset=utf-8");
    request.setCharacterEncoding("utf-8");
      

  3.   

    实在不行重新编码吧
    strName = new String("strName".getByte("iso-8859-1"),"uft-8")
      

  4.   

    tomcat的server.xml 设置 URIEncoding="UTF-8"
        <Connector executor="tomcatThreadPool"
                   port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" 
                   connectionTimeout="20000" 
                   redirectPort="8443" URIEncoding="UTF-8"/>
    试试
      

  5.   

    先转换再传var value = encodeURI(encodeURI(value));
    获取 String value = EscapeUnicode.unescape(value);
      

  6.   

    有以下几个地方可以检查:
    1、首先进入servlet中时检查request传入的编码
    request.getCharacterEncoding();
    2、web.xml中有没有EncodingFilter过滤器,其中的编码设置检查一下
    3、如果没有,也可以自己写一个过滤器,设定request的编码
    request.setCharacterEncoding("UTF-8")
    4、jsp页面上pageEncoding设置要检查一下
    5、tomcat/conf/server.xml中,connector这里的编码设置:URIEncoding="UTF-8"
    以上逐条检查调试,原则是:编码要保持一致。
      

  7.   

    Quote: 引用 2 楼 splendid_java 的回复: