zoukankan      html  css  js  c++  java
  • 将请求的数据以json(plain)输出有乱码

    由于发送一个请求路径返回到浏览器是一串json字符串,虽然写了过滤器但是实际上过滤器没有拦截到。因此它是一段纯文本格式,所有要设置它的编码类型和字符编码,

    也就是response.setContentType("text/plain;charset=UTF-8"),但是它的设定一定要在pw.write(json)之前。否则还是会出现乱码。

    public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{

     /**
      * @Fields serialVersionUID : 序列化
      */
     private static final long serialVersionUID = 2823334602737926536L;
     
     public static final String JSON_ERROR = "{\"success\":\"false\"}";
     public static final String JSON_SUCCESS = "{\"success\":\"true\"}";
     protected HttpServletRequest _request;
     protected HttpServletResponse _response;
     
     @Override
     public void setServletResponse(HttpServletResponse response) {
      this._response=response;
      this._response.setContentType("text/plain;charset=UTF-8");
     }

     @Override
     public void setServletRequest(HttpServletRequest request) {
      this._request=request;
     }
     @JSON(serialize=false)
     public void returnJson(String json){
      PrintWriter pw=null;
      try {
       pw=this._response.getWriter();
       pw.write(json);
       pw.flush();
      } catch (IOException e) {
       e.printStackTrace();
      }finally{
       if (pw!=null) {
        pw.close();
       }
      }
     } 
    }

  • 相关阅读:
    Pull Request
    选择器
    常见HTTP状态码
    286. Walls and Gates
    200. Number of Islands
    1. Two Sum
    名片管理系统(python实现)
    k近邻算法(简单版)
    基数排序
    递归算法的调试
  • 原文地址:https://www.cnblogs.com/lbangel/p/3045889.html
Copyright © 2011-2022 走看看