将变量从 JavaScript 传递到 servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22374299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:22:19  来源:igfitidea点击:

Passing variable from JavaScript to servlet

javajavascriptjspservlets

提问by

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

我在我的 JSP 页面中使用了 JavaScript 来处理获取 div 标签的数据:

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

现在我想将 myDivText 传递给我的servlet.java. 直到今天我已经通过它如下

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?

但是现在,我必须包含一些输入表单,现在我通过提交机制调用我的 servlet,那么如何在不使用上述方法的情况下将 myDivText 传递给 servlet.java?

==============================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:

通过 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

在 Servlet.java 中获取数据

String mydiv = request.getParameter("myField");
request.setAttribute("mydiv", mydiv);

采纳答案by Santino 'Sonny' Corleone

You could give your hidden field an id:

你可以给你的隐藏字段一个 id:

.html file

.html 文件

Your form

你的表格

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

and then when you want to assign its value:

然后当你想分配它的值时:

Your Javascipt

你的Javascipt

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

send the form with an action..

发送带有操作的表单..

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

并在操作页面上使用检索它 request.getParameter("myField")

Hope it helps

希望能帮助到你

UPDATE

更新

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

希望此链接有助于 Multipart/form-data 如何隐藏参数

How to upload files to server using JSP/Servlet?

如何使用 JSP/Servlet 将文件上传到服务器?

回答by Subir Kumar Sao

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

创建一个表单并通过javascript设置值并提交表单。

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

And in JS

在 JS 中

form = document.getElementById("formId"); // or directly through dom
// set the values.
form.submit();