1

I am trying to send an input from PHP server to Java server using "GET"

My request sent has no issues Request and Response

Both the server are connected, I am receiving a response "null Please enter a number"

where null is an output for

out.println(inputdata);

My code

HTML

<html>
<head>
<meta charset="UTF-8">
<title>Cloud Computing</title>

</head>
<body>
<form action="url.php" method="get">
Input: <input type="text" name="inputdata" ><br>
       <input align="center" type="submit">
</form>

</body>
</html> 

PHP

<html>
 <head>
 <meta charset="UTF-8">
  <title>PHP Test</title>
 </head>
 <body>
 <form action="url.php" method="get">
Input: <input type="text" name="inputdata" ><br>
<input align="center" type="submit">
</form>
<font face="century gothic" size="20px">
    <center> </br></br>
    <?php 

        echo "Query for:";
        echo $_GET["inputdata"]; 
        //echo $_POST["inputdata"];
         $inputdata = $_GET["inputdata"]; 
         $url = "http://localhost:8080/CloudComputingProj/Cloudpi";

    $post_params_s = ["inputdata"=>$inputdata];

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt ( $ch, CURLOPT_POST          , TRUE ) ;
    curl_setopt ( $ch, CURLOPT_POSTFIELDS    , $post_params_s ) ;
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE ) ; 
    curl_exec($curl);
    curl_close($curl);
?></center>
</font>

 </body>
</html>

Java Server

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            System.out.println("Inside Service");

            System.out.println(request.getQueryString());
            if(request.getMethod().equals("GET")){
                doGet(request, response);
            }
        }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
           //   System.out.println(request.getParameterMap());     //Returns null
            InputStream requestBodyInput = request.getInputStream();
            String inputdata = request.getParameter("inputdata");
            System.out.println(inputdata);
            //response.getWriter().append(" \n Served at: ").append(request.getContextPath());
            response.setContentType("text/html");
             PrintWriter out = response.getWriter();
             //out.println("Hello World!");
             out.println(inputdata);
            // System.out.println(request.getQueryString());
             if (request.getParameter("inputdata") == null) {
                 out.println("Please enter a number");
             } else {
                 out.println("Hello <b>"+request. getParameter("input")+"</b>!");
             }

        }

I am not finding anything here for "request", none of the standard methods give me anything other than 'null' kindly help ! I am new to this.

Pandeyji
  • 15
  • 7

2 Answers2

0

You are checking a non exist input

if (request.getParameter("input") == null)
                          ^^^^^ 

Which I think that is should be as follows:

if (request.getParameter("inputdata") == null)

UPDATE

you are posting the request to curl, while you are checking for GET request type.

if(request.getMethod().equals("GET"))

you need either to check if the request is post and then get the POST request body or setting your curl to send a GET request with a simple query string.

Community
  • 1
  • 1
hassan
  • 7,812
  • 2
  • 25
  • 36
  • Thank you Hassan for the quick reply. That "input" is by mistake, it should be inputdata. Nevertheless the request fails at service itself. It does not give me any Parameter or Query String. – Pandeyji Mar 10 '17 at 07:28
  • Just did , thanks for noting. Is there anything else you found fishy in the java server code? – Pandeyji Mar 10 '17 at 07:41
  • In protected void service, the "request" I am getting is does not return anything other than request.getMethod() For e.g. System.out.println(request.getQueryString()); gives me a 'null' where it should be giving me the query string i.e. the url of my php server with the input variables. The doGet method is inconsequential if the request itself is received incomplete. And neither getParameter() or getQueryString() works. – Pandeyji Mar 10 '17 at 08:00
  • Please check the image named "Request and Response". Maybe you find something I don;t. – Pandeyji Mar 10 '17 at 08:01
  • `request.getQueryString()` will read the query string of that url : `http://localhost:8080/CloudComputingProj/Cloudpi` not the url `localhost/url.php?inputdata=.....` , try to send the url as follows: `$url = "http://localhost:8080/CloudComputingProj/Cloudpi?inputdata=$inputdata";` – hassan Mar 10 '17 at 08:06
0

try to modify php code to $url = "http://localhost:8080/CloudComputingProj/Cloudpi?inputdata=".$inputdata Also you can debug your java side by typing the get request in any browser http://localhost:8080/CloudComputingProj/Cloudpi?inputdata=your-encoded-param

PWC
  • 266
  • 3
  • 11
  • This doesn't work as this is my Java server URL (Which I am calling from PHP) The inputdata is being sent by the request as query string and parameter (Check Image). – Pandeyji Mar 10 '17 at 07:39
  • @Pandeyji the request image you post is the request to the php server, not to java server.You can call you java server directly as I said. If your java server response correctly, then there is a bug in your php code. – PWC Mar 10 '17 at 07:56
  • Are you suggesting that I call my java server directly from the HTML instead of calling a php ? – Pandeyji Mar 10 '17 at 08:04
  • @Pandeyji yes, for testing purpose. – PWC Mar 10 '17 at 08:07
  • Oh yes I get the inputdata now Output: Webpage: 2131 Hello null! Java Console: Inside Service /CloudComputingProj java.util.Collections$3@20d3e8f0 2131 2131 – Pandeyji Mar 10 '17 at 08:10
  • But again the request as seen in the image seems to be correct. I am not sure why is it behaving this way – Pandeyji Mar 10 '17 at 08:14
  • Perfect it works !! Thanks a lot ...being breaking my head on this for hours now :X – Pandeyji Mar 10 '17 at 08:22