简体   繁体   中英

How to post parameters to java servlet using curl from php page?

I am working on a web application using Netbeans. My PHP page consists of a form which contains username and password which i have to validate in PHP using MySQL and send those parameters to Servlet using curl . I am able to receive the data at the servlet but i cannot progress furthur ie flow of control returns to PHP page again and displays error on the web page that the requested resource is not available(RequestDispatcher is not forwading properly i guess.)

What i've tried is as follows.

HTML

 <form id="loginform" name="loginform" method="POST" action="HomePage.php">
 <input type="text" id="user_name" name="user_name">
 <input type="password" id="password" name="password">
 <input type="submit" >

PHP

    <?php
    // After validating 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/WebApp/Validatelogin");//validatelogin is a servlet
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $email . "pass=" . $pass);
        curl_exec($ch);
        curl_close($ch);
     ?>

validateLogin.java

        email = request.getParameter("email").trim();
        Password= request.getParameter("pass");
        RequestDispatcher rd = request.getRequestDispatcher("./mainpage.jsp");
        rd.forward(request, response);

  Error : mainpage is not available. 

Anyone please help me as i am newbie to PHP.Thank You.

Handle redirect from your php as you are redirecting from your java.

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Also, if you want to see the output from your php, use return-transfer option

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

I guess that CURL doesn't redirect to another resource,it only fetches the data from another resource(websites). I have used PHP simplest method header to redirect to a servlet in my web application.It solved my problem.

header("Location: http://localhost:8080/WebApp/Validatelogin?email=$email");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM