Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using Java Swing in a HttpServlet

Options
  • 11-01-2009 2:22am
    #1
    Registered Users Posts: 677 ✭✭✭


    Hey,

    What I'm trying to do is whip up a really quick display to demonstrate what my servlet is doing. It is only for demonstration purposes and has no affect on the functions of the server. This is for my final year project so that I can demonstrate exactly what is going on behind the scenes when my servlet is running.

    Therefore I want a JFrame to appear on my laptop when the client connects to the HttpServlet running in Apache Tomcat 5.5 which I have up and running on my laptop. I only want to display an image and some text and update them as the app progresses.

    I have tried spawning another thread to display all this in a JFrame but for some reason it never appears when I communicate with the servlet. I have spent the last few hours googling the problem and havent found any reason why it wont appear. I dont even get any errors/exceptions in Tomcat, the servlet completes as if the Swing code has executed normally. I even double checked that i called the run() function... :o

    Surely if I can run a Swing application on my laptop then the server running on my laptop should be able to run it also? Wouldnt it just be invoking the JRE, the same one I use for normal apps?

    Any ideas/experience?
    Thanks


Comments

  • Registered Users Posts: 197 ✭✭pauldiv


    Sounds like the JFrame is maybe running but just not showing.
    There is a method for making a JFrame show on screen.
    I had a problem like this a few years ago and after applying the show method the frame appeared.


  • Registered Users Posts: 25 DeJaMo


    Do you have to use SWING?? Can you not print out server logs to see what the servlet is doing.

    Using SWING with J2EE is very bad practice! Put it should be possible to do in terms of experimentation.

    Can you post your JFrame code and how you are calling it...


  • Registered Users Posts: 677 ✭✭✭M450


    Hey,

    I want to show graphically what I am doing as it involves image processing. Is there anything else I could use other than Swing to do this?

    This is basically how I call the Swing code:
    public class CameraServer extends HttpServlet {
    
        @Override
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                   
           // Initialise stuff for my servlet
                    
            Display display = new Display();
            display.run();
            
            // Do stuff for my servlet
        }
    }
    
    class Display implements Runnable {
        
        public Display() {
        }
        
        public void run() {
            JFrame display = new JFrame("Camera Server");
    
            try {  // Use the system look and feel for the swing application
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException e) {
            } catch (InstantiationException e) {
            } catch (IllegalAccessException e) {
            } catch (UnsupportedLookAndFeelException e) {}
     
            SwingUtilities.updateComponentTreeUI(display);
            
            // Add JTextArea and JComponent objects to show text and an Image
            
            display.setPreferredSize(new Dimension(800, 600));
            display.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            display.pack();
            display.setLocationRelativeTo(null);
            display.setVisible(true);
        }    
    }
    

    as far as I know setVisible(true) is the way to make a JFrame appear. I dont see any other methods which may force it to appear.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Only really started doing servlets recently enough but I wouldn't have thought you'd be able to do that. Isn't a servlet kind of like a one shot, it runs the code then gets thrown away. So wouldn't your program end, and therefore the JFrame close, in the space of a few micro seconds?


  • Registered Users Posts: 25 DeJaMo


    When you say display.run() you are not creating a new Thread. You are just calling the run() method on the object Display...you need to say something like...

    Display display = new Display();
    Thread t = new Thread(display);
    t.start();


  • Advertisement
  • Registered Users Posts: 677 ✭✭✭M450


    DeJaMo wrote: »
    When you say display.run() you are not creating a new Thread. You are just calling the run() method on the object Display...you need to say something like...

    Display display = new Display();
    Thread t = new Thread(display);
    t.start();

    Good man, that was a bit stupid of me. I've done quite a bit on multithreading over the last year... but still no luck! Initially I tried creating the JFrame object in the doPost() function and it wouldn't work, which is why I decided to go with creating it in a new thread.
    Anima wrote:
    Only really started doing servlets recently enough but I wouldn't have thought you'd be able to do that. Isn't a servlet kind of like a one shot, it runs the code then gets thrown away. So wouldn't your program end, and therefore the JFrame close, in the space of a few micro seconds?

    Well if I spawn a new thread then shouldn't that keep running?


  • Registered Users Posts: 1,998 ✭✭✭lynchie


    What is your servlet container and more importantly are you running it as a windows service? If so, unless you have enabled the service to interact with the desktop, then any windows will appear in the service's own hidden desktop window and you wont be able to see it.


  • Registered Users Posts: 677 ✭✭✭M450


    lynchie wrote: »
    What is your servlet container and more importantly are you running it as a windows service? If so, unless you have enabled the service to interact with the desktop, then any windows will appear in the service's own hidden desktop window and you wont be able to see it.

    I just installed Tomcat 5.5 a few weeks ago so I'm not very familiar with it. I believe it is running as a windows service. (I basically open up an application 'Monitor Tomcat' and select 'start service' to run it).

    What do you mean by servlet container? I place the .class files in the Tomcat root directory so they are run when I access localhost with a POST, then the doPost(...) function of the HttpServlet is called. Thats about as much as I understand... everything works perfectly except for the Swing stuff.

    So do you know how I could enable this service to interact with the desktop?
    By the way this is my web.xml file, I believe this gives Tomcat info about running the servlet but all I specify is the location. Is there something I can add here to fix it?
    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
      <servlet>
        <servlet-name>project</servlet-name>
        <servlet-class>cameraserver.CameraServer</servlet-class>
      </servlet>
      
      <servlet-mapping>
        <servlet-name>project</servlet-name>
        <url-pattern>/project</url-pattern>
      </servlet-mapping>
    </web-app>
    

    Thanks for everyones input so far.


  • Registered Users Posts: 677 ✭✭✭M450


    I've figured it out.

    In 'Apache Tomcat Properties', in the 'Log On' tab, you can check a box to allow the service to interact with the Desktop.

    Thanks for your help, I really had no clue what direction to go in solving this.


  • Registered Users Posts: 677 ✭✭✭M450


    I just thought I'd update this thread as I hate trawling the web and finding people with the same problems but no solutions.

    If you want a GUI to appear from a servlet run from within the tomcat server this warning will appear saying:
    A program can't display a message on your desktop

    The program may need information or permission to complete a task.

    -> Show me the message
    -> Remind me in a few minutes

    By selecting 'Show me the message' the desktop completely changes to a new one with the GUI there.
    I think it may only happen on Vista.

    I tried going to Control Panel > Administrative Tools > Services.
    If you right click on tomcat and go to properties > Log on you can enter your login details for your windows account and apparently it will run as if you executed it.
    But this still doesn't allow it to draw the GUI to your desktop.

    As security isn't an issue for me I decided to just run tomcat5.exe myself instead of starting the tomcat service whenever I wanted to test my server.
    It should be located in C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin depending on your version and where you installed it. Just run that exe and the server will be up and running.

    Now when I run my phone application a window pops up on my desktop showing what the server is doing.


  • Advertisement
  • Registered Users Posts: 1,998 ✭✭✭lynchie


    M450 wrote: »
    I just thought I'd update this thread as I hate trawling the web and finding people with the same problems but no solutions.

    If you want a GUI to appear from a servlet run from within the tomcat server this warning will appear saying:



    By selecting 'Show me the message' the desktop completely changes to a new one with the GUI there.
    I think it may only happen on Vista.

    I tried going to Control Panel > Administrative Tools > Services.
    If you right click on tomcat and go to properties > Log on you can enter your login details for your windows account and apparently it will run as if you executed it.
    But this still doesn't allow it to draw the GUI to your desktop.

    As security isn't an issue for me I decided to just run tomcat5.exe myself instead of starting the tomcat service whenever I wanted to test my server.
    It should be located in C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin depending on your version and where you installed it. Just run that exe and the server will be up and running.

    Now when I run my phone application a window pops up on my desktop showing what the server is doing.

    The whole interact with the desktop approach only works for XP. In vista, all services run under a session id of 0 where as all interactive apps run under a session id > 1. Therefore a service can never display anything on the desktop, as there could be multiple desktops / users running on a machine.


Advertisement