2014-01-28 161 views
1

我有一个ajax调用,它向Servlet发送一个值,Servlet创建一个html文件,然后返回已经创建的html文件的绝对路径。
在来自服务器的响应是用jquery打开新窗口

"C:\Users\Ker\Documents\NETBEANS PROJECT\Titanium\build\web\default\result.html" 

,并在阿贾克斯

$(function() { 
      $("#BTNRUN").click(function() { 

       var html = $("#HTML").val(); 
       var body1 =html.replace('%','^'); 
       var body2=body1.replace('&','|'); 
       var css = $("#CSS").val(); 
       var js = $("#JS").val(); 
       var dataString = 'RUN=1&BODY=' + body2 
         + '&CSS=' + css + '&JS=' + js; 
       $.ajax({ 
        type: "POST", 
        url: "TitaniumServlet", 
        data: dataString, 
        success: function(data) { 
         window.open(data); 
        } 
       }); 
       return false; 
      }); 
     }); 

它会打开一个新的浏览器,但我送花儿给人去约:空白页。这个地址有什么问题吗?

编辑:

try { 

main_path is the context path of the project 

     File file = new File(main_path + "\\result.html"); 
     if (file.createNewFile()) { 
      System.out.println("File is created!"); 
     } else { 
      System.out.println("File already exists."); 
      file.delete(); 
      file.createNewFile(); 
     } 
     writer = new PrintWriter(new FileOutputStream(file, false)); 
     writer.write(html codes here); 
     return file.getAbsolutePath(); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(ProcessRun.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(ProcessRun.class.getName()).log(Level.SEVERE, null, ex); 
    } 
+0

你能告诉servlet代码? – SpringLearner

+0

尝试显示变量“数据”是什么。在这行后面“成功:函数(数据){”添加“console.log(数据);”在控制台中出现了什么? –

+0

@migueljimenezz我提到了数据的价值“C:\ Users \ Ker \ Documents \ NETBEANS PROJECT \ Titanium \ build \ web \ default \ result.html” –

回答

0

按我的理解,您已经创建了应用程序上下文路径上的文件和存储。如果新创建的文件位于上下文路径中,而不是直接使用相对路径或上下文路径访问文件,则可以使用服务器地址在新窗口中打开它。

你的小服务程序代码包括以下行来获取上下文路径:

//after your line writer.write(html codes here); 

String serverAddrs = request.getServerName()+":"+request.getServerPort(); 
String contextPath = "http://"+serverAddrs + main_path ; 
            //main_path is your context path 
contextPath = contextPath+"/result.html"; 
System.out.println(contextPath); 
return contextPath;