JFileChooser pop up 2 times

纵饮孤独 提交于 2019-12-12 05:29:38

问题


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?


回答1:


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 a JFileChooser. 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.



来源:https://stackoverflow.com/questions/4502182/jfilechooser-pop-up-2-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!