30

I have a JSP page running on Tomcat 5.5. I have the following code:

 <c:forEach var="i" begin="1" end="10" step="1">
  <c:out value="${i}" />
  <br />
</c:forEach>

The output I am getting is:

${i} 
${i} 
${i} 
${i} 
${i} 
${i} 
${i} 
${i} 
${i} 
${i} 

I cant work out why the forEach loop is working but the output is not working. Any help any one could give would be great.

Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116

9 Answers9

66

I know it's supposed to be on by default, but I run across pages now and again (or even the same page that changes behavior) where the EL processing doesn't happen. Adding the following to the top of any such pages should resolve the issue:

<%@ page isELIgnored="false" %> 

I add it to every page because it doesn't hurt, and I still don't know the root cause that occasionally causes a page to stop interpreting the EL expressions.

RHSeeger
  • 16,034
  • 7
  • 51
  • 41
  • 2
    On Tomcat 5.5, there are (only?) two possible reasons: invalid schema in web.xml, or el-ignored config option. – Peter Štibraný Apr 27 '09 at 15:28
  • 1
    I (and others) have had a single page working fine and, after changing something on the page (ie, adding some html), it just stops evaluating the EL expressions. The only explanation I can come up with is a bug in Tomcat somewhere, but it's not worth digging too deep into since enabling it manually (via the code above) resolves the problem. – RHSeeger Apr 27 '09 at 15:33
  • 4
    If you created your webapp with maven archetype the correct answer is this: http://stackoverflow.com/a/25372735/20654 – OscarRyz Sep 23 '15 at 22:29
  • @OscarRyz That's it. Many thanks! – smwikipedia Jan 18 '16 at 14:35
  • 1
    It's amazing. I got headache whole day until reading this solution. Thank you very much! – Thach Van May 11 '16 at 05:55
39

I just had this same problem and spent forever trying to figure out what was wrong.

I've developed lots of web apps from scratch. Why suddenly was this one not cooperating?

One difference was this time I used the maven webapp archetype to generate the project structure. It created a web.xml file that looked like this:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

Once I realized that as my problem, I was sure I had the answer. So I copied one of my 2.5 web.xml headers, rebuilt, and redeployed. No cigar. Couldn't believe that wasn't the problem. Cleaned the project, restarted tomcat. Nope.

RHSeeger's answer led me to try putting in the <%@ page isELIgnored="false" %>. That resolved the problem. But I still wanted to know why el started getting ignored to begin with.

I figured the el was being ignored because of something wrong with my web.xml, so I closely inspected it in comparison with another webapp's web.xml that I knew worked fine. No noticeable differences.

Then I removed the <%@ page isELIgnored="false" %> from my JSP, and redeployed, assuming the el would not be evaluated again, but much to my surprise, the el was evaluated fine!

Then, figuring it must be some sort of caching problem, I undid my changes to the web.xml to recreate the problem. I redeployed, but still the el was being evaluated correctly, even with the bad web.xml. Then I cleaned my entire project (I'm using an exploded deployment), blowing away the exploded directory and recreating it. I then restarted tomcat. Still, the el appeared to be getting evaluated correctly in spite of the bad web.xml.

Finally it dawned on me. I simply added a space to some place in the JSP, repackaged it, and refreshed the page. Bingo! Now the el was not getting evaluated.

So the problem was with the web.xml. It would further complicated by the fact that the JSPs weren't getting recompiled unless they had changed. Not sure if tomcat uses an MD5 sum to decide if the JSPs need to be recompiled or what. Another possibility is that I'm using tiles, which I know has a caching mechanism, but I wouldn't expect that to survive an tomcat restart.

Anyway, unless you modify your JSPs AFTER fixing the web.xml, all bets are off as to whether the EL will start working again. Hope this saves someone else a headache. I'm also interested if anyone can tell me whether it was tomcat not recompiling the JSPs or tiles caching the output of the JSP. I'm pretty sure it's the recompile, because at compile time is when the JSP should have to figure out what to do with the ${} el expressions, right? Tiles can't actually cache what gets substituted into the el expressions, otherwise all kinds of problems would arise.

kevinmrohr
  • 821
  • 7
  • 11
  • Thanks so much, this is exactly what I have been through - created from archetype, and EL and JSTL all not working, even though JSP file was using exactly same code as some previous app that was working before. Fixed the web.xml namespaces, restarted, still not working; finally modified the JSP, and sorted! – Ken Goh Jan 01 '14 at 22:04
  • It was Tomcat not recompiling the JSPs. It tracks the last modified of the JSP file - newer versions also track the last modified times of the dependencies. These are checked at a configurable frequency (usually 5 seconds if memory serves) and recompilation is triggered if a change is detected. The .java file can be located in Tomcat's work directory and will persist across restarts. – Mark Thomas Nov 07 '14 at 19:28
  • @kevinmrohr Thanks so much for your detailed experiment and answer. – smwikipedia Jan 18 '16 at 14:41
17

It's the header in web.xml that causes the problem

Below maven generated header stops EL from being eval'ed.

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>

Using below header eval's the EL.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
user3954300
  • 171
  • 1
  • 3
8

Make sure to include the relevant namespaces in the web.xml. Just try to replace

<web-app>

with something like

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

It fixed it for me. You can find the correct namespaces for your Tomcat instance in the example apps that come with a Tomcat install.

Bouke Woudstra
  • 261
  • 3
  • 4
2

For those interested, the equivalent XML syntax for JSP 2.0 is:

<jsp:directive.page isELIgnored="false"/>
Fabien R
  • 685
  • 1
  • 8
  • 24
1

had similar problem to Kevin, i used maven to get started with webapp but no joy with evaluating expressions in jsp - i had to remove DOCTYPE header - all is good now without playing with isELIgnored bit - maven generates web.xml which links to 2.3 which as Peter stated has EL disabled by default

greg one
  • 26
  • 2
0

From Controller:

@RequestMapping(value = "createcustomer",method = RequestMethod.GET)
    public String customer(Model model)
    {
        Customer cus=new Customer();
        cus.setCustomerNumber("Test");
        model.addAttribute("customer",cus);
        return "createcustomer";
    }

In View:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> 

<div class="cl">    
   <form:form commandName="customer" method="POST">

      <p>Name: <c:out value="${customer.CustomerNumber}"></c:out></p>

   </form:form>
<div>

Output:

Name: Test
Md Rahman
  • 1,812
  • 1
  • 14
  • 15
0

Use tomcat6. It doesn't requires any configuration for EL in web.xml.

  • 2
    Tomcat 5.5 also doesn't require any configuration for EL in web.xml. Tomcat 5.0 also doesn't. If you have had a problem with it, it lies somewhere else. – BalusC Apr 23 '10 at 11:34
0

See my answer at Javascript String.replace(/\$/,str) works weirdly in jsp file for possible reasons.

Longer answer: ${i} is expression in so called 'Expression Language'. Sometimes, Expression Language can be disabled. See above answer for potential reasons, and ways how to enable it.

Community
  • 1
  • 1
Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116