How to forward to or run a Java Swing application from JSP?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akino877
    New Member
    • Nov 2007
    • 37

    #1

    How to forward to or run a Java Swing application from JSP?

    Hello,

    I have a Java Swing application that I would like to be able to forward to or to run it - sorry, I am not sure if I am using the right term - from a JSP page. And I would like for my Java application to run in the same browser that my JSP page ran.

    My JSP code is like this :

    Code:
    <%@ page language ="java" import="java.sql.*" %>
    <html>
    <body bgcolor="pink">
    <form name="f1" method="post">
    <table>
    <tr>
    <td>User Name</td><td><input type="text" name="t1" ></td>
    </tr>
    <td>Password</td>
    <td><input type=""password"" name="t2"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="b1" value="LogIn"></td>
    </tr>
    </table>
    <%
    String user=request.getParameter("t1");
    String pass=request.getParameter("t2");
    
            Connection con = null;
    
         try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
         } catch (ClassNotFoundException cnfe) { 
            System.out.println("cannot load Driver");
         }
         try{
            con=DriverManager.getConnection("jdbc:jtds:sqlserver://nnn.nn.nn.nnn:50305", "akino", "abc123");
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select LName from login where LName = '" + user + "'");
            while (rs.next())
            {
               %>
               <hi>login ok</h1> 
               <jsp:forward page="LoginSuccess.jsp"/>
               <%
            }
         }catch(Exception e1) {}
         %>
    </form>
    </body>
    </html>
    With this code, I can forward to "LoginSuccess.j sp". But
    when I tried :

    Code:
    <%@ page language ="java" import="java.sql.*" %>
    <html>
    <body bgcolor="pink">
    <form name="f1" method="post">
    <table>
    <tr>
    <td>User Name</td><td><input type="text" name="t1" ></td>
    </tr>
    <td>Password</td>
    <td><input type=""password"" name="t2"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="b1" value="LogIn"></td>
    </tr>
    </table>
    <%
    String user=request.getParameter("t1");
    String pass=request.getParameter("t2");
    
            Connection con = null;
    
         try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
         } catch (ClassNotFoundException cnfe) { 
            System.out.println("cannot load Driver");
         }
         try{
            con=DriverManager.getConnection("jdbc:jtds:sqlserver://nnn.nn.nn.nn:50305", "akino", "abc123");
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select LName from login where LName = '" + user + "'");
            while (rs.next())
            {
    	   Runtime.getRuntime().exec("java -jar /NetBeansProjects/NumberAddition/dist/NumberAddition.jar")
               %>
               <hi>login ok</h1> 
               <%
            }
         }catch(Exception e1) {}
         %>
    </form>
    </body>
    </html>
    My Java Swing application ran. But it would display outside the original (JSP page) browser. I wonder if there is a way to make my Java application to run inside my original browser, like when I was forwarding to "LoginSuccess.j sp"?

    Thank you and Best Regards,

    Akino
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Akino,

    I think you can do what you want by creating a trival JSP page, then invoking the outside application as part of your current thread of exectution, so it runs under the same servlet server that your JSPs are running in.

    The sequence will go something like:

    1) Create a StringWriter (s) to catch the application's output.
    2) Load the application's main class (c).
    3) Use reflection to find the "main" entry point in c, call it "m".
    4) Update java.lang.Syste m to replace err and/or out with s.
    5) Invoke m.
    6) Restore java.lang.Syste m.err and/or .out
    7) Dump the accumulated string to your JSPs output stream.
    8) Smile and say "What a good boy am I".

    Or, if that's too complex, use System.execute to run your program as a background program, and collect its output using a pipe. Then retransmit that output to yoru JSP's data stream.

    The second is probably the better solution; the first is just begging for troubles and mischief.

    Cheers and Good Luck!

    Comment

    • Akino877
      New Member
      • Nov 2007
      • 37

      #3
      Hello Oralloy,

      Thank you so much for your help.

      I wonder if you could please tell me if the second solution you gave me would work if I run my JSP page and my Java application on a client side?

      Thank you again,

      Akino

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Akino,

        As far as I know, JSP is a server-side technology.

        Your server may be running on your desktop system, but that shouldn't negatively affect anything. Well, unless you've intentionally coded something to interfere with the smooth behaviour of your application.

        Anyway, I'm off for the rest of the day. Hopefully if I'm not clear, one of the other folks can help you.

        Cheers!

        Comment

        Working...