Expression Language in JSP not working [duplicate]
Asked Answered
L

5

34

I am new to JSP and using Expression language. I am using Eclipse Galileo with version 2.5 and Tomcat 6 server . I just want to ask that my simple Expression Language is not printing the vale like if i write ${1>2} which suppose to give false but it is displaying ${1>2} only when it renders the page. But when i am using <c:out value="${1>2}"/> it is printing false correctly. I think there is an issue with tag library. Please kindly suggest me the reason for this i am giving a sample code for this so that you can understand where I am going wrong:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                                 "http://www.w3.org/TR/html4/loose.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Expression Language Example</title>
</head>
<body>

Is 1 greater than 2 using cout  :<c:out value="${1>2}"/>

Is 1 greater than 2 without using cout: ${1>2}
</body>
</html> 

Update as per the answers, here is more information:

I am showing my web.xml how it looks like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ScriptLessJsp</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <description></description>
    <display-name>ElServlet</display-name>
    <servlet-name>ElServlet</servlet-name>
    <servlet-class>com.servlet.El.ElServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ElServlet</servlet-name>
    <url-pattern>/ElServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>Collections</display-name>
    <servlet-name>Collections</servlet-name>
    <servlet-class>com.servlet.El.Collections</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Collections</servlet-name>
    <url-pattern>/go</url-pattern>
  </servlet-mapping>
</web-app>

And in my lib folder I have added only jstl.jar so that i can make use of <c:out> tag to display but my EL for template text is not working.

Longdistance answered 30/1, 2010 at 18:4 Comment(0)
T
58

I'm quoting from an answer I provided before to the problem of EL not working:

With other words, the EL expression doesn't get evaluated? That can have one or more of the following causes:

  1. Application server in question doesn't support JSP 2.0.
  2. The web.xml is not declared as Servlet 2.4 or higher.
  3. The @page is configured with isELIgnored=true.
  4. The web.xml is configured with <el-ignored>true</el-ignored> in <jsp-config>.

In your particular case, EL works in taglibs, but not in template text, so I suspect it's caused by point 2. Ensure that your web.xml is declared as at least Servlet 2.4. As Tomcat 6.0 supports Servlet 2.5, I would recommend to declare your web.xml as Servlet 2.5:

<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="Your_WebApp_ID"
    version="2.5">

    <!-- Here you go. -->

</web-app>

Another rare cause I've seen on this is that there's a collision with EL JAR's in the classpath. Ensure that you have not copied any appserver-specific JAR files into your webapp's WEB-INF/lib or, more worse, the JRE/lib.

As you're already using Eclipse and Tomcat, I would review the development steps you used for this all. Ensure that you're using "Eclipse for Java EE developers" and that you've integrated the Tomcat instance in Eclipse's Servers view and that you've created a Dynamic Web Project set to "Servlet 2.5" which makes use of the Tomcat instance. This way everything should go automagically (Eclipse will take appserver's libraries in the build path itself and autogenerate a Servlet 2.5 compliant web.xml).

Update: as per your update: those com.servlet.El servlets look suspicious. What exactly do they do? Parsing EL? Remove them and retry.

Tremann answered 30/1, 2010 at 18:44 Comment(4)
I also noticed that the servlet shouldn't be named "jsp", "default", "ssi", "cgi" - naming it like that will also break JSP functionality as these are built-in Tomcat servlets.Vorster
@BalusC.. I got a same problem. I could solve it with your web.xml modification solution.. Thank you soooo much..Itchy
also it makes sence to add <%@ page isELIgnored="false" %> that only helped to me!!!! Tomcat 7, java 8Isador
Yep. My web.xml version was 2.3. Thank you sir!Marlinemarlinespike
I
27

Setting <%@ page isELIgnored="false" %> at the top of the page helped to me. Don't know why it was the root of the problem in my case. not clear why yet

Isador answered 15/11, 2015 at 17:55 Comment(3)
Your solution is just awesome. After trying lots of other option. Yours is worked for meAnthonyanthophore
Thanks. It worked in my case.Numerator
Oh my god. this worked:) Thanks a lot for saving my time.Garamond
D
12

In my case, I generated a webapp by maven archetype generator, I use maven-archetype-webapp. I need to change two things:

  1. in web.xml, change the head to:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      version="2.5">
    
  2. by default, eclipse generated jsp file with head:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    

Just remove it.

Dolliedolloff answered 12/4, 2014 at 9:11 Comment(2)
I had the problem in an old Tomcat 5.5 and solved it by going from web-app 2.5 to 2.4Howdoyoudo
solved with 2_5.xsd and version="2.5" many thanksLastex
A
4

BalusC covers it, but I'll add these comments:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

The JSTL TLD namespace should include a "jsp" (as above). Given this namespace error may be due to following old instructions, check the most recent documentation to ensure you haven't included any obsolete libraries in WEB-INF/lib. A number of technologies that used to be separate are now included in the container (the EL language being one).

As an aside, I would generally use keywords like gt instead of > and lt instead of < - this is friendlier to XML and its ilk.

Aldenalder answered 30/1, 2010 at 18:50 Comment(0)
C
0

I've had the problem that with the line <!DOCTYPE html> in my JSP, the tomcat started with eclipse didn't interpret the EL correctly. Using the war and copying it to the webapps folder and then starting tomcat with the commandline did the job.

So obviously eclipse tomcat plugin has a problem with <!DOCTYPE html>?!

Coray answered 7/9, 2015 at 19:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.