简体   繁体   English

如何使用PHP页面中的curl将参数发布到Java servlet?

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

I am working on a web application using Netbeans. 我正在使用Netbeans开发Web应用程序。 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 . 我的PHP页面由一个包含用户名和密码的表单组成,我必须使用MySQLPHP中进行验证,然后使用curl将这些参数发送给Servlet。 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.) 我能够在Servlet上接收数据,但是我无法继续进行操作,即控制流再次返回PHP页面,并在网页上显示错误,表明所请求的资源不可用(我猜RequestDispatcher不能正确地放弃。)

What i've tried is as follows. 我已经尝试过如下。

HTML 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

    <?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 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. 任何人都可以帮助我,因为我是PHP的新手。谢谢。

Handle redirect from your php as you are redirecting from your java. 从Java重定向时,请从php处理重定向。

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Also, if you want to see the output from your php, use return-transfer option 另外,如果您想查看PHP的输出,请使用return-transfer选项

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). 我想CURL不会重定向到另一个资源,它只会从另一个资源(网站)获取数据。 I have used PHP simplest method header to redirect to a servlet in my web application.It solved my problem. 我已经使用PHP最简单的方法标头将其重定向到Web应用程序中的servlet。它解决了我的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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