为什么从 JSP 生成的动态 JNLP 总是因 localhost href 而失败?
我有一个 JSP,它会输出 JNLP 文件,如下所示。部署后在本地主机上工作 到远程服务器时,Java Web Start 出错并出现异常 -
无法加载资源: http://localhost :8080/jnlp/myjnlp.jnlp
ExitException[ 3]com.sun.deploy.net.FailedDownloadException: Unable to load resource: http: // localhost:8080/jnlp/myjnlp.jnlp
at com.sun.javaws.Launcher.updateFinalLaunchDesc(Unknown Source)
at com.sun.javaws.Launcher.updateFinalLaunchDesc(Unknown Source)
at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
at com.sun.javaws.Launcher.launch(Unknown Source)
at com.sun.javaws.Main.launchApp(Unknown Source)
at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
at com.sun.javaws.Main$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
包含的 JNLP 文件正在根据服务器的 URL 替换其代码库行。将调试器附加到 JSP 会显示正确的代码库行以及服务器的 IP/主机名。
不明白 localhost 来自哪里?
<%@ page import="java.io.*" %>
<%@ page contentType="application/x-java-jnlp-file" language="java" %>
<%
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", -1);
response.setContentType("application/x-java-jnlp-file");
// Generating the codebase for the JNLP. This URL will be used for downloading this jsp and the jar
StringBuffer jspUrlsb = request.getRequestURL();
String url = jspUrlsb.substring(0,jspUrlsb.lastIndexOf("/"));
url = url.substring(0,url.lastIndexOf("/"));
String jnlpCodebase = url+ "/jnlp/";
String tool = request.getParameter("tool");
tool = tool==null || tool.length()==0 ? "myjnlp" : tool;
String jnlpFile = tool+".jnlp";
response.setHeader("Content-Disposition", "filename=\""+jnlpFile+"\";");
String path = config.getServletContext().getRealPath(request.getContextPath());
FileInputStream fis = new FileInputStream(new File(path,"/jnlp/"+jnlpFile));
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) { //consuming the stream
if (line.startsWith("<jnlp")) {
line = "<jnlp codebase=\""+jnlpCodebase+"\" href=\""+jnlpFile+"\" spec=\"6.0+\">";
}
%>
<%=line%>
<%
}
br.close();
isr.close();
fis.close();
%>
I have a JSP that spits out a JNLP file as shown below. Work son localhost, when deployed
to a remote server, Java Web Start errors out with an exception -
Unable to load resource: http://localhost:8080/jnlp/myjnlp.jnlp
ExitException[ 3]com.sun.deploy.net.FailedDownloadException: Unable to load resource: http: // localhost:8080/jnlp/myjnlp.jnlp
at com.sun.javaws.Launcher.updateFinalLaunchDesc(Unknown Source)
at com.sun.javaws.Launcher.updateFinalLaunchDesc(Unknown Source)
at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
at com.sun.javaws.Launcher.launch(Unknown Source)
at com.sun.javaws.Main.launchApp(Unknown Source)
at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
at com.sun.javaws.Main$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
The included JNLP file is getting its codebase line replaced based on server's URL. Attaching a debugger to the JSP show sthe correct codebase line with server's IP /Host name.
Don't understand where the localhost comes from ?
<%@ page import="java.io.*" %>
<%@ page contentType="application/x-java-jnlp-file" language="java" %>
<%
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", -1);
response.setContentType("application/x-java-jnlp-file");
// Generating the codebase for the JNLP. This URL will be used for downloading this jsp and the jar
StringBuffer jspUrlsb = request.getRequestURL();
String url = jspUrlsb.substring(0,jspUrlsb.lastIndexOf("/"));
url = url.substring(0,url.lastIndexOf("/"));
String jnlpCodebase = url+ "/jnlp/";
String tool = request.getParameter("tool");
tool = tool==null || tool.length()==0 ? "myjnlp" : tool;
String jnlpFile = tool+".jnlp";
response.setHeader("Content-Disposition", "filename=\""+jnlpFile+"\";");
String path = config.getServletContext().getRealPath(request.getContextPath());
FileInputStream fis = new FileInputStream(new File(path,"/jnlp/"+jnlpFile));
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) { //consuming the stream
if (line.startsWith("<jnlp")) {
line = "<jnlp codebase=\""+jnlpCodebase+"\" href=\""+jnlpFile+"\" spec=\"6.0+\">";
}
%>
<%=line%>
<%
}
br.close();
isr.close();
fis.close();
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信它来自本地缓存的 JNLP 文件。有多种方法可以检验这一理论,其中一些方法比其他方法更可靠。
1) 在从未在本地主机测试中使用过的 PC 上尝试 Web 版本。
对于开发机本身。
2) 在启动“实时”网络版本之前,从 Java 缓存 卸载应用程序。或者..
3) 出于每次测试的目的,完全移动缓存。
As two asides..
从上面的代码片段中我不清楚 JNLP href 是否被写入响应。如果是,请将其删除。在动态生成的 JNLP 中包含 href 通常是不必要的,而且会适得其反。
请编辑您的帖子,将输入/输出、代码/代码片段、HTML/XML 格式化为代码。
I believe it comes from the JNLP file that is cached locally. There are various ways to test this theory, some more reliable than others.
1) Try the web version on a PC never used in testing on the localhost.
For the development machine itself.
2) Uninstall the application from the Java Cache before launching the 'live' web based version. Or..
3) Move the cache entirely, for the purposes of each test.
As two asides..
I was not clear from the code snippets above whether the JNLP href was being written to the response. If it is, remove it. It is usually both unnecessary and counterproductive to include the href in a dynamically generated JNLP.
Please edit your post to format the input/output, code/code snippets, HTML/XML as code.
尽管使用 Java 6u21,我还是被迫对代码库进行硬连线。这应该在 18+ 更新中得到修复,但对我来说不起作用。硬连线代码库 URL 后,它就可以工作了。
Despite using Java 6u21 i was forced to hardwire the codebase. This was supposed to have been fixed in update 18+ but didn't work for me. After hardwiring the codebase URL it works.
如果您使用动态 jnlp,则必须动态对文件进行签名,即从网络服务器调用 jarsigner 以及动态生成的 jnlp,即首先将它们打包。
If you use a dynamic jnlp you have to dynamically sign the files, i.e. invoke jarsigner from the webserver along w/ the dynamically generated jnlp, i.e. jar 'em first as well.