Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing Components in Servlet

 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sachin!
Thanks for pointing out that mistake.
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic