JFileChooser弹出2次
这是我的 JSP 文件。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
//JFileChooser filechoose = new JFileChooser();
JFileChooser filechoose = new JFileChooser("D:\\");
filechoose.showOpenDialog(null);
File file = filechoose.getSelectedFile();
XLCauHoi.ImportXmlFileToData(file);
%>
<h4> Đã xuất file thành công </h4>
</body>
</html>
我的问题是:当我在浏览器上运行 JFileChooser 时,它会弹出两次。如果我在 Java 类中运行它,JFileChooser
会弹出 1 次。 我的问题是什么以及如何解决?
This is my JSP file.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
//JFileChooser filechoose = new JFileChooser();
JFileChooser filechoose = new JFileChooser("D:\\");
filechoose.showOpenDialog(null);
File file = filechoose.getSelectedFile();
XLCauHoi.ImportXmlFileToData(file);
%>
<h4> Đã xuất file thành công </h4>
</body>
</html>
My problem is that: the JFileChooser
pops up 2 times when I run it on browser. If I run it in a Java class, JFileChooser
pops up 1 time.
What is my problem and how to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一个重大的误解。首先,JSP/Java 在 Web 服务器上运行,生成一堆 HTML/CSS/JS 并将其发送到 Web 浏览器。 Web 浏览器退役 HTML/CSS/JS 并解释/应用/执行它。它不运行任何 Java 代码行,因为它已经在 Web 服务器上执行。右键单击网络浏览器中的页面并选择查看源代码。你看到了吗?如果网络服务器正确完成了它的工作,您不应该在其中看到任何 Java 代码行。网络浏览器无法理解它。它只理解 HTML/CSS/JS。
在 JSP scriptlet 中使用
JFileChooser
从技术上讲,只有当 Web 服务器和 Web 浏览器在物理上运行在同一台计算机上时才“有效”。基本上是由网络服务器显示对话框,而不是网络浏览器。仅当您在本地开发时,这才“有效”,但当您通过独立的网络服务器将网站发布到万维网时,这永远不会“有效”。要通过 HTML 上传文件,您需要
元素,而不是
JFileChooser
。有关如何将其与 JSP/Servlet 一起使用的更多详细信息,请检查 此回答。至于具体问题,我不知道为什么它会弹出两次,但这应该是您在这种特殊情况下最不关心的问题。
There's a major misconception here. First thing, JSP/Java runs at the webserver, produces a bunch of HTML/CSS/JS and sends it to webbrowser. Webbrowser retireves HTML/CSS/JS and interprets/applies/executes it. It doesn't run any line of Java code because it has already been executed on the webserver. Rightclick page in webbrowser and choose View Source. Do you see it? If webserver has done its job right, you should not see any line of Java code in there. The webbrowser namely doesn't understand it. It only understands HTML/CSS/JS.
Using a
JFileChooser
in a JSP scriptlet would technically only "work" when both the webserver and webbrowser runs at physically the same machine. It's basically the webserver which displays the dialog, not the webbrowser. This would only "work" when you're locally developing, but never when you publish the website into world wide web by a standalone webserver.To upload files by HTML, you need an
<input type="file">
element, not aJFileChooser
. For more detail how to use it with JSP/Servlet, check this answer.As to the concrete problem, I have no idea why it pops 2 times, but that should be your least concern in this particular case.