Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Swing Components in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Swing Components in Servlet" Watch "Swing Components in Servlet" New topic
Author

Swing Components in Servlet

dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi All,

I have a very basic question. When i use any Swing components inside my Servlet, after the execution the Tomcat server gets shut down. Whereas if i use the components inside applet and then use in the Servlet the server works fine. Can anybody explain me why does it behave like that?

Here is the code which caused the server to shutdown.

import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;

public class GreeterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
JFrame testFrame = new JFrame("Tester");
JButton testButton = new JButton("Hai");
testFrame.add(testButton);
testFrame.setSize(350,350);
testFrame.setVisible(true);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Please note that, the code provided above is only to understand the concept.

Thanks in Advance,
dinesh
Sachin Dimble
Ranch Hand

Joined: Dec 07, 2005
Posts: 100
Hi Dinesh,
Just comment out the

testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

and it will work fine. now the question is why?
think on this, still if you have doubt get back here!

-Saaaaaaaaaaaachin.
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Thanks Sachin!
Thanks for pointing out that mistake.
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi Sachin,

I have commented that line to avoid shutting down. But when i shut down the server properly, the server is not shutting down and throws an Exception that FAILED TO SHUTDOWN and gets hung.

Thanks in Advance,
dinesh.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can't host swing components in a servlet.
Servlets are run up on a server.
They deliver content (usually HTML and Javascript) to browsers.

If you want to display Swing objects on the client's machine, you'll need to look at applet or Java Web Start technology.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Sachin Dimble:
.. Just comment out the
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
and it will work fine....


Sachin Dimble,
If you're not familiar with servlets or don't understand the issue that the original poster is having trouble with, please say so before trying to answer their question. Wrong or misleading answers are worse than no answer at all.
Sachin Dimble
Ranch Hand

Joined: Dec 07, 2005
Posts: 100
Hey Ben cool!
I know it's not possible to stick swing or awt components on servlets
but if try to do same it will work but logicaly it's incorrect as these components never get delivered to client side, they are running under server jvm as an seperate frame, now if i close this frame which will lead to System.exit will kill the container jvm......shut down tomact....!
Dinesh's problem was why tomcat gets shut down if he closed swing application.
I was letting him think about above concept.

still if Dinesh gets mislead because of my post......I am realy SORRY.

-Saaaaaaaaaaaachin.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

dinesh,

Good point.
System.exit is something that should never be called from within a web app.

Thanks for helping out.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
and the reason the appserver doesn't shut down when you don't call it is that the Swing stuff will have started an event dispatcher thread which never gets killed.

As Ben says, NEVER mix Swing (or AWT, or SWT) with serverside components (Servlets, JSP, EJB, webservices, etc.).
You may get lucky and get something that appears to work (especially if you only use non-visual elements of the APIs) but you're inviting trouble.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing Components in Servlet