Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.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/6/mongodb/11.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
如何将变量从java提取到jquery?_Java_Jquery_Jsp - Fatal编程技术网

如何将变量从java提取到jquery?

如何将变量从java提取到jquery?,java,jquery,jsp,Java,Jquery,Jsp,例如,我在java类中有一些变量,如何使用jquery访问它?如果我有一张便条,我会做类似的事情 <% SomeVariableClass var = SomeVariableClass.getInstance()%> 或者我需要从servlet或其他什么调用方法吗?在代码中,您只是在为JSP创建一个变量 您可以通过在答案中创建元素来访问它。 比如: <div id="var">value</div> <---JSP have to reply t

例如,我在java类中有一些变量,如何使用jquery访问它?如果我有一张便条,我会做类似的事情

<% SomeVariableClass var = SomeVariableClass.getInstance()%>

或者我需要从servlet或其他什么调用方法吗?

在代码中,您只是在为JSP创建一个变量

您可以通过在答案中创建元素来访问它。 比如:

<div id="var">value</div>   <---JSP have to reply that.

<div id="var"><%= var.value() %></div>   <--- exemple of JSP code to produce the answer u need.

value如果您想从java类访问某个变量而不在JSP中添加scriptlet,则需要设置一个servlet,该servlet将返回数据,并且需要从jQuery调用该服务

如果可以将scriplets添加到JSP中,那么在JSP中的div下添加脚本

例如:

<div id="java_content">
 <% SomeVariableClass var = SomeVariableClass.getInstance();
    var.callAppropriateMethod();  
    // which will return you the data
%></div>

它将返回内容,现在您可以随心所欲地使用javaContent javascript变量:)

那么如何使用ajax呢?
$.ajax({
        type: 'GET',
        url: 'xxxx',   <-- your url
        success: function(reply) {
                    Alert(reply); <-- just for you to see the reply, u can do whatever..
                 }
       });
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body> 
<% SomeVariableClass var = SomeVariableClass.getInstance()%>
<div id="var"><%=var.getValue()%></div>
</body>
<div id="java_content">
 <% SomeVariableClass var = SomeVariableClass.getInstance();
    var.callAppropriateMethod();  
    // which will return you the data
%></div>
var javaContent = $("#java_content").html();