如何在javascript中访问servlet变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21457408/
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
How to access a servlet variable inside javascript
提问by Arpit
I have a servlet which has a variable which holds a JSON string.
我有一个 servlet,它有一个包含 JSON 字符串的变量。
JSONObject obj = new JSONObject();
obj.put("parameter", jsonList);
request.setAttribute("jsonstring", obj.toString());
RequestDispatcher rd = request.getRequestDispatcher("/file.jsp");
rd.forward(request, response);
Now I am forwarding my request and response objects to a JSP page which contains a JS file. How can I access the value of jsonstringvariable inside the JS file.As I need to parse my JSON string further using jQuery.
现在我将我的请求和响应对象转发到包含 JS 文件的 JSP 页面。如何访问JS 文件中jsonstring变量的值。因为我需要使用 jQuery 进一步解析我的 JSON 字符串。
I tried doing this in my JS file as I saw in some of the posts online.But it seems like its not working for me.
正如我在一些在线帖子中看到的那样,我尝试在我的 JS 文件中执行此操作。但它似乎对我不起作用。
var test = '<%=request.getAttribute("jsonstring")%>'
Kindly guide me on the same.Thanx.
请指导我。谢谢。
回答by guli
Try this :
尝试这个 :
var test = <%=request.getAttribute("jsonstring")%>;
Else try that :
否则试试:
var test = eval('<%=request.getAttribute("jsonstring")%>');
回答by Anthony Grist
If the JSP file uses a <script>
tag to load an external JavaScript file, something like this:
如果 JSP 文件使用<script>
标记加载外部 JavaScript 文件,则如下所示:
<script type="text/javascript" src="js/yourFile.js"></script>
then it won't work. What you have is a JSP scriptlet which only means something during the execution of the JSP file. That yourFile.js
file won't be parsed server-side because it doesn't need to be. Only the browser will know what to do with the <script>
tag and issue a request to the server to load the JavaScript.
那么它就行不通了。您拥有的是一个 JSP scriptlet,它只在 JSP 文件的执行过程中有意义。该yourFile.js
文件不会在服务器端解析,因为它不需要。只有浏览器知道如何处理<script>
标签并向服务器发出加载 JavaScript 的请求。
In these cases what you can do is something like this inside your JSP:
在这些情况下,您可以在 JSP 中执行以下操作:
<script type="text/javascript">
var test = <%=request.getAttribute("jsonstring")%>;
</script>
<script type="text/javascript" src="js/yourFile.js"/></script>
Since the JSP scriptlet is inside your JSP file it now actually gets processed correctly, and instead outputs the valid JSON for the object/array to store in the globally scoped test
variable, which you can then refer to inside yourFile.js
.
由于 JSP scriptlet 位于您的 JSP 文件中,因此它现在实际上得到了正确处理,而是输出对象/数组的有效 JSON 以存储在全局范围test
变量中,然后您可以在内部引用该变量yourFile.js
。
回答by Sanju2014
put object direct in the request like this
像这样将对象直接放入请求中
request.setAttribute("jsonstring", obj);
then assign request obj to js variable, it will be accessable as json object;
然后将请求 obj 分配给 js 变量,它将作为 json 对象访问;
var test = <%=request.getAttribute("jsonstring")%>;
... = test.parameter;
回答by jorge santos
The solution for me was the following:
我的解决方案如下:
<script type="text/javascript">
var value =<%=request.getAttribute("indicadorId") %>;
</script>