发送时从 JSP 文件中的请求接收空参数
Receiving null parameters from request in JSP file when are being sent
我有一个 JSP 应用程序。它上传一个文件,但为此用户必须使用名称和密码进行身份验证。所以我的 JSP 文件以:
开头
//0.2.- We get the password
String password = (String) request.getParameter("pass"); // -> This returns NULL
//0.3.- We get the "uvus"
String uvus = (String) request.getParameter("uvus"); //-> This also returns NULL
//More code
所以我想知道为什么我从这些变量中得到空值。
我转到我正在上传的表格,并查找正在发送的数据。使用 Firefox 调试工具,我看到:
所以事实上,它正在发送。
作为附加信息,我正在构建这样的请求:
var pUvus = document.getElementById("uvus").value;
var pPassword = document.getElementById("pass").value;
var file = document.getElementById("userFile");
var formData = new FormData();
formData.append("upload", file.files[0]);
formData.append("uvus", pUvus);
formData.append("pass", pPassword);
xmlhttp.open("POST","uploadFile.jsp",true);
xmlhttp.send(formData);
最后,我想说我可以在同一个 JSP 中从应用程序对象中获取 vars 而没有错误,并且在另一对 JSP 文件中收到了请求对象中的 vars没有更多问题,所以我认为我的错应该是我在 Ajax 中构建请求的方式,但我对此没有更多线索...
谁能指导我?
感谢您的帮助
更新:@rickz 问我如何获取文件并解析请求(在我的问题之后做了什么,试图从请求范围中获取对象):
List items;
items = servlet_up.parseRequest(request);
for(int i=0;i<items.size();i++)
{
FileItem item = (FileItem) items.get(i);
if (! item.isFormField())
{
request.getParameter() 不适用于 multipart/form-data 请求。
如果您使用 org.apache.commons.fileupload 那么您应该使用类似
的东西
if(item.isFormField()){
name = item.getFieldName();
...
}
我有一个 JSP 应用程序。它上传一个文件,但为此用户必须使用名称和密码进行身份验证。所以我的 JSP 文件以:
开头//0.2.- We get the password
String password = (String) request.getParameter("pass"); // -> This returns NULL
//0.3.- We get the "uvus"
String uvus = (String) request.getParameter("uvus"); //-> This also returns NULL
//More code
所以我想知道为什么我从这些变量中得到空值。
我转到我正在上传的表格,并查找正在发送的数据。使用 Firefox 调试工具,我看到:
所以事实上,它正在发送。
作为附加信息,我正在构建这样的请求:
var pUvus = document.getElementById("uvus").value;
var pPassword = document.getElementById("pass").value;
var file = document.getElementById("userFile");
var formData = new FormData();
formData.append("upload", file.files[0]);
formData.append("uvus", pUvus);
formData.append("pass", pPassword);
xmlhttp.open("POST","uploadFile.jsp",true);
xmlhttp.send(formData);
最后,我想说我可以在同一个 JSP 中从应用程序对象中获取 vars 而没有错误,并且在另一对 JSP 文件中收到了请求对象中的 vars没有更多问题,所以我认为我的错应该是我在 Ajax 中构建请求的方式,但我对此没有更多线索...
谁能指导我?
感谢您的帮助
更新:@rickz 问我如何获取文件并解析请求(在我的问题之后做了什么,试图从请求范围中获取对象):
List items;
items = servlet_up.parseRequest(request);
for(int i=0;i<items.size();i++)
{
FileItem item = (FileItem) items.get(i);
if (! item.isFormField())
{
request.getParameter() 不适用于 multipart/form-data 请求。 如果您使用 org.apache.commons.fileupload 那么您应该使用类似
的东西if(item.isFormField()){
name = item.getFieldName();
...
}