gpt4 book ai didi

java - 使用 jquery 打开新窗口

转载 作者:太空宇宙 更新时间:2023-11-04 15:10:24 24 4
gpt4 key购买 nike

我有一个 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);
}

最佳答案

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

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

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

关于java - 使用 jquery 打开新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21398476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com