1

Currently I'm trying to make a javascript button that when clicks sends an Http Post request to a javaservlet. I see how one can get a file ready to send from here. However, I don't know the syntax to have the form send both a file and text strings as parameters to a servlet.

Currently, I'm trying to do something along the lines of

var form = document.createElement("Form");
form.setAttribute("Method", "POST");
var formData = new FormData();

var blob = new Blob(['Lorem ipsum'], { type: 'plain/text' });
formData.append('file', blob,'myfile.xml')
form.setAttribute("FIle", "myfile.xml");
form.setAttribute("contentTYpe", "text/xml");
document.body.appendChild(form);
form.submit();

However my servlet can't seem to grab these attributes. I keep sysout only null

Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28
Qwerty Qwerts
  • 1,521
  • 2
  • 9
  • 13

1 Answers1

0

You can change your action URL as follow to send text in the same request:

http://www.test.com/yourServlet?param=iota1&param2=iota2

In the servlet you can fetch the data as follows:

String p1 = request.getParameter("param");
System.out.println(p1); // should return 'iota1'
Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28