gpt4 book ai didi

java - java服务器中的请求没有查询字符串

转载 作者:行者123 更新时间:2023-11-28 22:26:31 25 4
gpt4 key购买 nike

我正在尝试使用“GET”将输入从 PHP 服务器发送到 Java 服务器

我发送的请求没有问题 Request and Response

两个服务器都已连接,我收到响应“null Please enter a number”

其中 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 服务器

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>!");
}

}

我在这里找不到“请求”的任何内容,除了“null”之外,没有任何标准方法给我任何帮助!我对此很陌生。

最佳答案

尝试修改php代码为$url = "http://localhost:8080/CloudComputingProj/Cloudpi?inputdata=".$inputdata您还可以通过在任何浏览器中键入 get 请求来调试 java 端 http://localhost:8080/CloudComputingProj/Cloudpi?inputdata=your-encoded-param

关于java - java服务器中的请求没有查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42712432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com