java - 如何在服务器端接收 XMLHttpRequest 请求的所有参数?

标签 java html servlets

我正在使用 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);
}

最佳答案

您错误地对表单数据进行了编码,您必须使用 & 分隔字段,而不是使用 , 分隔字段。 请参阅Wikipedia总结:

This is a format for encoding key-value pairs with possibly duplicate keys. Each key-value pair is separated by an '&' character, and each key is separated from its value by an '=' character.

顺便说一句,你的 Java 代码看起来很冗长,你可以通过使用 for-each-loops 来简化。

关于java - 如何在服务器端接收 XMLHttpRequest 请求的所有参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5933557/

相关文章:

html - div 中两个输入的绝对定位

java - 无法使用 AJAX 响应作为文件名(而非内容)

eclipse - 动态 Web 项目库

java - 在 Eclipse 中启动程序与在终端中启动程序有何不同?

java - 在内部存储的文件夹内创建文件

javascript - 将静态 csv 数据加载到 Highcharts Highstock 图表中时如何更改线条颜色?

javascript - href ="javascript:"在新选项卡中打开

java - 在 JPanel 中显示 JDesktopPane

java - Eclipse:在 NotANumber 上停止

tomcat - Servlet问题