Servlet 参数和 doPut [英] Servlet parameters and doPut
本文介绍了Servlet 参数和 doPut的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用 HttpServlet#doPut 从 PUT 请求中获取参数:
Trying to get parameters from a PUT request using HttpServlet#doPut:
public void doPut(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
// name is null
}
使用 curl 发送请求:
Using curl to send the request:
curl -X PUT
--data "name=batman"
--header "Content-Type: text/plain" http://localhost:8080/sample.html
使用 doGet 和 GET curl 请求可以正常工作.我错过了什么吗?
works fine with using doGet and GET curl request. Am I missing something?
推荐答案
根据评论和进一步的研究,我意识到 Servlet 不能假设任何关于放入服务器的数据,因此不会解析名称/值对.
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.
以下解决方案似乎是处理通过 PUT 传递的任何数据的正确方法,并且可以解析为 XML、名称/值或其他任何内容.
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.
BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream());
String data = br.readLine();
这篇关于Servlet 参数和 doPut的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文