AnsweredAssumed Answered

java API handle form submit as json content type

Question asked by thinhnk on Mar 11, 2013
Latest reply on Mar 12, 2013 by thinhnk
Hi friends,
I have a form:

<div id="${el}-body" class="password">
   <form id="${el}-form" action="${url.context}/service/formhandle" method="post">
  
      <div class="header-bar">${msg("label.title")}</div>
      <div class="row">
         <span class="label"><label for="${el}-username">${msg("label.username")}:</label></span>
         <span class="input"><input type="text" maxlength="255" size="30" id="${el}-username" /></span>
      </div>
     <div class="row">
         <span class="label"><label for="${el}-password">${msg("label.password")}:</label></span>
         <span class="input"><input type="password" maxlength="255" size="30" id="${el}-password" /></span>
      </div>
   <form>
</div>

And I have a java-backend webscript class.

public class SimpleWebScript extends AbstractWebScript
{
    public void execute(WebScriptRequest req, WebScriptResponse res)
        throws IOException
    {
       try
       {
         String[] params = req.getParameterNames();
         String errormsg = "";
         String mess = "fail";
         for(int i = 0; i < params.length; i++){
            errormsg += params;
            errormsg += "*";            
         }
         // build a json object
          JSONObject obj = new JSONObject();
          
          // put some data on it
          obj.put("success", false);
         if(params.length == 0)
            obj.put("message", fail);
         else
            obj.put("message", errormsg);          
          // build a JSON string and send it back
          String jsonString = obj.toString();
          res.getWriter().write(jsonString);
       }
       catch(JSONException e)
       {
          throw new WebScriptException("Unable to serialize JSON");
       }
    }   
}



I submit the form with content type "application/json" and use java backend to handle it. But I cannot get the parameters as I want be cause req.getParameterNames() not work. It returns an empty string array. :( How can I get the form field? Please help me.Thanks for any help.

Outcomes