如何在服务器端接收 XMLHttpRequest 请求的所有参数?
我正在使用 XMLHttpRequest 创建一个简单的表单提交并传递 2 个参数。在服务器端,我收到两个参数,但如何将它们放在不同的变量中?
这是 Servlet
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
paramMap=request.getParameterMap();
if (paramMap == null)
throw new ServletException(
"getParameterMap returned null in: " + getClass().getName());
iterator=paramMap.entrySet().iterator();
System.out.println(paramMap.size());
String str="";
while(iterator.hasNext())
{
Map.Entry me=(Map.Entry)iterator.next();
String[] arr=(String[])me.getValue();
configId=arr[0];
System.out.println(me.getKey()+" > "+configId);
}
/***Above println** i get "name > Abhishek,filename=a.txt*/
rand=new Random();
randomInt=rand.nextInt(1000000);
configId=randomInt+configId;
System.out.println(configId);
out.println(configId);
/*creates a new session if a session does not exist already*/
session=request.getSession();
session.setAttribute("cid", configId);
out.close();
/*I also need to check a session name `uid` i.e., already created before calling this servlet and then only get both the parameters in parameterMap and store all the params in session. so i'd like to do something like this */
session=request.getSession(false);
if(session!=null) //then get all the parameters here and store them into session
{
uid=session.getAttribute("uid").toString();
/*get nameFromTheParameterMap and fileNameFromTheParameterMap from paramt
session.setAttribute("name", nameFromTheParameterMap);
session.setAttribute("filename", fileNameFromTheParameterMap);
}
这是正确的方法吗?另外,我将如何从 dataString
获取参数到parameterMap,
这里是 saveConfig 函数
function saveConfig()
{
var url_action="/temp/SaveConfig";
var client;
var dataString;
if (window.XMLHttpRequest){ // IE7+, Firefox, Chrome, Opera, Safari
client=new XMLHttpRequest();
} else { // IE6, IE5
client=new ActiveXObject("Microsoft.XMLHTTP");
}
client.onreadystatechange=function(){
if(client.readyState==4&&client.status==200)
{
alert(client.responseText);
}
};
dataString="name="+document.getElementById("name").value+",filename="+document.getElementById("tfile").value;
client.open("POST",url_action,true);
client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
client.send(dataString);
}
I am using XMLHttpRequest to create a simple form submit and pass 2 parameters. On the server side I am receiving both the parameters but how to get them in different variables?
Here is the Servlet
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
paramMap=request.getParameterMap();
if (paramMap == null)
throw new ServletException(
"getParameterMap returned null in: " + getClass().getName());
iterator=paramMap.entrySet().iterator();
System.out.println(paramMap.size());
String str="";
while(iterator.hasNext())
{
Map.Entry me=(Map.Entry)iterator.next();
String[] arr=(String[])me.getValue();
configId=arr[0];
System.out.println(me.getKey()+" > "+configId);
}
/***Above println** i get "name > Abhishek,filename=a.txt*/
rand=new Random();
randomInt=rand.nextInt(1000000);
configId=randomInt+configId;
System.out.println(configId);
out.println(configId);
/*creates a new session if a session does not exist already*/
session=request.getSession();
session.setAttribute("cid", configId);
out.close();
/*I also need to check a session name `uid` i.e., already created before calling this servlet and then only get both the parameters in parameterMap and store all the params in session. so i'd like to do something like this */
session=request.getSession(false);
if(session!=null) //then get all the parameters here and store them into session
{
uid=session.getAttribute("uid").toString();
/*get nameFromTheParameterMap and fileNameFromTheParameterMap from paramt
session.setAttribute("name", nameFromTheParameterMap);
session.setAttribute("filename", fileNameFromTheParameterMap);
}
Is this the correct approach? Also how will I get parameters from dataString
to parameterMap
here is the saveConfig function
function saveConfig()
{
var url_action="/temp/SaveConfig";
var client;
var dataString;
if (window.XMLHttpRequest){ // IE7+, Firefox, Chrome, Opera, Safari
client=new XMLHttpRequest();
} else { // IE6, IE5
client=new ActiveXObject("Microsoft.XMLHTTP");
}
client.onreadystatechange=function(){
if(client.readyState==4&&client.status==200)
{
alert(client.responseText);
}
};
dataString="name="+document.getElementById("name").value+",filename="+document.getElementById("tfile").value;
client.open("POST",url_action,true);
client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
client.send(dataString);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您错误地对表单数据进行了编码,必须用
&
分隔字段,而不是用,
分隔字段。有关摘要,请参阅维基百科:
顺便说一句,你的 Java 代码看起来很冗长,你可以通过使用 for-each-loops 来简化。
You are wrongly encoding the form-data, you have to seperate the fields by
&
and not by,
.See Wikipedia for a summary:
BTW, your Java-Code looks verbose, you could simplify by using for-each-loops.