我正在尝试使用" GET"
从PHP服务器向Java服务器发送输入我发送的请求没有问题 Request and Response
服务器都已连接,我收到回复" null请输入一个号码"
其中null是
的输出out.println(inputdata);
我的代码
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>!");
}
}
我在这里找不到任何内容&#34; 请求&#34;,没有任何标准方法可以提供除<&lt; null & #39;请帮忙!我是新手。
答案 0 :(得分:0)
您正在检查不存在的输入
if (request.getParameter("input") == null)
^^^^^
我认为应该如下:
if (request.getParameter("inputdata") == null)
当您检查curl
请求类型时,您是将请求发布到GET
。
if(request.getMethod().equals("GET"))
您需要检查请求是否已发布,然后get the POST
request body或将curl设置为发送带有简单查询字符串的GET
请求。
答案 1 :(得分:0)
尝试将php代码修改为$url = "http://localhost:8080/CloudComputingProj/Cloudpi?inputdata=".$inputdata
您还可以通过在任何浏览器http://localhost:8080/CloudComputingProj/Cloudpi?inputdata=your-encoded-param