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);
2
  • 2
    use a hidden field within a form to pass it.. Commented Mar 13, 2014 at 9:34
  • How? I've no idea of that method and that is exactly why I've posted this question. Commented Mar 13, 2014 at 9:35

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

11 Comments

Just a moment, trying it..I'll let you know soon.
Hey the form itself is not getting submitted. When I click submit nothing is happening.
@Zedai <input type="submit" value"submit"> did u try this?
Just check I've just updated my code.
if u r using chrome.press ctrl+shift+i and go to the console and check if u getting any error in ur javascript...probably some error is taking place before the form.submit
|
-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();

Comments