Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2010, 07:21 PM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
CheatCat is on a distinguished road
Default Java + PHP?
Hello.

I wanna send data from a Java Applet to PHP and from PHP to a Java Applet using only Java and PHP and not other things like servlets and so on and no frame usage if possible. It should also be done dynamically so you don't have to reload the site and the applet and everything every time you want send something from PHP or Java.

I have looked a little on URLConnection that can be used to send stuff from Java to PHP, but does that updates the webpage?

And for sending data from PHP to Java I guess parameters are used, but can I tell Java to read the parameters again without reload?

Can someone please help me?

Last edited by CheatCat; 02-08-2010 at 07:24 PM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-10-2010, 12:47 AM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 459
Rep Power: 1
travishein is on a distinguished road
Default
the url connection would need to work a bit like javascript ajax would, where it initiates the requset to the server to get the server's response, for server or PHP in this case, to client message on the response.

as for updating the web page, an applet is likely not the way to go. there used to be an applet to javascript bridge, but that only rarely worked in some netcape and later mozilla browsers. if you want that true dynamic html type experience, i would recommend not using a java applet, learn how to use a rich javascript toolkit, such as the yahoo user interface (YUI) library.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-10-2010, 10:48 PM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
CheatCat is on a distinguished road
Default
Thank you!

I actually tried to do a online multiplayer game in Java and it have really nothing to do with the other content of the webpage.

So url connection can be used to sending data to php. Now I just need a way to sending back some other data from php to Java.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-17-2010, 05:33 PM
Member
 
Join Date: Jan 2010
Location: Pennsylvania
Posts: 3
Rep Power: 0
treckstar is on a distinguished road
Default
If you ever figure that out, you should let me know. Though I don't think it would be possible to send data back to Java without reloading the applet or application.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-17-2010, 07:17 PM
Member
 
Join Date: Feb 2010
Posts: 4
Rep Power: 0
CheatCat is on a distinguished road
Default
Yeah, I have figured it out! Java can retrieve echos from php.

Using this code really solve my problem:
Code:
public class URLTest2 {

    public static void main( String[] args ) {
	
        try {
		
            String data1 = URLEncoder.encode("first", "UTF-8") + "=" + URLEncoder.encode("Fred", "UTF-8");
            String data2 = URLEncoder.encode("last", "UTF-8") + "=" + URLEncoder.encode("Hamilton", "UTF-8");
            String data = data1 +"&" +data2;
		
            String inLine;
		
            URL u1 = new URL("http://192.168.0.10/test1/test2.php");
            URLConnection uc1 = u1.openConnection();
			
            uc1.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(uc1.getOutputStream());
            out.write(data);
            out.flush();
		
            BufferedReader in = new BufferedReader( new InputStreamReader( uc1.getInputStream() ) );

            while ((inLine = in.readLine()) != null) {
                System.out.println(inLine);
            }
		
            in.close();
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
    }
}
I have found that code on this page:
Writing to a URLConnection object (Sockets and Internet Protocols forum at JavaRanch)
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
dynamically, java, php, urlconnection

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 04:42 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org