Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

jakarta ee - How to share session between php app and Java EE app?

We may replace a PHP app with a Java EE app, but the problem is we wanna replace the modules one by one, which means two apps would co-exist and communicate with each other.

So is it possible to share the user session between the 2 apps? Or use a cookie to solve the problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sharing a regular Java EE session with PHP can be done very efficient and fast with PHP-java-bridge.

This solution offer superior performance over using a database as it does not generate any disk IO. It also does not need any changes on the PHP webserver or Java EE server. Just add some code, configure and you're done.

Setting up the php-java-bridge can be confusing, but if you know how to do it, it's only a 10-minute job. As I just did a proof of concept my self I can hand you the cookbook solutions:

  1. Download PHP-java-bridge files. I downloaded JavaBridgeTemplate610.zip (for the needed jar files) and php-java-bridge_6.1.0_documentation.zip for the needed examples, php include file and sample code (session sharing!).

  2. Add the "JavaBridge.jar", "php-script.jar" and "php-servlet.jar" to you're webapp by putting it in the "/WEB-INF/lib/" of you're Java EE server.

  3. Add a "test.jsp" to you're Java EE servers "/web" directory:

    <HTML>
    <TITLE>PHP and JSP session sharing</title>
    <BODY>
    <%
    
    javax.servlet.http.HttpSession $session = request.getSession();
    if($session.getAttribute("counter")==null) {
      $session.setAttribute("counter", new java.lang.Integer(1));
    }
    
    int $counter = ((java.lang.Integer)$session.getAttribute("counter")).intValue();
    out.println ("HttpSession variable "counter": " + $counter + "<br>");
    java.lang.Integer $next = new java.lang.Integer($counter+1);
    session.setAttribute("counter", $next);
    %>
    <a href="http://127.0.0.1/test.php">PHP page</a>
    </BODY>
    </HTML>
    
  4. Configure the JavaBridge servlet so it can be used by PHP to communicate to the Java EE server. Just add the following lines to you're web.xml:

    <servlet>
      <servlet-name>PhpJavaServlet</servlet-name>
      <servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>PhpJavaServlet</servlet-name>
      <url-pattern>*.phpjavabridge</url-pattern>
    </servlet-mapping>
    
  5. Add a file named "test.php" to you're webserver root and make sure you edit the JAVA_HOSTS and JAVA_SERVLET to correctly point to the Javabridgeservlet as configured in the web.xml.

    <?php
    define ("JAVA_HOSTS", "127.0.0.1:8084");
    define ("JAVA_SERVLET", "/JavaBridge.phpjavabridge");
    
    require_once("java/Java.inc");
    $session = java_session();
    ?>
    
    <HTML>
    <TITLE>PHP and JSP session sharing</title>
    <BODY>
    <?php
    if(is_null(java_values($session->get("counter")))) {
      $session->put("counter", 1);
    }
    $counter = java_values($session->get("counter"));
    print "HttpSession variable "counter": $counter<br>
    ";
    $session->put("counter", $counter+1);
    ?>
    <a href="http://127.0.0.1:8084/test.jsp">JSP page</a>
    </BODY>
    </HTML>
    
  6. Install the needed "java.inc" php include file. You will find the file in the downloaded "php-java-bridge_6.1.0_documentation.zip" in the src.zip. Copy the "java.inc" file in the "/java" directory (just this one php file!).

  7. Restart Application server

  8. Start the test.php script (for example goto: http://127.0.0.1/test.php)

If you click on the links to the jsp and php file back, you will notice that the counter shares the Java session between both JSP and PHP scripts!

In order to share the same JSession cookie in a JSP/Servlet and PHP they both need to run on the same domain name (also make sure that JAVA_HOSTS is the PHP file uses the same domain name!).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...