I have tried to open a dialog box in Servlet & it opens fine.
But then I tried to achieve same thing in my thread’s run method.
It gaved me following error:
java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Window.<init>(Window.java:431)
at java.awt.Frame.<init>(Frame.java:403)
Below is my code :
JFrame frame = new JFrame("Success Message");
frame.setSize(200, 50);
frame.add(new JLabel("Data uploaded from "+inputFile.getFilename()));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I also tried below code, but failed
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
System.out.println("Headless mode: " + ge.isHeadless());
if(!ge.isHeadless()){
System.setProperty("java.awt.headless", "true");
}
Exception is described as :
Thrown when code that is dependent on a keyboard, display, or mouse is called in an environment that does not support a keyboard, display, or mouse.
Java servlet code runs at webserver, not at webclient (webbrowser). All the webserver does is listening on HTTP requests, producing HTML/CSS/JS responses and sending it to the webclient. All the webclient does is sending HTTP requests and processing the retrieved HTML/CSS/JS responses.
If you execute Swing GUI in the servlet, it will be displayed in the webserver, not in the webclient.
There are basically 3 solutions for this particular problem:
Run Swing GUI code at webclient instead. You can do that in flavor of an Applet or Web Start which get served by a JSP/HTML page.
Use a client side programming/scripting language instead, e.g. JavaScript or ActionScript (Flash). In JavaScript there’s an
alert()function which displays a dialog.Use taglibs like JSTL
<c:if>and/or EL in JSP to render HTML/CSS/JS content conditionally. Can eventually be combinied with solution #2.