views:

580

answers:

5

I have some questions which are as follows:

  1. How can I use the JSP variable/array in JQUERY code? Here what ever the JQUERY code we have is stored in separate .js file and that file is included in the JSP file.

  2. Actually I want to initialize the JQUERY array with the JSP variable. So please guide me to achieve this task.

A: 

Try to convert them to JS constants. There is no constants in JS but let it be so called constant. Just do not change it anywhere else.

<script>
  var const = yourJSPvariable;

And use CONST in your js file

Artic
JavaScript doesn't have constants. This is just a variable with a name in ALL-CAPS. And you've failed to mention anything about converting the data into a format that JS can handle (strings need to be escaped, more complex objects have to be serialised, etc).
David Dorward
Just conception not kind of realization. Think it is more useful then peaces of code. Make you think.
Artic
Please put the reason of down voting. Be polite and think about if you were me before putting down vote.
Artic
A: 

Not directly. You'd have to set it into the page somewhere.

<script>
   var fromJsp = '${theVar}';
</script>

Note that this could get tricky for complex objects. Maybe JSON serialization can be your friend here.

Also note that this is one-way only. It is impossible to set the value of JSP variables from JavaScript (since the JavaScript runs client-side after the JSP has finished its work).

Thilo
Note the XSS implications: String theVar = "hello'; alert(\"XSS\"); //"
Olaf
A: 

You can use this -

<input type="hidden" id="var1" value="<%=jspVar%>" />

and then use var1 in jQuery.

Padmarag
Assuming that the variable is a string and that it doesn't contain a " character.
David Dorward
+4  A: 

In Plain Old JSP

<script>
  var someText = "<%= myBean.getText() %>";
</script>

Using EL (Expression Language)

<script>
  var someText = "${myBean.text}";
</script>

Using Struts

<script>
  var someText = "<bean:write name="myBean" property="text" />";
</script>

Using JSTL

<script>
  var someText = "<c:out value="${myBean.text}" />";
</script>

In essence, it's possible to populate Javascript objects from JSP. Don't forget that scriptlets and tags are just rendered back as HTML/XHTML, so JS cannot talk to tags and vice-versa.

The Elite Gentleman
The 2nd example doesn't use JSTL at all. It's called EL (Expression Language).
BalusC
Thanks BalusC...I was looking for that word....I've updated it ...
The Elite Gentleman
But EL was part of JSTL - http://www.jcp.org/aboutJava/communityprocess/first/jsr052 (Appendix A) :)
Jack Leow
As this answer floats to the top I'll repeat my comment from another answer, especially for the "Plain Old JSP" solution: Note the XSS implications: String theVar = "hello'; alert(\"XSS\"); //"
Olaf
True Olaf...hence it's better to use JSTL `<fmt />` for such implications and move away from "Plain Old JSP".
The Elite Gentleman
Erm, the `fmt` taglib doesn't take care about XSS. The `c:out` and `fn:escapeXml` does.
BalusC
@BalusC, true....I used the `fn:replace` to solve XSS, I was thinking struts when I wrote this. In struts you can handle XSS by allowing `filter="true"` on `bean:write`
The Elite Gentleman
A: 

Java/JSP runs in webserver at the server machine and produces HTML/CSS/JS code. Server machine sends HTML/CSS/JS code to client machine. HTML/CSS/JS runs in webbrowser at client machine. Rightclick page and view source, you don't see any Java/JSP code.

JSP is a view technology which provides a template to write HTML/CSS/JS in and the ability to interact with backend Java data using taglibs/EL to control page flow and access data.

Whenever you want to let JavaScript access Java/JSP variables, all you need to do is to just write a Java variable as if it is a JavaScript variable.

<script>var foo = '${bean.foo}';</script>

Is an excellent example. Note that those quotes are required for JavaScript itself, not for JSP/EL. Imagine that ${bean.foo} returns bar, then the generated HTML/CSS/JS page which arrived at the client side would end up looking like:

<script>var foo = 'bar';</script>

Whenever you want to let Java/JSP access JavaScript variables, all you need to do is to let JavaScript fire a (XML)HTTP request. More background info and examples can be found in this article.

BalusC