问题描述
我的看法是HTML 5,我用FORMDATA做一个AJAX 2 POST到Servlet。里面的servlet的我想要读取请求参数。我看不到任何参数。然而,谷歌Chrome开发控制台显示的请求负载。我怎样才能在Servlet的code中的一样吗?任何帮助将AP preciated。这里的code。
JS code
VAR XHR =新XMLHtt prequest();
VAR FORMDATA =新FORMDATA();
formData.append('的firstName','ABC');
formData.append('lastName的','某某');
xhr.open(POST,targetLocation,真正的);
xhr.send(FORMDATA);
Servlet的code(这两个参数返回空
)
通过out.println(你好!+的request.getParameter(名字)++的request.getParameter(姓氏)+,感谢发送您的反馈意见。 );
谷歌浏览器控制台
内容处置:表格数据; NAME =名字
XYZ
内容处置:表格数据; NAME =姓
ABC
在HTML5 FORMDATA
API发送的multipart / form-data的
请求。它的最初设计能够通过AJAX上传文件,与新版本2 XMLHtt prequest
。上传的文件是不可能的previous版本。
的 的request.getParameter()
默认识别应用程序/ x-WWW的形式urlen codeD只有
请求。但是你发送一个的multipart / form-data的
请求。您需要使用 @MultipartConfig 这样就可以通过的request.getParameter让他们()
。
@WebServlet
@MultipartConfig
公共类YourServlet延伸的HttpServlet {}
或者,当你还没有上的Servlet 3.0然而,使用Apache Commons FileUpload一起。有关这两种方法的更详细的回答,请参阅本:How将文件上传到使用JSP / Servlet的服务器?
如果你并不需要在所有上传的文件,使用标准 XMLHtt prequest
办法来代替。
VAR XHR =新XMLHtt prequest();
VAR数据=名字=EN + codeURIComponent(名字)
+&放大器; lastName的=EN + codeURIComponent(lastName的);
xhr.open(POST,targetLocation,真正的);
xhr.setRequestHeader(内容类型,应用程序/ x-WWW的形式urlen codeD);
xhr.send(数据);
这样,您就不需要 @MultipartConfig
在你的servlet了。
参见:
- 如何使用servlet和阿贾克斯?
- <一个href="http://stackoverflow.com/questions/9395911/sending-a-file-as-multipart-through-xmlhtt$p$pquest">sending该文件为多部分通过xmlHtt prequest
My view is HTML 5. I'm using FormData to make a AJAX 2 POST to a Servlet. Inside the servlet i'm trying to read request parameters. I can't see any parameters. However, Google Chrome Dev console shows the request payload. How can I get the same in Servlet code? Any help will be appreciated. Here's the code.
JS code
var xhr = new XMLHttpRequest();
var formData = new FormData();
formData.append('firstName', 'ABC');
formData.append('lastName', 'XYZ');
xhr.open("POST", targetLocation, true);
xhr.send(formData);
Servlet code (both parameters return null
)
out.println("Hello! "+ request.getParameter("firstName")+ " "+ request.getParameter("lastName")+ ", thanks for sending your feedback." );
Google Chrome Console
Content-Disposition: form-data; name="firstName"
XYZ
Content-Disposition: form-data; name="lastName"
ABC
The HTML5 FormData
API sends a multipart/form-data
request. It's initially designed to be able to upload files by ajax, with the new version 2 XMLHttpRequest
. Uploading files wasn't possible with the previous version.
The request.getParameter()
by default recognizes application/x-www-form-urlencoded
requests only. But you're sending a multipart/form-data
request. You need to annotate your servlet class with @MultipartConfig
so that you can get them by request.getParameter()
.
@WebServlet
@MultipartConfig
public class YourServlet extends HttpServlet {}
Or, when you're still not on Servlet 3.0 yet, use Apache Commons FileUpload. For a more detailed answer on both approaches, see this: How to upload files to server using JSP/Servlet?
If you don't need to upload files at all, use the "standard" XMLHttpRequest
approach instead.
var xhr = new XMLHttpRequest();
var data = "firstName=" + encodeURIComponent(firstName)
+ "&lastName=" + encodeURIComponent(lastName);
xhr.open("POST", targetLocation, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
This way you don't need @MultipartConfig
on your servlet anymore.
See also:
这篇关于HTML5 FORMDATA中的Java Servlet的request.getParameter返回null()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!