• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to pass values between JSP - JSTL - Javascript ?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Sankar B
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must convert them into scoped attributes by using
<% pageContext.setAttribute(..., ...); %>
 
Sankar B
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic