简体   繁体   English

如何在jsp页面中将javascript值赋给jsp变量

[英]how to assign javascript value to jsp variable in jsp page

I have a text field, I need to send these text value to the same page which is in jsp . 我有一个文本字段,我需要将这些文本值发送到jsp中的同一页面。

I just want to assign javascript value to jsp variable. 我只想将javascript值赋给jsp变量。

You can't. 你不能。 JSP runs on the server, Javascript runs on the client, so when Javascript runs the JSP variable doesnt exist anymore. JSP在服务器上运行,Javascript在客户端上运行,因此当Javascript运行时,JSP变量不再存在。

Consider using AJAX . 考虑使用AJAX

You could set a hidden field. 您可以设置隐藏字段。

Put this in your JSP form: 把它放在你的JSP表单中:

<input type="hidden" id="foo" name="foo" />

Execute this script whenever you want to fill the field: 只要您想填写该字段,请执行此脚本:

document.getElementById("foo").value = "some value";

When you submit the form, it'll be available as follows in the servlet: 提交表单时,它将在servlet中按如下方式提供:

String foo = request.getParameter("foo"); // "some value"
// ...

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM