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

发布于 2024-11-06 01:27:13 字数 2508 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2024-11-13 01:27:13

您错误地对表单数据进行了编码,必须用 & 分隔字段,而不是用 , 分隔字段。
有关摘要,请参阅维基百科

这是一种用于编码键值对的格式,可能包含
重复的键。每个键值对是
用 '&' 分隔性格,以及
每个键与其值分开
通过“=”字符。

顺便说一句,你的 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:

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.

BTW, your Java-Code looks verbose, you could simplify by using for-each-loops.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文