用jquery打开新窗口

时间:2014-01-28 06:33:03

标签: java jquery ajax servlets

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

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

和ajax

 $(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);
    }

1 个答案:

答案 0 :(得分:0)

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

您的servelet代码包含以下行以获取上下文路径:

 //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;