java,Serverlet,jsonp,js跨域获取SVG,XML

从事web相关开发的同学应该不会陌生跨域这个名词,跨域就是……自行百度


同事在实现一个大数据可视化平台时候遇到一个现象,展示端一般与管理端不在一起,展示端中有一个需求是ajax获取xml并将xml对象返回给echarts的回调,: 


require('echarts/util/mapData/params').params.DiySVG = {

    getGeoJson: function (callback) {
    //格式化xml文档
       function create_doc(text){
            var xmlDoc = null;
            try //Internet Explorer
            {
                xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async="false";
                xmlDoc.loadXML(text);
            }
            catch(e)
            {
                try //Firefox, Mozilla, Opera, etc.
                {
                    parser=new DOMParser();
                    xmlDoc=parser.parseFromString(text,"text/xml");
                }
                catch(e) {}
            }
            return xmlDoc;
        }
//跨域获取xml字符串
        $.jsonp({
            'contentType': "text/html;charset=UTF-8",
            'url': "http://192.168.50.126:8082/DataMapManager/BrowAjax.do",
            'data': {
                dz_type: 'getSvgByName',
                mapname:"暂未用到,测试写的绝对路径",
            },
            'dataType':"jsonp",
            'callbackParameter':"jsonCallback",
            'async':false,
            'type': 'get',
            'success': function (data) {
                console.warn( create_doc( data)  );
                callback(create_doc( data));
            },
            error:function (resp) {
                console.error( resp  );
            }
        });
    }
};

java实现方式,由于我是写的一个测试用例,java我用serverlet写了一下:  

@WebServlet(name = "BrowAjax")public class BrowAjax extends HttpServlet {  
    private HttpServletRequest _request;    
   private HttpServletResponse _response;   
    protected void doPost(HttpServletRequest request,  
    HttpServletResponse response) throws ServletException, IOException { 
   }  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
        response.setContentType("text/html;charset=UTF-8");      
        response.getWriter().flush();      
          _request = request;        
         _response = response;        
        String dz_type = request.getParameter("dz_type");       
         switch( dz_type  ){             
              case "getSvgByName":            
                System.out.println("getSvgBuName>");               
               GetSvgInfo();             
               break;       
             }    
          }     
private void GetSvgInfo() throws IOException {       
     String jsonCallback = _request.getParameter("jsonCallback");        
    String mapType = _request.getParameter("mapType");        
    String return_JSONP = "";        
    Getsvg svgUtil = new Getsvg();       
     try {         
    //测试写的绝对路径,自行解决  
   return_JSONP = svgUtil.fileRead("I:manager\\out\\artif\\map_exploded\\SVG\\yongan.svg");
//            System.out.println(return_JSONP);    
        //替换回车以及换行确保前端不会异常           
    return_JSONP= return_JSONP.replace("\n","");           
    return_JSONP= return_JSONP.replace("\r","");     
   } catch (Exception e) {           
     e.printStackTrace();       
   }       
 return_JSONP = jsonCallback + "('" + return_JSONP.toString()  + "');";       
 System.out.println( return_JSONP);       
 _response.getWriter().println( return_JSONP   );  
  }
}
//解析SVG工具类: 
public class Getsvg {
    public String  fileRead(String filename) throws Exception {
        File file = new File(filename);//定义一个file对象,用来初始化FileReader
        FileReader reader = new FileReader(file);
    //定义一个fileReader对象,用来初始化BufferedReader
        BufferedReader bReader = new BufferedReader(reader);
    //new一个BufferedReader对象,将文件内容读取到缓存
        StringBuilder sb = new StringBuilder();
    //定义一个字符串缓存,将字符串存放缓存中
        String s = "";
        while ((s =bReader.readLine()) != null) {
    //逐行读取文件内容,不读取换行符和末尾的空格
            sb.append(s + "\n");//将读取的字符串添加换行符后累加存放在缓存中
        }
        bReader.close();
        String str = sb.toString();
//        System.out.println(str );
        return  str;
    }
}


        // $.ajax({
        //     //url: "./veSVG/yongan.svg",
        //     url: svgfile,
        //     dataType: 'xml',
        //     success: function( xml ) {
        //         callback( xml )
        //     }
        // });
@WebServlet(name = "BrowAjax")public class BrowAjax extends HttpServlet { private HttpServletRequest _request; private HttpServletResponse _response; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {     response.setContentType("text/html;charset=UTF-8"); response.getWriter().flush(); _request = request; _response = response; String dz_type = request.getParameter("dz_type"); switch( dz_type ){ case "getSvgByName": System.out.println("getSvgBuName>"); GetSvgInfo(); break; } } private void GetSvgInfo() throws IOException { String jsonCallback = _request.getParameter("jsonCallback"); String mapType = _request.getParameter("mapType"); String return_JSONP = ""; Getsvg svgUtil = new Getsvg(); try { return_JSONP = svgUtil.fileRead("I:manager\\out\\artifacts\\DataMapManager_war_exploded\\veSVG\\yongan.svg");// System.out.println(return_JSONP); //替换回车以及换行确保前端不会异常 return_JSONP= return_JSONP.replace("\n",""); return_JSONP= return_JSONP.replace("\r",""); } catch (Exception e) { e.printStackTrace(); } return_JSONP = jsonCallback + "('" + return_JSONP.toString() + "');"; System.out.println( return_JSONP ); _response.getWriter().println( return_JSONP ); }}

阅读更多
个人分类: js jsonP java 跨域
想对作者说点什么? 我来说一句

jquery跨域调用 js跨域调用

2012年09月14日 826B 下载

没有更多推荐了,返回首页

不良信息举报

java,Serverlet,jsonp,js跨域获取SVG,XML

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭