javascript 在 jsp 字符串变量中存储一个 java 脚本值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15093245/
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
Store a java script value in jsp String variable
提问by Vijay Kumar Khushlani
Can anyone help me to solve my simple problem of my jsp code? here is my jsp page with JavaScript
谁能帮我解决我的jsp代码的简单问题?这是我使用 JavaScript 的 jsp 页面
function fid()
{
var id=document.getElementById("id").value;
alert(id);
<%
String demo=id; // store id value of java script but not working
or find(id); // or call a function with parameter of java script var value
// both are not working
%>
}
here is my html page --
这是我的 html 页面——
Enter Student Id<input type='text' id='id'><button type='button' onclick='fid()'>Find</button>
How do I pass the js value in jsp function.
I know that I can do this with ajax or jquery but I don't know these languages. Any suggestions or code snippets are welcome.
如何在jsp函数中传递js值。
我知道我可以用 ajax 或 jquery 做到这一点,但我不知道这些语言。欢迎任何建议或代码片段。
回答by Chic
Server-side vs Client-side
服务器端 vs 客户端
In order to allow your server to respond to user input there has to be a request sent. You can ask your server can send to send a response in one of two ways:
为了让您的服务器响应用户输入,必须发送请求。您可以要求您的服务器可以通过以下两种方式之一发送响应:
- A completely new page - such as when a link is clicked and a new HTML page loaded
- A AJAX response - run asynchronously and formatted however you want so that your client side JavaScript can use it.
- 一个全新的页面——例如当一个链接被点击并加载一个新的 HTML 页面时
- AJAX 响应 - 异步运行并按照您的需要格式化,以便您的客户端 JavaScript 可以使用它。
But the server cannot manipulate the DOM of your existing page by itself. Even with AJAX, it has to have some code (usually JavaScript) on the client side to manipulate the page.
但是服务器不能自己操作现有页面的 DOM。即使使用 AJAX,它也必须在客户端有一些代码(通常是 JavaScript)来操作页面。
Normally if you are doing a search, I would recommend option 1. This has the benefit of being simpler and it allows your users to navigate back/forwards or bookmark their search (assuming you make a GET call vs POST). See When to Use AJAX and When Not To
通常,如果您要进行搜索,我会推荐选项 1。这样做的好处是更简单,它允许您的用户向后/向前导航或为他们的搜索添加书签(假设您进行 GET 调用与 POST)。查看何时使用 AJAX,何时不使用
Using AJAX
使用 AJAX
If you want to use AJAX, make a call to your server create your xmlhttp object and then then you can pass your variable as @A4L mentioned only using AJAX. It would look something like this:
如果您想使用 AJAX,请调用您的服务器创建您的 xmlhttp 对象,然后您可以将您的变量作为 @A4L 提到的仅使用 AJAX 传递。它看起来像这样:
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// Do what you want with the results from your server
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",'myJSP.jsp/?id=' + id,true);
xmlhttp.send();
Then your server can pull in the parameter like @A4L mentioned. You may want to have your response just be straight text or an HTML block that the javascript can place inside a DIV but that's all up to you.
然后你的服务器可以拉入像@A4L 提到的参数。您可能希望您的响应只是纯文本或 HTML 块,javascript 可以将其放置在 DIV 中,但这完全取决于您。
Disclaimer: I'm no expert in AJAX. Most of this is something you can learn from the W3 Schools site
免责声明:我不是 AJAX 方面的专家。您可以从W3 Schools 网站上学到大部分内容
回答by Chic
This solution does not involves js. As you said your js and jsp scriplet are on the same jsp. Try this instead:
本方案不涉及js。正如您所说,您的 js 和 jsp 脚本在同一个 jsp 上。试试这个:
<%
//find form was submit, if cmd is not null
if(null != request.getParameter("cmd")) {
String id = request.getParameter("id");
//your logic to find
}
%>
<form action='same.jsp'>
Enter Student Id: <input type='text' id='id' value='${param.id}'/><!-- ${param.id} to restore the previously entered value by user -->
<button name='cmd'>Find</button><!-- removed onclick, added name -->
</form>
P.S.avoid using jsp scriplets, use JSTL instead.
PS避免使用jsp脚本,使用JSTL代替。
回答by A4L
Java script is run on the client, JSP is run on the server. If you want your JS-variable to come to the server and thus be accessible from the JSP than you have to send it as request parameter to the JSP using something like var url = 'myJSP.jsp/?id=' + id
and in your jsp you can retrieve it using request.getParameter("id")
Java 脚本运行在客户端,JSP 运行在服务器上。如果您希望您的 JS 变量到达服务器并因此可以从 JSP 访问,那么您必须使用类似的东西将其作为请求参数发送到 JSP,var url = 'myJSP.jsp/?id=' + id
并且在您的 jsp 中,您可以使用request.getParameter("id")
回答by PSR
Your javascript values are client-side, your scriptlet is running server-side. So if you want to use your javascript variables in a scriptlet, you will need to submit them.
您的 javascript 值在客户端,您的 scriptlet 在服务器端运行。因此,如果您想在 scriptlet 中使用您的 javascript 变量,您将需要提交它们。
To achieve this, either store them in input fields and submit a form, or perform an ajax request. I suggest you look into JQuery for this.
为此,请将它们存储在输入字段中并提交表单,或执行 ajax 请求。我建议你为此研究 JQuery。