This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL won't work

 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning EL. I can not see the value x using EL

--------------------
<%! int x = 4;%>

<br>
EL expression for x=${x}
----------------------------
I am using apache-tomcat-5.5.17.


Thanks
 
Anupama Ponnapalli
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to enable it first

<%@page isELIgnored="false"%>

You would also have to add it to the pageContext as an attribute

pageContext.setAttribute("name", value);
[ March 06, 2007: Message edited by: Anupama Ponnapalli ]
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works!

Thanks Anupama !
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we conclude that for EL to get any variable, it should be some where in pageScope, requestScope etc

Any one please confirm
 
Anupama Ponnapalli
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tiffiny Yang ,

I forgot to mention this: EL is enabled by default. If you are using a servlets 2.4 container like tomcat 5, you do not need to say isELIgnored=false.

Pls check you web.xml to make sure that you are using the right version for you app too. This from one of my apps:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
...

</web-app>
 
Anupama Ponnapalli
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amir,

yes, the object must be an attribute in one of the 4 scopes. I believe it uses the JspContext findAttribute method internally.

Regards,
Anu
 
Manju Devarla
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is not printing the value of x when we say ${x}

i am using Tomcat 5+..By deafult EL is enabled so what should be done to print this..
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will enabled if you actually use the correct version of web.xml.
Please check the following :
http://faq.javaranch.com/view?ElOrJstlNotWorkingAsExpected
 
Manju Devarla
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my web.xml is correct with
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2.4.xsd"
version="2.4">

but still why the value of x is not getting printed..
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manju, do you have a scoped attribute named x? If you believe that you do, would you minded posting the code to show how you are going about it?
 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello manju,
Have you tried the following snippet.
<%pageContext.setAttribute("name", "ashok");%>
my name is ${name}
it works..
 
Chaminda Amarasinghe
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is not printing the value of x when we say ${x}


because you have not placed "x" in a scope as an attribute.
 
Manju Devarla
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%! int x = 4;%>

<br>

<%pageContext.setAttribute("x", "10");%>

EL expression for x=${x} // prints 10

Now i am able to print value of x as 10..But How do i do it with just
declaration with out setting with pageContext.setAttribute(name,value)

ie The value of x as 4..?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. The EL has no access to scripting variables. The whole point of the JSTL and EL is to eliminate the use of scriplets.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic