开发者

how to recieve all the parameters from XMLHttpRequest request on server side?

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").va开发者_如何学JAVAlue+",filename="+document.getElementById("tfile").value;
 client.open("POST",url_action,true);
 client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

 client.send(dataString);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜