java Servlet 参数和 doPut

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/855835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:04:57  来源:igfitidea点击:

Servlet parameters and doPut

javarestservlets

提问by Erick Fleming

Trying to get parameters from a PUT request using HttpServlet#doPut:

尝试使用 HttpServlet#doPut 从 PUT 请求中获取参数:

public void doPut(HttpServletRequest request, HttpServletResponse response) {
    String name = request.getParameter("name");
    // name is null
}

Using curl to send the request:

使用 curl 发送请求:

curl  -X PUT \
      --data "name=batman" \
      --header "Content-Type: text/plain" http://localhost:8080/sample.html

works fine with using doGet and GET curl request. Am I missing something?

使用 doGet 和 GET curl 请求可以正常工作。我错过了什么吗?

回答by Erick Fleming

Based on comments and further research I realized that the Servlet cannot assume anything about the data being put onto the server and therefore, will not parse name/value pairs.

基于评论和进一步的研究,我意识到 Servlet 不能假设任何关于放入服务器的数据,因此不会解析名称/值对。

The following solution seems to be the proper way to handle any data passed via PUT, and can be parsed as XML, Name/Value, or whatever.

以下解决方案似乎是处理通过 PUT 传递的任何数据的正确方法,并且可以解析为 XML、名称/值或其他任何内容。

BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream());

String data = br.readLine();

回答by Jan Bodnar

Unlike in doGet()and doPost()methods, we are not able to get the request parameters using the getParameter()method in doPut()and doDelete()methods. We need to retrieve them manually from the input stream.

与 indoGet()doPost()方法不同,我们无法使用getParameter()indoPut()doDelete()methods 方法获取请求参数。我们需要从输入流中手动检索它们。

The following method retrieves request parameters and returns them in a map:

以下方法检索请求参数并在映射中返回它们:

public static Map<String, String> getParameterMap(HttpServletRequest request) {

    BufferedReader br = null;
    Map<String, String> dataMap = null;

    try {

        InputStreamReader reader = new InputStreamReader(
                request.getInputStream());
        br = new BufferedReader(reader);

        String data = br.readLine();

        dataMap = Splitter.on('&')
                .trimResults()
                .withKeyValueSeparator(
                        Splitter.on('=')
                        .limit(2)
                        .trimResults())
                .split(data);

        return dataMap;
    } catch (IOException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(Utils.class.getName()).log(Level.WARNING, null, ex);
            }
        }
    }

    return dataMap;
}

The example uses Google's Guava library to parse the parameters. For a complete example containing doGet(), doPost(), doPut()and doDelete()methods, you can read my Using jsGrid tutorial.

该示例使用 Google 的 Guava 库来解析参数。对于包含一个完整的例子doGet()doPost()doPut()doDelete()方法,你可以阅读我的使用jsGrid教程

回答by Adeel Ansari

What do you mean by doPut()is not working? As far as I know, it doesn't work like doGet()or doPost(), where you have request parameters and stuff.

你说的doPut()不工作是什么意思?据我所知,它不像doGet()or那样工作doPost(),在那里你有请求参数和东西。

PUT can be used to put something on the server. In particular, the PUT operation allows a client to place a file on the server and is similar to sending a file by FTP. Check out this example, I found on JGuru.

PUT 可用于在服务器上放置一些东西。特别是 PUT 操作允许客户端将文件放置在服务器上,类似于通过 FTP 发送文件。看看这个例子,我在 JGuru 上找到的。

->GET /file.dat HTTP/1.1

<-HTTP/1.1 404 Not Found

->PUT /file.dat HTTP/1.1
Content-Length: 6
Content-Type: text/plain

Hello!

<-HTTP/1.1 200 OK

->GET /file.dat HTTP/1.1

<-HTTP/1.1 200 OK
Content-Length: 6
Content-Type: text/plain

Hello!