2016-11-11 63 views
2

我使用php-script.jar,php-servlet.jar和JavaBridge.jar从php脚本到Java控制器。 但是,现在我需要将数据从java查看页面发送到php查看页面。通过使用php-script.jar,php-servlet.jar和JavaBridge.jar集成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"; 
    } 
} 

回答

0

首先检查请求方法类型(GET/POST),重写在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>