Question

I have an editor, in which all the user entered text is entered into a tag, now I want to pass this data to a servlet. I've tried using forms but, the value on the servlet side displays a null.

How to do it? and I want to get this data into my doget().

JSP

<form method="post" name="divdata" action="mygeco" target="_blank" > 
<div id="editor">
User enters some text here
</div></form>

<button type= "button" style="position: absolute; right: 11%; top: 30%;" id="execute" onclick="saveTextAsFile()">Click to execute</button>
<script type='text/javascript'> 
function saveTextAsFile()
{
document.divdata.submit();
}

Servlet:

String text = request.getParameter("divdata");
System.out.println(text);

PS: I've also tried passing by url, but this is creating me further problems, apart from this pl suggest if there is any other method.

window.open('http://XXX.XX.XXX.XXX:7774/FirstServlet/mygeco?mytxt=' + myDivText,'_top','resizable=yes');
Was it helpful?

Solution

i think what you got to do is like the following :

1st - declare your ACE editor with the div like the following :

 <body onload="onloadPage()">

<form method="post" id="divdata" action="mygeco" target="_blank" >
<div id="editor">
User enters some text here
</div>

<textarea id="textArea" ></textarea>
<input type="button" onclick="submitForm()" value="Button"/>
</form>
</body>

2nd - onloadPage() :

  function onloadPage()
{
    // hide the textArea
    document.getElementById('textArea').style.display = "none";
}

3rd - submitForm() :

function submitForm() {

    var editor = ace.edit("editor");
    var code = editor.getSession().getValue();
    document.getElementById('textArea').style.display = "block";
    document.getElementById('textArea').value=code;

    document.getElementById("divdata").submit();
}

4th - get the value in the servlet :

String textArea=request.getParameter("textArea");

and please give me feedback .

Hope that helps .

OTHER TIPS

From form datas are passed from input fields like input type="text" or input type="password" etc

You do not have any inout field in the form

Secondly, when you are writting request.getParameter() then you are trying to get from a name attribute not from id

<div> is not an <form> tag, therefore it's not sent by submit function. You can find all HTML form tags here: http://www.w3schools.com/html/html_forms.asp

Possible solution for your problem:

Change your form to:

<form method="post" name="divdata" action="mygeco" target="_blank" > 
  <input type="text" name="editor" value="User enters some text here">
</form>

Important is name attribute from input element, you use it in request.getParameter(arg) function.

I am not sure what you are asking. How the user enters value inside div without any input control. You can use some hidden fields to store the date value when user entering the value into div. Keep the hidden field inside of your form. So you can easily get the value from form. Like this,

<form method="post" name="divdata" action="mygeco" target="_blank" > 
 <div id="editor">
   User enters some text here
 </div>
  <input type="hidden" name="hiddenDate" id="hiddenDate" >
</form>

And in your javascript,you can get the text available in your div. Like this,

<script type='text/javascript'> 
    $("#hiddenDate").val($("#editor").html());
</script>

Finally in your servlet,

String text = request.getParameter("hiddenDate");
System.out.println(text);

Hope this helps.

There are lots of ways to pass data from your jsp to your servlet. I prefer passing data via making it via jQuery.getJson() : http://api.jquery.com/jquery.getjson/

Here is an example :

$.getJSON(YourServlet, {
        param: paramvalue,
        param:{
           param : value,
           param : value
        }
    }, function(json){
           //response callback
}

request.getParamater() can be used to get data by using name not by id, you need to convert it or assign to some other field which has a name or it may be dynamically created HTML element before form submission in JS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top