是否可以在 servlet/jsp 和 javascript 函数之间共享会话变量?
我有一个场景,必须尽可能轻松地填写表格。最初向用户询问要在表单中填写的值并将其存储在数据库中。
之后,当用户单击按钮时,将调用 JavaScript 函数,该函数会使用 jsp/servlet 获取的值自动填充表单中的所有字段。
我想在 servlet/jsp 的帮助下从数据库检索值(要填写到表单中)...现在应该有某种方法让 javascript 函数填写这个值(通过servlet/jsp) 到表单中...
是否可以在 jsp/servlet 和 javascript 函数之间共享数据/变量? javascript 函数将成为 jsp 的一部分(检索应在表单中填写的值)。
I have a scenario where a form has to be filled in as easily as possible. The values to be filled in in the form, are asked from the user initially and stored in database.
Afterwards, when user clicks on a button, a javascript function is invoked that auto-fills all the fields in the form with values obtained by jsp/servlet.
I want to retrieve the values (that are to be filled in into the form) from database with the help of servlet/jsp...Now there should be some way for the javascript function to fill in this value (that was obtained by the servlet/jsp) into the form...
Is it possible to do such sharing of data/variables between jsp/servlets and javascript functions? The javascript functions will be part of the jsp (that retrieves the value that should be filled in in the form).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果请求是同步发送的(例如表单提交按钮),那么您不一定需要 JS 来完成这项工作。只需让 JSP/EL 根据 servlet 预先填充的数据立即将它们打印为输入值即可。例如,假设
${user}
是一个由servlet准备的典型Javabean:(
fn:escapeXml()
只是为了防止XSS攻击)如果请求是异步发送的(例如使用ajax),那么你只需要让servlet以易于JS解析的格式返回数据即可,例如JSON。
然后您可以在 ajax 响应回调函数中使用它(其中
user
是获取的 JSON 对象):jQuery 使这样的事情变得更加容易。另请参阅如何使用 Servlet 和 Ajax?
If the request is sent synchronously (e.g. a form submit button), then you don't necessarily need JS for this job. Just let JSP/EL print them immediately as input values based on data which is prepopulated by a servlet. For example, assuming that
${user}
is a typical Javabean which is prepared by the servlet:(the
fn:escapeXml()
is just to prevent XSS attacks)If the request is sent asynchronously (e.g. using ajax), then you just need to let the servlet return the data in a format which is easily parseable by JS, for example JSON.
which you can then use as follows in the ajax response callback function (where
user
is the obtained JSON object):jQuery makes things like this much easier. See also How to use Servlets and Ajax?
浏览器中的 Javascript 客户端无法直接访问服务器上的环境(并且不应该 - 想象一下,如果您决定使用不同的服务器解决方案的那天所有页面都损坏了,那将是多么糟糕)。您基本上有两种选择:
在向客户端提供的页面上包含您需要的所有数据(也许表单 html 是动态生成的以满足您的需求等)
通过 XMLHttpRequest(又名 AJAX)动态手动请求额外信息。如果您只能在页面加载后动态地确定需要哪些数据,那么这是唯一的方法。
The Javascript client in the browser has no direct access to the environment on the server (and shoudln't - imagine how it would be bad if all pages broke the day you decided to use a diferent server solution). You basically have two choices:
Include all the data you will ever need on the page you serve to the client (perhaps the form html is dinamically generated to suit your needs, etc etc)
Manually request extra information dinamically via XMLHttpRequest (aka AJAX). This is the only way if you can only determine what data you will need dinamically, after the page loads.
您可以使用 xmlhttprequest 动态访问数据库。
如需更多信息+相关示例,请阅读:
http://www.w3schools.com/ajax/ajax_aspphp.asp
You could access your database dynamiclly using xmlhttprequest.
For further information + relevant examples read :
http://www.w3schools.com/ajax/ajax_aspphp.asp