1

I've used JavaScript in my JSP page to process get data of the div tag:

var script = ace.edit("editor");
var myDivText = script.getValue();

Now I want to pass myDivText to my servlet.java. Till today I've passed it as below

window.open('http://XXX.XX.XXX.XX:8800/FirstServlet/mygeco?mytxt=' + myDivText,'resizable=yes');

But now, I had to include a few input forms and now I am calling my servlet through a submit mechanism, so how do I pass myDivText to servlet.java without using the above method?

==============================EDIT==========================

My form looks as follows:

<form method="post" name="myform" action="upload" target="_top" enctype="multipart/form-data">
<li>Left File :  <input type="file" name="dataFile1" id="fileChooser1" /></li>
<li>Right File : <input type="file" name="dataFile2" id="fileChooser2" /></li>
<li>Config File :<input type="file" name="dataFile3" id="fileChooser3" /></li>
<li><input type="hidden" name="myField" id="myfield" value="" /></li>
</form>

Submitting form through JavaScript:

var scc1 = document.getElementById("box");
scc1.innerHTML = scc1.innerHTML + "<br>" + "<span class='blue'>Uploading files, the page might refresh</span>";
var thetxt = scc1.innerHTML;
document.getElementById('myField').value = thetxt; 
document.myform.submit();   

Getting data in Servlet.java

String mydiv = request.getParameter("myField");
request.setAttribute("mydiv", mydiv);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

2

You could give your hidden field an id:

.html file

Your form

<form action="">
<input type="hidden" name="myField" id="myField" value="" />
</form>

and then when you want to assign its value:

Your Javascipt

document.getElementById('myField').value = myDivText;

send the form with an action..

And at the action page your retrieve it using request.getParameter("myField")

Hope it helps

UPDATE

Hope this links help Multipart/form-data how to get parameter hidden

How to upload files to server using JSP/Servlet?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52
-1

Create a form and set the values through javascript and submit the form.

<form action='<url>' method="POST">
<input type="hidden" name="divtext"/>
</form>

And in JS

form = document.getElementById("formId"); // or directly through dom
// set the values.
form.submit();
Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47