0

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Are you sure you want to show the file chooser on server side? Because your code will do so. – Enrique Dec 21 '10 at 19:05
  • @Enrique: likely not. It's one of the most common misconceptions among starters in web development. They need to learn to distinguish between "server side" and "client side" first. What runs where and what exactly happens where. @HoangLam: you may find [this answer](http://stackoverflow.com/questions/1958808/java-web-development-what-skills-do-i-need/1958854#1958854) useful then. – BalusC Dec 21 '10 at 19:08

1 Answers1

5

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, I see my fault. At first, I'd rather use java servlet and add an element to upload a file to server. However, I had some trouble technique, so I changed by using JFileChoose. This leads to the problem that I did not distinguish between "server side" and "client side". :D, I applied that project already but this is a good experience. –  Jan 05 '11 at 01:58