wood burning stoves 2.0*
The moose likes JSP and the fly likes How to pass values between JSP - JSTL - Javascript ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to pass values between JSP - JSTL - Javascript ?" Watch "How to pass values between JSP - JSTL - Javascript ?" New topic
Author

How to pass values between JSP - JSTL - Javascript ?

Sankar B
Greenhorn

Joined: Sep 23, 2002
Posts: 7
Dear All,
Is it possible to share the variable declared in JSP from JSTL and from JSTL to Javascript ?
Example:
String x="ID";
out.println(x);
But, when I access this using JSTL out tag,
value="${x}"
Im not getting the value displayed. How to access it.
But, when i put the x value in
pageContext.setAttribute("jstlX",x);
Im able to get the value of the 'jstlX' using the out Tag.
How to access the value of a variable declared in JSP from JSTL Tag ?
How to pass value from Javascript to JSTL and JSTL to Javascript ?
Yours,
Sankar.B
Information Dynamics, Chennai, India
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
It doesn't look like you're talking about JavaScript, but about Java scripting elements in your JSP pages. The answer is to create a scoped attribute, as you've discovered:
<% pageContext.setAttribute("x", x); %>
There is no way for JSP tags to read scripting variables; there's no way around that because of the way JSP is designed.
See chapter 3 of JSTL in Action for more info on the JSTL expression language (EL).


Shawn Bayern<br />"JSTL in Action" <a href="http://www.jstlbook.com" target="_blank" rel="nofollow">http://www.jstlbook.com</a>
Sankar B
Greenhorn

Joined: Sep 23, 2002
Posts: 7
Hi,
See my problem is as follows:
In a JSP page, using scriptlet, we declare some variable. We may do some calculations on it based on the User input in few of the form fields by passing it to Javascript. Ok.
Now, my Q is how to use a variable which is declared in Java Scriptlet from JSTL ?
Ex: String x; int p,q; Now, how to access these variables from JSTL ?
Yours,
Sankar.B
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
You must convert them into scoped attributes by using
<% pageContext.setAttribute(..., ...); %>
Sankar B
Greenhorn

Joined: Sep 23, 2002
Posts: 7
Dear Shawn,
Ok, Ill use pageContext to make the variable a scoped attribute.
But, when i use the c:set to set a variable, how to call it from a scriplet ?
c:set var="jstl" value="Shawn"
% String jsp = pageContext.getAttribute("jstl") %
is giving error.
How to call 'jstl' wich is a scoped attribute set in JSTL from a scriptlet ?
Yours,
Sankar.B
Sankar B
Greenhorn

Joined: Sep 23, 2002
Posts: 7
Hi
Sorry for asking this. Actually first ive used the getAttribute(jstl), i.e. without double quotes. So it gave error. After that, i used the getAttribute("jstl"), but, without converting it toString() ive used.
But, still im doubt whether without using JSP scriptlet can we use JSTL ? Cos, there is no designers and all here. All html coding as well as jsp coding we developers do. Cos, this is not a web site, we develop web based applications. So, we need to use JSP coding in html pages a lot.
Also, I think, if we start JSTL coding, we will forget Java syntax. Cos, all the JSTL tags use different syntax for all.
So, we fear whether we can utilise JSTL or stick with JSP itself or switch over to JSF when its released.
Yours,
Sankar.B
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
Originally posted by Sankar B:
So, we fear whether we can utilise JSTL or stick with JSP itself or switch over to JSF when its released.

Faces isn't designed to operate in precisely the same space; it doesn't replace either JSTL or scriptlets. JSTL, however, is designed to replace scriptlets (in concert with JSP 2.0's features).
Sankar B
Greenhorn

Joined: Sep 23, 2002
Posts: 7
Hi,
What yar. Then u might've provided to declare variable in JSTL and use it without making it as an scoped attribute. Cos, declaring variables are just used for some temporary operations within that page.
Or, Is there any option to declare variables in JSTL and use it without making it as an Scoped Attribute ?
Ex: int x,y; String myString; like this. But, without making it as a scoped attribute.
Yours,
Sankar.B
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
Originally posted by Sankar B:
[QB]Hi,
What yar. Then u might've provided to declare variable in JSTL and use it without making it as an scoped attribute. Cos, declaring variables are just used for some temporary operations within that page.

I think you're fundamentally misunderstanding the way that JSP works. It is not possible for a tag to read a scripting variable. JSTL could not have done anything in this regard; it's structurally impossible based on the way that JSP compiles pages into Java servlets.
What is wrong with converting a variable into a scoped attribute? It takes a single line of code and incurs an extremely minor runtime cost.

Or, Is there any option to declare variables in JSTL and use it without making it as an Scoped Attribute ?
Ex: int x,y; String myString; like this. But, without making it as a scoped attribute.

You can take care of both at once with a single <jsp:useBean> tag, whether you're using a bean or not; e.g., <jsp:useBean> works with Strings.
You may want to go back to first principles and read the JSP and JSTL standards.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to pass values between JSP - JSTL - Javascript ?
 
Similar Threads
Calling Java Class method from JS
JSTL and scriplets together doesn't work
questions about JSTL variables
Help with JSP
How to pass enum values from jsp page to Servlet