smondal345 smondal345 - 29 days ago 6
Java Question

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

I am using php-script.jar, php-servlet.jar, and JavaBridge.jar to transfer data from from php script to java controller.
But , now I need to send data from java view page to php view page.

I am not able to understand which Servlet is configured to send data from jsp page(hello.jsp) to php script(index.php).

And how to send data from jsp page(hello.jsp) to php script page(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>


dispatcher-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";
}
}

Answer

First check the request method type (get / post), Rewrite the index.php as

<?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>