Results 1 to 11 of 11
- 08-05-2010, 03:01 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
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.
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.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"); }
Any help would be appreciated.
- 08-05-2010, 03:17 PM #2
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.
- 08-05-2010, 03:23 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
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']."')"); } ?>
- 08-05-2010, 04:01 PM #4
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 04:05 PM.
- 08-05-2010, 04:16 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
I got messages for http://localhost/subtest.php:
init started
init done
I suppose it is good...?
- 08-05-2010, 04:21 PM #6
Did your PHP file get the POSTed data you expected it to get?
The output to the Java console was for debugging
- 08-05-2010, 04:33 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
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:
Is it all right?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>
- 08-05-2010, 04:39 PM #8
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
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:
What is wrong with it?Java Code:<?php if(isset($_POST['upisodg'])) { mysql_connect("localhost","root",""); mysql_select_db("mynewdatabase"); mysql_query("INSERT INTO testtab (upisodg) VALUES ('".$_POST['upisodg']."')"); } ?>
- 08-05-2010, 04:44 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Ask at a PHP forum?
I need PHP books in front of me to do any PHP work...:)
- 08-05-2010, 04:47 PM #10
Sorry, you're mixing in html and php on a java forum.
Best ask questions about those other languages on their forums.
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.I didn't get Server trace - I use NetBeans
- 08-05-2010, 05:27 PM #11
Member
- Join Date
- Apr 2010
- Posts
- 57
- Rep Power
- 0
Norm, I am deeply grateful. You helped me very much :)
I succeeded to write php file. It looks like this:
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.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']."')"); } ?>
Greetings for all!
Similar Threads
-
Switch Statement/Parameter Passing
By spmooney@hotmail.co.uk in forum NetBeansReplies: 1Last Post: 01-06-2010, 01:50 PM -
passing parameter to a thread
By adammyth in forum Threads and SynchronizationReplies: 1Last Post: 01-02-2010, 07:58 PM -
passing a parameter
By aarthi2learn in forum AWT / SwingReplies: 4Last Post: 12-22-2008, 05:46 AM -
Passing short value as parameter
By javanewbie83 in forum New To JavaReplies: 16Last Post: 07-16-2008, 05:27 AM -
passing an enum type as a parameter ??!
By SCS17 in forum New To JavaReplies: 11Last Post: 07-13-2008, 01:44 PM


LinkBack URL
About LinkBacks

Bookmarks