17

There's a small problem with my servlets/jsp web application. I'm trying to use jstl in jsp page. When I use any tag for example:

<c:out value="${command}"/>

it shows me

${command} 

in my browser instead of parameter 'command' value. I'm using maven (and I guess the problem is here). Here is pom xml dependencies:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

my web.xml declaration tag:

<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">

and jsp part:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>
<title>Parsing results</title>
<link type="text/css" rel="stylesheet" href="css/page.css"/>
<link type="text/css" rel="stylesheet" href="css/table.css"/>
</head>

<body>
<h2 align="center">Results of 
parsing. Parsing method = <c:out value="${command}"/></h2>.......

EDIT: Code, which sets command value, is simple:

request.setAttribute("command", parser.getName());

then goes

request.getRequestDispatcher(redir).forward(request, response);

Tell me please, what I'm doing wrong! Thx!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Gleb S
  • 403
  • 1
  • 5
  • 13
  • Can you show the java code setting the Command value? – Raphael Amoedo May 06 '15 at 15:17
  • what happens when you make that jstl dependency provided too so it isn't deployed along with your application? – Gimby May 06 '15 at 15:18
  • You're not terribly clear as to the concrete problem. You're describing the problem as if you were the enduser not the developer. This is not entirely right. To start, what do you see in JSP-generated HTML output? (rightclick page in browser and *View Source*). Is the `` left unparsed in there? If so, have you confirmed the JSTL JAR file being physically present in the `/WEB-INF/lib` folder of the Maven-produced WAR file? In any case, have you tried the [`javax.servlet:jstl`](http://mvnrepository.com/artifact/javax.servlet/jstl/1.2) dependency instead of `jstl:jstl`? – BalusC May 06 '15 at 15:32
  • @Raphael Amoedo - updated post – Gleb S May 06 '15 at 15:54
  • @BalusC - in browser i get in my h2: Results of parsing. 'Parsing method = ${command}' – Gleb S May 06 '15 at 16:02
  • Ah, in other words, JSTL works fine but EL expression is simply not evaluated? Can you assure that you don't have a `DOCTYPE` in your `web.xml`? And that you don't have any other dependencies in POM? – BalusC May 06 '15 at 16:02
  • 1
    @BalusC - Yes, i have doctype in web.xml ' ' In pom last only dependency is junit. – Gleb S May 06 '15 at 16:06
  • 2
    Get rid of that doctype and retry. – BalusC May 06 '15 at 16:07
  • 1
    @BalusC - IT WORKED!! Thanks a lot!! But can you explain the reason of doctype failure?? – Gleb S May 06 '15 at 16:11

5 Answers5

38

Yes, i have doctype in web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "java.sun.com/dtd/web-app_2_3.dtd"; >

Remove that <!DOCTYPE> from web.xml and make sure that the <web-app> is declared conform Servlet 2.4 or newer and all should be well.

A valid Servlet 3.0 (Tomcat 7, JBoss AS 6-7, GlassFish 3, etc) compatible web.xml look like below in its entirety, without any <!DOCTYPE>:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- Config here. -->

</web-app>

For Servlet 3.1 (Tomcat 8, WildFly 8-11, GlassFish/Payara 4, etc) it look like below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <!-- Config here. -->

</web-app>

For Servlet 4.0 (Tomcat 9, WildFly 12-21, GlassFish/Payara 5, etc) it look like below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">

    <!-- Config here. -->

</web-app>

For Servlet 5.0 (Tomcat 10, WildFly 22-26, GlassFish/Payara 6, etc) it look like below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="https://jakarta.ee/xml/ns/jakartaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
    version="5.0">

    <!-- Config here. -->

</web-app>

When using JSTL 1.1 or newer, you need to assure that your web.xml is declared in such way that the webapp runs in at least Servlet 2.4 modus, otherwise EL expressions won't work in the webapp.

When still having a Servlet 2.3 or older <!DOCTYPE> or <web-app> in web.xml, even though you already have a Servlet 2.4 or newer XSD, then it would still be forced to run in Servlet 2.3 or older modus, causing the EL expressions to fail.

The technical reason is, EL was originally part of JSTL 1.0 and not available in Servlet 2.3 / JSP 1.2 and older. In JSTL 1.1, EL was removed from JSTL and integrated in JSP 2.0, which goes along with Servlet 2.4. So, if your web.xml is declared to run the webapp in Servlet 2.3 or older modus, then JSP would expect to find EL in JSTL library, but this would in turn fail if it's a newer JSTL version, lacking EL.

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • What if I "need" to work with xmi:version="2.0", is there any solution there? – L_Cleo Dec 17 '19 at 15:16
  • Upgrade your server and project. Servlet 2.0 is more than 2 decades old. This isn't right. – BalusC Dec 17 '19 at 15:25
  • I need to find a way! Unless of course I could use scriptlets but it looks really ugly and messy... Is there any other solution to this? – L_Cleo Dec 17 '19 at 15:30
  • Downgrade JSTL to a version compatible with Servlet 2.0. Theoretically, JSTL 1.0 should work (see above answer for technical reason). JSTL 1.1 is for Servlet 2.4 or newer so this is definitely not going to work. Newer JSTL versions such as 1.2 and 1.2.1 are obviously a no-go. – BalusC Dec 17 '19 at 15:33
  • Ok but is the code formatting the same? Can I still iterate through maps and stuff like that? Or is there a slight change in the code... I tried to look something on the internet but I figured that maybe you could suggest me where to look for better understanding because of your experience – L_Cleo Dec 17 '19 at 15:38
  • Wait I've found this on top of my web-xml: Does this means it's using 2.3? – L_Cleo Dec 17 '19 at 15:43
  • Still Working in 2022 Properly. Thanks – Purushottam Jul 10 '22 at 12:44
1

In my case for web.xml file (version="3.0") I had to run the application on Tomcat server v.8 instead of v.7, otherwise I had the same issue as you. Hope this helps someone...

<?xml version="1.0" encoding="ISO-8859-1" ?>
<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">
Dodi
  • 2,201
  • 4
  • 30
  • 39
1

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.

Eljah
  • 4,188
  • 4
  • 41
  • 85
-1

Try placing the jdbc driver class in the WEB-INF -> lib folder and verfiy the versions of servlet and jar file used. In my case, I used mssql-jdbc-8.2.2.jar and update the same in pom.xml

arcane_J
  • 1
  • 3
-1

My JSP page also couldn't recognize the <c:choose></:choose>. Always executed the false condition i.e <c:otherwise>. This is what was happening.

This was an inner JSP page i.e

<jsp:include page="your-inner-page.jsp"/>

The inner JSP was loading first and did not have the below tag libraries. They were placed on the outer JSP. Adding them to the inner worked for me.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
veritas
  • 378
  • 1
  • 6
  • 16
  • Question is not about JSP tags. Question is about EL expressions. Those are completely different things. – BalusC Oct 23 '20 at 21:17
  • @BalusC I understand that. OP is saying did not work. Even my c:choose did not work because tags were missing. How is that not relevant. Why the downvote? – veritas Oct 26 '20 at 13:59
  • Because this answer doesn't answer the question. If JSTL wasn't properly installed, OP wouldn't have seen `${command}` being printed in web browser but a completely empty string in place where some output is expected, and the whole `` tag still being present unparsed in generated HTML output as can be seen via *rightclick, view source* in web browser. – BalusC Oct 26 '20 at 14:35
  • @BalusC If I delete my wrong answer, do I get my two points back? – veritas Oct 26 '20 at 15:25