9 Replies Latest reply: Jul 19, 2012 6:41 PM by 949428 RSS

    Post parameters to a JSP from an applet

    949428
      Hello,

      Is there a way to send a post request to a JSP page from an applet ? Here is the scenario I am looking for :

      In Test.jsp, I invoke an applet. The applet should then do a POST request to the JSP. Currently, I am able to send a JSP request by appending parameters to the query string as

      getAppletContext().showDocument(new URL(getDocumentBase(),"Test.jsp?querystring")); in the applet. But this will be showing the parameters in the URL. I would like to have the parameters sent as "POST". I tried doing something like

      String data = "All URL encoded parameter names and values";
      URL postURL = new URL("Test.jsp");
      conn = (HttpURLConnection)postURL.openConnection();
      conn.setRequestMethod("POST");
      conn.setDoOutput(true);
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
      wr.write(data);
      wr.flush();
      wr.close();

      But how do I pass the control back to the JSP ? Any thoughts ?
        • 1. Re: Post parameters to a JSP from an applet
          EJP
          You've almost done it. Now get the response code. That will send the output to the server and wait for the response.
          • 2. Re: Post parameters to a JSP from an applet
            949428
            I tried getting the response code but I still don't get the parameters posted to the jsp. Here is what I am looking the solution for:

            In the applet

            public class Detection extends Applet {

            .........

            String data = "osName=" + URLEncoder.encode(System.getProperty("os.name"),"UTF-8");

            URL url = new URL("http://hostname:80/test.jsp");
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the response code
            int code = conn.getResponseCode();
            System.out.println("Response code of the object is "+code);
            if (code==200) {
                 System.out.println("OK");
            }

            wr.close();
            conn.disconnect();

            ........
            }


            In Test.jsp

            ----------------

            String osName = request.getParameter("osName");

            <object>
            <param name="type" value="application/x-java-applet" />
            <param name="code" value="Detection.class" />     
            <comment>
            <embed type="application/x-java-applet" code="Detection.class" width="1" height="2" />
            <noembed>No osName detected</noembed>
            </comment>
            </object>


            --------------

            The osName is returned as null even after the applet detection happens. Any thoughts ?
            • 3. Re: Post parameters to a JSP from an applet
              gimbal2
              946425 wrote:
              The osName is returned as null even after the applet detection happens. Any thoughts ?
              I see you close the writer AFTER you get the response code - you will probably want to try closing it BEFORE you get the response.

              If that doesn't work then you'll probably want to use a HTTP sniffer to see what the HTTP request being sent looks like.
              • 4. Re: Post parameters to a JSP from an applet
                949428
                How to get retrieve the parameter in the JSP ?

                I can see the response being written to the console when I do the following in the applet:

                // Get the response
                               BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                               String line;
                               while ((line = rd.readLine()) != null) {
                               System.out.println(line);
                               }
                               rd.close();

                But I would like to be able to read/process the parameters in the JSP. Any thoughts ? Is there a sample JSP/applet that I can look at on how to post to a JSP from an applet and how to retrieve the post parameters in the JSP ?
                • 5. Re: Post parameters to a JSP from an applet
                  gimbal2
                  It will work once a proper HTTP request is sent.
                  • 6. Re: Post parameters to a JSP from an applet
                    EJP
                    conn.setRequestMethod("POST");
                    • 7. Re: Post parameters to a JSP from an applet
                      949428
                      Thanks for all your responses. I posted the request and still couldn't get it working. Here is my code

                      osDetection.jsp:
                      ----------------------

                      <%@ page contentType="text/html; charset=UTF-8" %>

                      <% // Get all the request parameters
                      String userOSVersion = request.getParameter("osVersion") == null ? "" : request.getParameter("osVersion");
                      String detect = request.getParameter("detect") == null ? "" : request.getParameter("detect");
                      %>



                      <% if(detect.equals("")) { %>
                                <span>detect os version</span>
                      <% } %>

                      <%     if("os".equals(detect)) { %>

                      <div align="center">
                      <object width="250" height="75">
                      <param name="type" value="application/x-java-applet" />
                      <param name="code" value="Detection.class" />     
                      <comment>
                      <embed type="application/x-java-applet" code="Detection.class" width="1" height="2" />
                      <noembed>No OS detected</noembed>
                      </comment>
                      </object>
                      </div>     
                      <% } %>

                      OS Version: <%=userOSVersion%>
                      Detect:          <%=detect%>

                      Detection.java
                      ---------------------

                      import java.applet.*;
                      import java.io.OutputStreamWriter;
                      import java.net.HttpURLConnection;
                      import java.net.URL;
                      import java.net.URLEncoder;
                      import java.io.BufferedReader;
                      import java.io.InputStreamReader;

                      public class Detection extends Applet {

                           public void init() {
                                
                                HttpURLConnection conn = null;
                                
                                try {
                                
                                     String data = "osVersion=" + URLEncoder.encode(System.getProperty("os.version"),"UTF-8");
                                     URL postURL = new URL("http://localhost:8080/detection/osDetection.jsp");
                                     System.out.println("posting to the url :");
                                     conn = (HttpURLConnection)postURL.openConnection();
                                     conn.setRequestMethod("POST");
                                     conn.setDoOutput(true);
                                     System.out.println("connection open");
                                     
                                     OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                                     wr.write(data);
                                     wr.flush();
                                     wr.close();
                                     
                                     // Get the response code
                                     int code = conn.getResponseCode();
                                     System.out.println("Response code of the object is "+code);
                                     if (code==200) {
                                     System.out.println("OK");
                                     }
                                     
                                     // Get the response
                                     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                                     String line;
                                     while ((line = rd.readLine()) != null) {
                                     System.out.println(line);
                                     }
                                     rd.close();
                                     
                                     conn.disconnect();
                                     
                                } catch(Exception ex) {
                                     System.out.println("Error:" + ex.getMessage());
                                }
                                          
                      }
                      }

                      Am I missing something here ? I see the response being written to the Java console but how do I transfer the control back to JSP to do further processing in the JSP ?

                      Thanks much in advance.
                      • 8. Re: Post parameters to a JSP from an applet
                        EJP
                        What are you expecting to see? Those last two lines? OS Version: and Detect:?
                        • 9. Re: Post parameters to a JSP from an applet
                          949428
                          Thats right. I am mainly looking for the following variable:

                          OS Version: <%=userOSVersion%>

                          Depending on this variable value, I have varied messaging.