Java 从 JSP 上的文本框获取用户输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/796136/
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-11 19:35:43  来源:igfitidea点击:

Get user input from textbox on JSP

javatextboxinput

提问by user96884

I need to get the value that a user has entered in a textbox on a JSP. I used JavaScript to obtain the value, but then I need to access that same variable outside the JavaScript later on in the page.

我需要获取用户在 JSP 上的文本框中输入的值。我使用 JavaScript 来获取该值,但随后我需要在页面的 JavaScript 之外访问相同的变量。

Here's what I have:

这是我所拥有的:

<script type="text/javascript">

   var sp;

function setPolicy(){

sp = document.getElementById('policy').value;

if(sp != "")
    alert("You entered: " + sp)

else
    alert("Would you please enter some text?")
}

</script>

the textbox:

文本框:

input type="text" id='policy' size="15" tabindex = "1" onblur="setPolicy()"

But I need to access the string entered in this "policy" textbox later on in a scriplet to call a function within my session bean. Is there any way I can pass that variable outside the JavaScript? Or is there another way to do this?

但是我需要稍后在脚本中访问在此“策略”文本框中输入的字符串,以调用会话 bean 中的函数。有什么方法可以在 JavaScript 之外传递该变量?或者有另一种方法可以做到这一点?

Thanks!

谢谢!

回答by Bhushan Bhangale

JavaScript execution happens at client side and Scriptlet executes at server side. You are trying to combine the two.

JavaScript 在客户端执行,Scriptlet 在服务器端执行。您正在尝试将两者结合起来。

You should submit the form to the same page by passing a param which will have the value entered in the textbox. Your scriptlet should check if the param is present or not. First time coming on this jsp the param will not be present, it will be available only when user enter something in textbox.

您应该通过传递一个参数将表单提交到同一页面,该参数将在文本框中输入值。您的 scriptlet 应该检查参数是否存在。第一次使用这个 jsp 时,参数不会出现,只有当用户在文本框中输入内容时它才可用。

May not be the best solution as I don't know the whole context.

可能不是最好的解决方案,因为我不知道整个背景。

回答by Nathan Feger

I'm not sure you have the lifecycle of a jsp down quite yet. By the time that the javascript is running, all the scriptlets have been evaluated.

我不确定您是否已经完全关闭了 jsp 的生命周期。到 javascript 运行时,所有的 scriptlet 都已被评估。

By this I mean, one the JSP is rendered, html and javascript are emitted on the response to the browser, and there is no more server to interpret the JSP.

我的意思是,一个 JSP 被呈现,html 和 javascript 被发送到浏览器的响应中,并且没有更多的服务器来解释 JSP。

So, you should think of your problem as how do I communicate back to the server, the result of the user action?

因此,您应该将您的问题视为我如何与服务器通信,即用户操作的结果?

Probably by posting a form to an action on the server.

可能通过将表单发布到服务器上的操作。

回答by Journeyman Programmer

If you are asking how to get hold of text input value in a java session bean, it has nothing to do with your javascript code.

如果您询问如何在 java 会话 bean 中获取文本输入值,它与您的 javascript 代码无关。

The session bean is server side code. To pass the input value to your session bean, you need to change server side code, a servlet, strut action, webwork action depending on which web framework you use.

会话 bean 是服务器端代码。要将输入值传递给会话 bean,您需要根据您使用的 Web 框架更改服务器端代码、servlet、strut 操作、webwork 操作。