0

I am trying to setup PHP Java Bridge and I am able to invoke Java Method from PHP, But I am unable to get the request object (HttpServletRequest) in my java class. How can I get that ?

My PHP source is something like this.

<?php require_once("http://localhost:8080/PJBridge/java/Java.inc");
echo "PHP JAVA Bridge -- Calculator Test Application <br>"

$Calc= new java("test.math.Calc");
$val1 = 20;
$val2 = 10;
echo "<br> Addition of $val1 and $val2 is";
echo $Calc->add($val1,$val2);


$hello = new java("test.Hello");
echo "<br>";
echo $hello->sayHello("PHP from Java");
echo "<br>";
?>

The Java Class source is like below:-

public class Hello implements ServletRequestAware{


HttpServletRequest request;
public Hello(){ 

}

@Override
public void setServletRequest(HttpServletRequest request) {
    this.request = request;

}



public String sayHello(String name){
    return "Hello  " + name ;
}
}

I am getting the expected result but I need the client IP so I need to access the request object but unable to get that.

3
  • Eh? The HttpServletRequest is supplied directly to your Servlet. Commented Feb 7, 2017 at 22:26
  • May be it's a very poor questions to ask here, but I am getting null request object every time. If I run the application as a web application then I am getting the request object but when the request is initiated by PHP using PHP Java bridge that time it is null. Commented Feb 8, 2017 at 4:37
  • Looking at the code source for both client and server, I cannot really figure out where the client_ip is registered and usable from the servlet. (By client IP, I mean the usable one: $_SERVER['REMOTE_ADDR']) I suppose either you sent the value in your method sayHello(client_ip) or you use java_session(). If you are curious, take a look at this attempt I've made to try it out. Commented Feb 8, 2017 at 12:02

1 Answer 1

0

Cannot answer for the HttpServletRequest,

but if your idea is to transmit the client remote IP to the Java side... you might have to take another path. AFAIK the PHPJavaBridge will only know the IP address of the client (php script calling Java.inc - generally localhost) and not the remote user IP. So even if you fix the HttpServletRequest, you won't really have what you expect. I might be wrong of course...

If I had to do it, I would use either:

Send the remote client ip in the method

<?php
$javaObject = new java('...');
$javaObject->log('username', $_SERVER['REMOTE_ADDR']);

Or set it in the java session

<?php 
java_session('remote_ip', $_SERVER['REMOTE_ADDR']);

I'm not using the Java.inc anymore, so I'm not exactly sure about the syntax for sessions, but If you're looking for infos, have a look to soluble-japha session usage and context. It might help you, syntax differs, but pretty much compatible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.