9
回答
如何解决jquery中文问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我用jquery get方法提交,到servlet中处理数据,然后用response.getWriter().write()方法将数据返回到页面,但返回的结果中 中文全部是乱码。代码如下:

PrintWriter out = response.getWriter();
        try {    
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8"); 
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("loginUser");
            String mblogName = request.getParameter("funsName");
            HashMap<String,Object> params = new HashMap<String,Object>();
            params.put("mblogUserId", String.valueOf(user.getId()));
            StringBuffer result = new StringBuffer("");
            Map<String,Integer> areaMap = new HashMap<String,Integer>();
            List<TMBlog> blogs = MBlogHelper.getMBlogByParams(params);
            if(blogs != null && blogs.size() > 0) {
                result.append("[");
                result.append("{\"name\":\"粉丝地域分布\",");
                result.append("\"type\":\"pie\",");
//                result.append("\"xAxis\":{\"categories\":[");
//                String xAxis = "";
                for(int i = 0;i < blogs.size();i++) {
                    TMBlog blog = blogs.get(i);
                    String location = blog.getMblogUserLocation() == null ? "" : blog.getMblogUserLocation().substring(0,2);
                    if(!areaMap.containsKey(location)) {
                        areaMap.put(location, 1);
                    } else {
                        areaMap.put(location, areaMap.get(location)+1);
                    }
                    
                }
                
//                if(areaMap != null && areaMap.size() > 0) {
//                    Iterator<String> it = areaMap.keySet().iterator();
//                    while(it.hasNext()) {
//                        String key = it.next();
//                        xAxis += "\""+key+"\",";
//                    }
//                    xAxis = xAxis.substring(0,xAxis.length()-1);
//                    result.append(xAxis+"],");
//                }
                result.append("\"data\":[");
                if(areaMap != null && areaMap.size() > 0) {
                    Iterator<String> it = areaMap.keySet().iterator();
                    while(it.hasNext()) {
                        String key = it.next();
                        Integer value = areaMap.get(key);
                        result.append("[\""+key+"\","+value+"],");
                    }
                }
                if(String.valueOf(result).endsWith(",")) {
                    result = new StringBuffer(result.substring(0, result.length()-1));
                }
                result.append("]}]");
                
                System.out.println(response.getCharacterEncoding());
                System.out.println(response.getContentType());
                out.write(String.valueOf(result));
                out.close();
            } else {
                out.write("[{\"message\":\"no funs named "+mblogName+" no funs\"}]");
            }
        } catch (Exception e) {
            out.write(e.getMessage());
            out.close();
        }

输出的response.getCharacterEncoding()是iso-8859-1,请大家帮忙解决一下,谢谢

举报
赵国鑫
发帖于6年前 9回/815阅
顶部