簡體   English   中英

通過使用php-script.jar,php-servlet.jar和JavaBridge.jar集成Java和Php

[英]Integration Java and Php by using php-script.jar, php-servlet.jar, and JavaBridge.jar

我正在使用php-script.jar,php-servlet.jar和JavaBridge.jar將數據從php腳本傳輸到Java控制器。 但是,現在我需要將數據從Java視圖頁面發送到php視圖頁面。

我不明白哪個Servlet被配置為將數據從jsp頁面(hello.jsp)發送到php腳本(index.php)。

以及如何將數據從jsp頁面(hello.jsp)發送到php腳本頁面(index.php)?

在web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            15
        </session-timeout>
    </session-config>

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/jsp/pagenotfound.jsp</location>
    </error-page>

    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/jsp/500exception.jsp</location>
    </error-page>

   <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.php</welcome-file>
</welcome-file-list>

    <listener>
        <listener-class>php.java.servlet.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>PhpJavaServlet</servlet-name>
        <servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>PhpCGIServlet</servlet-name>
        <servlet-class>php.java.servlet.fastcgi.FastCGIServlet</servlet-class>
        <init-param>
            <param-name>prefer_system_php_exec</param-name>
            <param-value>On</param-value>
        </init-param>
        <init-param>
            <param-name>php_include_java</param-name>
            <param-value>Off</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>PhpJavaServlet</servlet-name>
        <url-pattern>*.phpjavabridge</url-pattern> 
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>PhpCGIServlet</servlet-name>
        <url-pattern>*.php</url-pattern>
    </servlet-mapping>


</web-app>

的index.php

<?php
require_once("java/Java.inc");


echo "<pre>";
//echo java("java.lang.System")->getProperties(); 
print_r(java("java.lang.System")->getProperties());
?>
<form action="helloPage.do" method="POST">
    <input id="text1" name="name" type="text">
    <input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['name'])){
    echo $_POST['name'];
}
?>

為hello.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <p>I am ${name}</p>
        <form action="index.php" method="POST">
            <input type="text" name="name">
            <input type="submit" value="send">
        </form>
    </body>
</html>

調度員servlet.xml中

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <!--scan the given package for controllers--> 
    <context:component-scan base-package="com.abcd.controllers"/> 
    <!--support for @Controller and @RequestMapping-->
    <mvc:annotation-driven />
    <!--support for general annotations such as @Required, @Autowired, @PostConstruct, and so on.-->
    <context:annotation-config/>

     <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
    </bean>

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>
</beans>

HomeController的

@Controller
public class HomeController {
    static{
        System.out.println("In Home");
    }

    @RequestMapping(value={"helloPage.do"}, method = {RequestMethod.POST})
    public String helloJsp(HttpServletRequest req, ModelMap map){
        System.out.println("In helloPage");
        String name = req.getParameter("name");
        map.put("name", name);
        System.out.println("Going to search hello");
        return "hello";
    }
}

首先檢查請求方法的類型(獲取/發布),將index.php重寫為

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     // The request is using the POST method

    if(isset($_POST['name'])){
       echo $_POST['name'];
    }

}else{
    require_once("java/Java.inc");
    echo "<pre>";
   //echo java("java.lang.System")->getProperties(); 
    print_r(java("java.lang.System")->getProperties());
}
?>
<form action="helloPage.do" method="POST">
    <input id="text1" name="name" type="text">
    <input type="submit" value="Submit">
</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM