【问题标题】:request in java server has no query stringjava服务器中的请求没有查询字符串
【发布时间】:2017-07-31 10:43:31
【问题描述】:

我正在尝试使用“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>!");
             }

        }

我在这里没有为“request”找到任何东西,除了“null”之外,没有任何标准方法可以给我任何帮助!我是新手。

【问题讨论】:

    标签: java php apache tomcat server


    【解决方案1】:

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

    【讨论】:

    • 这不起作用,因为这是我的 Java 服务器 URL(我从 PHP 调用)输入数据由请求作为查询字符串和参数(检查图像)发送。
    • @Pandeyji 你发布的请求图片是对php服务器的请求,而不是对java服务器的请求。你可以直接调用你的java服务器,就像我说的那样。如果您的 java 服务器响应正确,那么您的 php 代码中存在错误。
    • 你是否建议我直接从 HTML 调用我的 java 服务器而不是调用 php?
    • @Pandeyji 是的,用于测试目的。
    • 哦,是的,我现在得到了输入数据输出:网页:2131 你好空! Java 控制台:内部服务 /CloudComputingProj java.util.Collections$3@20d3e8f0 2131 2131
    【解决方案2】:

    您正在检查一个不存在的输入

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

    我认为应该如下:

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

    更新

    您正在将请求发布到curl,同时您正在检查GET 请求类型。

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

    您需要检查请求是否已发布,然后 get the POST request body 或设置您的 curl 以发送带有简单查询字符串的 GET 请求。

    【讨论】:

    • 感谢 Hassan 的快速回复。那个“输入”是错误的,应该是inputdata。然而,请求在服务本身失败。它没有给我任何参数或查询字符串。
    • 刚刚做了,感谢您的关注。你在 java 服务器代码中还有什么可疑的地方吗?
    • 在受保护的 void 服务中,我得到的“请求”除了 request.getMethod() 之外不返回任何内容。 System.out.println(request.getQueryString());给我一个'null',它应该给我查询字符串,即我的php服务器的url和输入变量。如果接收到的请求本身不完整,则 doGet 方法无关紧要。 getParameter() 或 getQueryString() 都不起作用。
    • 请检查名为“请求和响应”的图像。也许你找到了我不知道的东西。
    • request.getQueryString() 将读取该 url 的查询字符串:http://localhost:8080/CloudComputingProj/Cloudpi 而不是 url localhost/url.php?inputdata=.....,尝试按如下方式发送 url:$url = "http://localhost:8080/CloudComputingProj/Cloudpi?inputdata=$inputdata";
    猜你喜欢
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多