Results 1 to 11 of 11
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default Passing parameter from JApplet to php file using POST method

    Hello!

    In my applet I have a textfield, where user enters string and submit button. I need to pass that string as parameter to php page, which will do insert to the database. I did passing parameters using GET method, but I have to swich it to POST.

    Here is what I wrote in ActionPerformed method of Submit button. It doesn't work, but I hope somebody can help. It's important for me.

    Java Code:
    try {
        // Construct data
        String data = "http://localhost/subtest.php?upisodg=" + URLEncoder.encode(unos.getText().toString());
    
        // Send data
        URL url = new URL("http://localhost/subtest.php");
        URLConnection conn = url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        wr.close();
    } catch (Exception ex) {
        System.out.println("error");
    }
    I tried to solve this at Pass parameter from JApplet to php file using POST method (I/O and Streams forum at JavaRanch) but without success.

    Any help would be appreciated.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    What does your code send to the server? Can you see what the server receives when you execute your code? That would help you solve your problem.

  3. #3
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    No, I can not see. I use php file on which I write, then it writes to database. Here is how php file looks:

    Java Code:
    <?php
    if(isset($_POST['upisodg']))
    {
    mysql_connect("localhost","root","");
    mysql_select_db("mynewdatabase");
    mysql_query("INSERT INTO testtab (upisodg) VALUES ('".$_POST['upisodg']."')");
    }
    ?>

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    You need to see what the server gets to verify if the code is working.
    If the server gets good data and can figure it out, then it will call your PHP code but that is AFTER it has gotten good data that it knows what to do with.

    Here's an applet that will POST to my server. My servers listens on port 8080 and needs newline at the end of post data.
    You need to experiment with that for your server.
    Java Code:
    import java.applet.*;
    import java.net.*;
    import java.io.*;
    
    
    public class AppletPostProblem extends Applet {
    
       public void init() {
          System.out.println("init started");
          try {
              // Construct data
              String data = "http://localhost/subtest.php?upisodg=XXX\n\n"; //" + URLEncoder.encode(unos.getText().toString());
          
              // Send data
              URL url = new URL("http://localhost:8080/subtest.php");
              URLConnection conn = url.openConnection();
              conn.setDoInput(true);
              conn.setDoOutput(true);
              DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
              wr.writeBytes(data);
              wr.close();
    
              /*may not use this if you do not need to read feedback*/
              DataInputStream is = new DataInputStream(conn.getInputStream());
              String nextline;
              while((nextline = is.readLine()) != null)
                 System.out.println(nextline);
    
              is.close();
    
          } catch (Exception ex) {
              System.out.println("error");
              ex.printStackTrace();
          }      System.out.println("init done");
       }
    }
    /*   Server trace:
    
    run(): 6 connection accepted: Socket[addr=/127.0.0.1,port=2138,localport=8080] TO:0
    HndlReq.run(). Thread 6 started processing.
    6 getRawReq: maxCnt=100000, available=305,  in=java.net.SocketInputStream@e2eec8
    6 getRawReq..end: #char=254, last c=10, maxCnt=99992, available=42
    HandleRequest.run(): request follows: >
    POST /subtest.php HTTP/1.1
    User-Agent: Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_20
    Host: localhost:8080
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive
    Content-type: application/x-www-form-urlencoded
    Content-Length: 42
    < (end of request) <<<
       method: POST
       url: >/subtest.php< (end of url)
    7 getRawReq: maxCnt=42, available=42,  in=java.net.SocketInputStream@e2eec8
    7 getRawReq..end: #char=41, last c=10, maxCnt=42, available=0
    HandleReq.run(): postData >http://localhost/subtest.php?upisodg=XXX
    <
      Content-length=42,  available=0, LOC=-1
    handlePost:  url='/subtest.php',  data='http://localhost/subtest.php?upisodg=XXX
    ', len= 41
    Post data:
    << end post Data
    
    localhost - - [05 38 0-36000000] method=POST URL=null code=405 length=0
    writeString: writing >HTTP/1.0 405 No POST method
    Date: Aug 5, 2010
    Server: NormsHTTPServer/0.1
    Set-Cookie: LastVisit=5 Aug 2010 14:37:38 GMT; expires=Wednesday, 31-Dec-2003 23:59:59 GMT;
    Content-Type: text/html
    Content-Length: 46
    
    <body>
    <h1>405 No POST method</h1>
    </body>
    < (end of writing) <<<
    HndlReq.run(). Thread 6 ended processing.
    
    >>>>>>>>>>>>> Java console: <<<<<<<<<<<<<<
    
    init started
    error
    java.io.IOException: Server returned HTTP response code: 405 for URL: http://localhost:8080/subtest.php
    	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    	at AppletPostProblem.init(AppletPostProblem.java:25)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    init done
    
    */
    Last edited by Norm; 08-05-2010 at 05:05 PM.

  5. #5
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I got messages for http://localhost/subtest.php:

    init started
    init done

    I suppose it is good...?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Did your PHP file get the POSTed data you expected it to get?

    The output to the Java console was for debugging

  7. #7
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I suppose it isn't since it didn't made any recording to database. I didn't get Server trace - I use NetBeans.

    I run this applet in AppletViewer and in Web page, but without any writing to database.

    In HTML file I wrote:

    Java Code:
    <html>
      <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      </head>
      <body>
          <applet code="org.me.test.AppletPostProblem" archive="Test.jar" height="100" width="700"> </applet>
          <form method="post" action="http://localhost/subtest.php"></form></body>
    </html>
    Is it all right?

  8. #8
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    It was passed to php page!

    I put this link http://www.link-elearning.com/linkdl/testpage.php instead of localhost for testing web page methods and it passed using POST method. You can check too.

    I suppose problem is then in my php file on localhost...?

    It looks like this:

    Java Code:
    <?php
    
    
    if(isset($_POST['upisodg']))
    {
    
    mysql_connect("localhost","root","");
    mysql_select_db("mynewdatabase");
    mysql_query("INSERT INTO testtab (upisodg) VALUES ('".$_POST['upisodg']."')");
    
    
    }
    ?>
    What is wrong with it?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Ask at a PHP forum?
    I need PHP books in front of me to do any PHP work...:)

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Sorry, you're mixing in html and php on a java forum.
    Best ask questions about those other languages on their forums.

    I didn't get Server trace - I use NetBeans
    By Server trace, I meant the trace log output from MY SERVER. Something I wrote for testing HTTP connections with. It shows what it receives and what it sends back. Its good for problems like yours where you need to see the traffic.

  11. #11
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Norm, I am deeply grateful. You helped me very much :)

    I succeeded to write php file. It looks like this:

    Java Code:
    <?php
    
    $_POST['upisodg']; 
    print_r($_POST);
    
    if(isset($_POST['upisodg']))
    {
    
    mysql_connect("localhost","root","");
    mysql_select_db("mynewdatabase");
    mysql_query("INSERT INTO testtab (upisodg) VALUES ('".$_POST['upisodg']."')");
    
    
    }
    ?>
    It works just fine now. Point is to add this at the path for passing parameters: ?id=&upisodg=XXX because I had id field in my table.

    Greetings for all!

Similar Threads

  1. Switch Statement/Parameter Passing
    By spmooney@hotmail.co.uk in forum NetBeans
    Replies: 1
    Last Post: 01-06-2010, 02:50 PM
  2. passing parameter to a thread
    By adammyth in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-02-2010, 08:58 PM
  3. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 06:46 AM
  4. Passing short value as parameter
    By javanewbie83 in forum New To Java
    Replies: 16
    Last Post: 07-16-2008, 06:27 AM
  5. passing an enum type as a parameter ??!
    By SCS17 in forum New To Java
    Replies: 11
    Last Post: 07-13-2008, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •