Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将参数从AJAX传递到JSP页面_Ajax_Jsp_Parameter Passing_Session Variables - Fatal编程技术网

将参数从AJAX传递到JSP页面

将参数从AJAX传递到JSP页面,ajax,jsp,parameter-passing,session-variables,Ajax,Jsp,Parameter Passing,Session Variables,我试图将一个参数从AJAX传递回我的JSP页面。以下是我的示例代码: JS文件: $(document).ready(function() { $.ajax({ type: "GET", url: "URL...", dataType: "xml", success: function(xml) { $(xml).find('Rowsets')

我试图将一个参数从AJAX传递回我的JSP页面。以下是我的示例代码:

JS文件:

$(document).ready(function() { 
            $.ajax({
            type: "GET",
            url: "URL...",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('Rowsets').each(function(){ 
                            var x = $(this).find('Auto_Id').text() // Assign data from Auto_Id into variable x
                    document.form.y.value = x; // Pass the parameter back to the JSP page
                    });
                }
    });
});
<FORM name="form"><input name="y" value="" /></FORM> //value left blank for AJAX to auto-populate
.JSP文件:

$(document).ready(function() { 
            $.ajax({
            type: "GET",
            url: "URL...",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('Rowsets').each(function(){ 
                            var x = $(this).find('Auto_Id').text() // Assign data from Auto_Id into variable x
                    document.form.y.value = x; // Pass the parameter back to the JSP page
                    });
                }
    });
});
<FORM name="form"><input name="y" value="" /></FORM> //value left blank for AJAX to auto-populate
//值留空,以便AJAX自动填充
上面的代码可以工作-我可以得到参数x。但是,是否可以在同一个.JSP页面上将x的值转换为以下格式

<%= session.getAttribute("x") %>

或者,获取x的值并将其传递到java标记中


这样做的目的是在页面加载时(通过AJAX)从XML中获取参数,将参数传递回我的JSP页面,以便我可以使用它动态创建URL(例如http://xyz&Param=“+session.getAttribute(“x”)+”)。请注意,URL必须在jsp页面的java标记中定义。

您不能在Scriptlet中使用Javascript变量。我希望您知道,JSP是在服务器端和进行AJAX调用之前执行的。您应该在代码中做一些调整以实现这一点,在JS中构造URL。像这样,

在JSP中,您可以有,

<input type='hidden' value='<%=dynamicallyCreatedURL%>' id='dynamicallyCreatedURL'/>

您好,我能够在表单中正确地看到我的值。但是,是否可以将动态创建的表单值传递回JSP页面以在其间使用?我试过了,但没有成功。这可能吗?您不能在JSP中恢复“dynamicallyCreatedURL”。因为JSP将在脚本执行之前执行。因为JSP在服务器端运行,脚本在客户端运行。为什么在JSP中需要这个变量?谢谢,因为这是不可能的,所以我决定使用jquery/xslt来呈现我的数据。谢谢