0

I am creating an html form dynamically using javascrpt and submitting it using ajax. When I check the parameter names from the servlet, it does not show the parameter names correctly. The javascript / ajax and servlet code are shown below along with the diagnostic output from the tomcat log file (catalina.out)

Javascript:

var gulticsForm = document.createElement("form");
gulticsForm.action="/gultics/Utils";
var element1=document.createElement("input");
element1.setAttribute("name", "sqlText");
element1.setAttribute("value", "This is the value of the element");
gulticsForm.appendChild(element1);
document.body.appendChild(gulticsForm);
var fm = new FormData(gulticsForm);
xhttp.open("POST", "/gultics/Utils", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xhttp.send(fm);

Servlet:

public void doPost(HttpServletRequest request, HttpServletResponse response) { callServer(request, response); }
public void doGet(HttpServletRequest request, HttpServletResponse response) { callServer(request, response); }
private void callServer(HttpServletRequest request, HttpServletResponse response) {
    try {
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        Enumeration<String> parameterNames = request.getParameterNames();
        int parameterNo = 0;
        while (parameterNames.hasMoreElements()) {
            System.out.println("Name of Parameter " + parameterNo++ + " is " + parameterNames.nextElement());
        }
        String sqlText = request.getParameter("sqlText");
        System.out.println("SQL Text: " + sqlText);

Output from catalina.out

Name of Parameter 0 is -----------------------------229583121529757

Content-Disposition: form-data; name

SQL Text: null

Can you please help? Please let me know if I can provide more information.

  • Couple of points 1)Is the form being rendered correctly using JavaScript in terms of structure 2) Try debugging parameterNames for the values received to figure out if issue is on form end or servlet end – Shivam Aggarwal Jun 29 '17 at 08:37
  • Hi Shivam, thank you very much for your help. Shivam, the form is created dynamically from scratch for being submitted via ajax; the intention is to create and submit the form using ajax and then destroy / remove the temporarily created form object within the javascript function itself. It is not intended to render the form. Also, the output from catalina.out shows the parameter name, which is the 2 lines of metadata shown above, "-----------------------------229583121529757 Content-Disposition: form-data; name" – Programmer Jun 29 '17 at 16:42
  • Can you try debugging parameterNames for values received in the same – Shivam Aggarwal Jun 30 '17 at 06:11
  • Just to be curious,what is the actual business case u r looking for?instead of form,we can directly send over the value to servlet/controller using ajax – Shivam Aggarwal Jun 30 '17 at 06:17
  • Thank you for the suggestion Shivam. I shall try to send the contents directly through ajax, without using form and update the outcome here. – Programmer Jul 01 '17 at 00:58

1 Answers1

0

you have to read the body of the request,look at this answer

Getting request payload from POST request in Java servlet

  • Hi Lorenzo, thanks for your help. Do you think I shall have to parse the message body and re-implement the methods getParameterNames, getParameter etc.? Can I not use the methods as it is? – Programmer Jun 28 '17 at 20:23