Solved

accessing php session variable in java

Posted on 2012-04-03
4
1,117 Views
Last Modified: 2012-04-08
hello how do i access the php session variable in java?

java code
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

/**
 * Send a POST request using URLConnection
 * (Consider using HttpURLConnection or HttpsURLConnection)
 */

/**
 *
 * @author theodore werntz ii
 */
public class WebClientPHP {
    
public static void main(String[] args) {
        
        try {
            // Prepare data
            String data = "UserId" ; // THIS IS THE ELEMENT I NEED TO GET FROM PHP SESSION 
    
            // Prepare connection
            URL url = new URL("http://127.0.0.1/subtest.php?name=" 
                    + data );
            URLConnection connection = url.openConnection();
            connection.setDoOutput(true);  // Needed to write to a URLConnection
            
            // Get the response
            BufferedReader reader = new BufferedReader(
                                          new InputStreamReader(
                                                connection.getInputStream()));
            // Print the response
            String line;
            while ((line = reader.readLine()) != null)
                System.out.println(line);
            
            reader.close();  // Close the reader
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Open in new window


php code
<applet code=ComputerInfoApplet.class width="400" height="400"
ARCHIVE="ComputerInfoSepFiles.jar">
<param name="mayscript" value="true">
<param name="scriptable" value="true">
</applet>

Open in new window



so how will i pass a variable from php to java applet and how will i access the variable in java that was passed to the applet?

thanks in advance for your help!

Theodore E. Werntz II
0
Comment
Question by:only1wizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37801704
PHP can expose any variable value in the browser output stream with a simple echo statement, and can also make a one-time echo that terminates the script with die().  If your Java program can call a PHP script and read the browser output your PHP script could expose the contents of $_SESSION["userid"] with something as simple as this.
<?php
session_start();
if (!empty($_SESSION["userid"]))
{
    die($_SESSION["userid"]);
}
else
{
    die('NO USER ID');
}

Open in new window

0
 

Author Comment

by:only1wizard
ID: 37801787
but that doesnt help the problem of accessing the php $_SESSION variable in java. I understand that accessing the variable by $_SESSION and or echo $VARIABLE.

what i dont understand is how to access the php variable in java?

thanks in advance for your help!
0
 

Accepted Solution

by:
only1wizard earned 0 total points
ID: 37802541
i got it working

here is the php code
<?php 
// set session variables
if (!isset($_SESSION)) {
  session_start();
}
$_SESSION['first'] = '1';
$_SESSION['second'] = '2';
$_SESSION['third'] = '3';

?>

Open in new window


html code
<applet code="MyApplet.class" height="100" width="400" ARCHIVE="MyApplet.jar">
<param name="first" value="<?php echo $_SESSION['first']; ?>" />
<param name="second" value="<?php echo $_SESSION['second']; ?>"  />
<param name="third" value="<?php echo $_SESSION['third']; ?>"  />
</applet>

Open in new window


java code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package myapplet;

/**
 *
 * @author theodore werntz ii
 */
import java.applet.*;    
import java.awt.*;

public class MyApplet extends Applet  {

    /**
     * @param args the command line arguments
     */
    
    public void init() {
  add(new Label(getParameter("first")));
  add(new Label(getParameter("second")));
  add(new Label(getParameter("third")));
  }
    
}

Open in new window

0
 

Author Closing Comment

by:only1wizard
ID: 37820881
ive worked out a solution for cross language communication with sessions
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to count occurrences of each item in an array.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question