Results 1 to 5 of 5

Thread: Java + PHP?

  1. #1
    CheatCat is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    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.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    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.

  3. #3
    CheatCat is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    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.

  4. #4
    treckstar is offline Member
    Join Date
    Jan 2010
    Location
    Pennsylvania
    Posts
    3
    Rep Power
    0

    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.

  5. #5
    CheatCat is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    Yeah, I have figured it out! Java can retrieve echos from php.

    Using this code really solve my problem:
    Java 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)

Tags for this Thread

Posting Permissions

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