【问题标题】:How to receive form parameters values in server?如何在服务器中接收表单参数值?
【发布时间】:2018-04-05 12:58:04
【问题描述】:

我有 React JS 应用程序,我使用 axios 库将 post 请求发送到服务器并提交表单。

客户请求:

sendData(data,price) {
    axios.post('http://localhost:8080/SampleJavaAPP/UserServer', {
    item: data,//these value
    price:price//these value
  })
  .then(function (response) {
      console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
  }

我不确定如何将这些值输入到我正在服务器中获取这样的值的服务器中

String name = request.getParameter("item");
        String price = request.getParameter("price");

        System.out.println("Result "+name + price);

但它在服务器中给出空值。如何在服务器中接收这些值参数?

【问题讨论】:

  • 您是否在发布请求之前尝试过 console.log(data, price) 以澄清问题的根源?
  • 是的值正在控制台中打印,但它在服务器中给出 null...
  • 您使用什么来编写服务器端软件?请注意,使用 React 发出请求并不重要。无论他们使用什么语言或框架,所有客户端都将以完全相同的方式发出 HTTP 请求。
  • 我在服务器端使用 java j2EE spring mvc 框架...但是请求正在访问服务器,但参数值为空,如我所述。
  • stackoverflow.com/questions/3831680/… 您正在发送无法直接作为表单数据读取的 json 数据。

标签: java server httprequest axios


【解决方案1】:

由于 Axios 正在发送 Json 数据,您将无法直接读取它。 有两种可能的解决方案:

  1. 要么将数据作为表单数据发送。

  2. 在 servlet 上读取和解析 JSON:

    public void doPost(HttpServletRequest request, HttpServletResponse response) 
                                          throws ServletException, IOException {    
        StringBuffer jb = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                jb.append(line);
            }
        } catch (Exception e) { /*report an error*/ }
    
        try {
            JSONObject jsonObject =  HTTP.toJSONObject(jb.toString());
            String price = jsonObject.get("price"); // will return price value.
        } catch (JSONException e) {
            throw new IOException("Error parsing JSON request string");
        }
    }
    

【讨论】:

  • 从这一行 JSONObject jsonObject = HTTP.toJSONObject(jb.toString());如何导入 JSONObject 和 HTTP ??这些都给了
  • json对象需要导入对应的jars..org.jsonmvnrepository.com/artifact/org.json/json
  • String price = jsonObject.get("price");此行给出错误,因为 jsonObject.get("price") 没有保存到价格
  • 我已经用一点点不同的方法完成了......但是你让我知道 axios 发送 json 数据,所以我能够完成它,这就是我接受你的答案的原因......
【解决方案2】:

request.getParameter() 不检索请求正文。您需要通过 request.getReader() 来检索它。

String body = IOUtils.toString(request.getReader());

建议先使用Apache Commons IO获取Content。因为您的请求是 JSON 格式。您可以使用 Jackson 将 String 转换为 Map。

Map<String, String> map = mapper.readValue(body, new TypeReference<Map<String, String>>(){});
System.out.println(map.get("item"));
System.out.println(map.get("price"));

【讨论】:

    【解决方案3】:

    request.getParameter()指的是URL 参数 ->myurl?someparameter=1

    通过request.getParameter("item"),您的网址需要看起来像http://localhost:8080/SampleJavaAPP/UserServer?item=myitem

    你在这里实际做什么

    sendData(data,price) {
        axios.post('http://localhost:8080/SampleJavaAPP/UserServer', {
        item: data,//these value
        price:price//these value
    }
    

    正在将对象添加到请求正文中,恕我直言,这是正确的。你不会在服务器端请求对象上找到任何参数 itemprice

    您需要做的是解析请求正文。使用request.getInputStream(),您可以获得Inputstream。我建议你使用一个对象映射器,它使这非常容易。见Intro to the Jackson ObjectMapper

    在您的 servlet 中,您可以执行以下操作:

    ObjectMapper objectMapper = new ObjectMapper();
    MyItem myItem = objectMapper.readValue(request.getInputStream(), MyItem.class);
    
    public class MyItem{
    
        String price;
        String item;
    
        public void setItem(String item) {
            this.item = item;
        }
    
        public void setPrice(String price) {
            this.price = price;
        }
    
        public String getItem() {
            return item;
        }
    
        public String getPrice() {
            return price;
        }
    
    }
    

    【讨论】:

    • 我应该在哪里调用 request.getBody() ??
    • 通常在您的 servlet 中。您拨打电话的同一地点request.getParameter
    • 我的错。这不是request.getBody() 检查我的更新答案
    • 我没有正确理解你想说的话....我应该调用 String price = request.getInputStream("price") 吗?我打电话给它,但它给了我错误。
    • 请求正文可用作 InputStream(请参阅 InputStream),当然不能直接转换为您的对象之一。您需要做的是将此InputStream 转换为您的对象。为此,您将使用对象映射器。
    猜你喜欢
    • 2018-03-05
    • 1970-01-01
    • 2017-12-16
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多