views:

67

answers:

2

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.

+3  A: 

There is code which eventually needs to touch a graphics card, or at least a working graphics subsystem in Java. If you run this code on a system which doesn't have graphics enabled, then you will throw a HeadlessException.

You are running your code in a Servlet, which is a chunk of code that typically returns a web page. Since the web page is one big string containing all of the proper tags, the web page doesn't need a graphical environment. That string is received by a web browser, and the web browser generally has a graphical environment to display the results.

On your web server, you are asking the web page generator (the servlet) to open up a dialog box. This creates problems for a number of reasons:

  1. The dialog box would show up on the web server, not on the web client.
  2. The web server has only one screen, and the dialog boxes will pop up according to the people browsing the web page.
  3. Since the server potentially might handle many clients simultaneously, the server is tuned to use a minimum amount of resources for each client.
  4. Since the server only returns items to web browsers, the server doesn't need a graphical environment.

All of these points combined means that the servlet won't be configured to have access to a graphics environment, and there will be no opportunity to display a pop-up dialog; because, there is no graphical environment available to display a dialog.

In general, you can't mix swing / awt code with servlets; however, there is a subset of Graphics operations available in both swing and awt which allow image manipulation without needing a graphical environment. This is to ease the development of transforming and building images in a file processing environment, where the images will never be displayed by the program. Think of a .png to .jpg converter as an example, provided it never shows the image, the program could open Image(s) do its work, and close the images without needing a graphics card.

Edwin Buck
See also http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/
trashgod
Thanks for the link. In this case, the proper solution is to use logging, but I guess if you're used to a pop-up dialog, it might be surprising when it's not available.
Edwin Buck
A: 

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:

  1. 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.

  2. 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.

  3. 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.

BalusC
Its true, but I have multiple threads running on server side, so how to show dialog box in same page on client side when particular thread has finished its task.
Nayan Wadekar
I already answered that. I think option 2 in combination with Ajax is the best.
BalusC