<c:out value="Test"/> inside jspx doesn't show in browser

1.4k Views Asked by At

When I try to test my jstl lib (jstl-1.1.2.jar & standard-1.1.2.jar) by adding it doesn't show anything in the browser.

this is my home.jspx:

<?xml version="1.0" encoding="utf-8"?>
<jsp:root
    xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns="http://www.w3.org/1999/xhtml"
    version="2.1">

<jsp:directive.page contentType="text/html" pageEncoding="UTF-8" />
<jsp:output omit-xml-declaration="true" />
<jsp:output doctype-root-element="HTML"
            doctype-system="about:legacy-compat" />

<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>gdgdfgdg</title>

</head>
<body>
<h1>fsdf</h1>
<c:out value="Test"/>
</body>
</html>

</jsp:root>

Necessary jars are added. Can't figure out what's the problem.

EDITED:

This is the source code, the browser receives:

<!DOCTYPE HTML SYSTEM "about:legacy-compat">
<html lang="en">
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="content-type"/>
    <title>gdgdfgdg</title>
  </head>
  <body>
    <h1>fsdf</h1>
    <c:out value="TEST"/>
  </body>
</html>

I added the following dependencies to maven and it loaded the right jars as described at the top properly..

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
2

There are 2 best solutions below

3
On

Try this simple .jsp page if anything works at all. Test page uses random fmt and c features.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ 
    taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><%@
    page contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"
    import="java.util.*,
    java.text.*
    "
%><%

String var0 = "Foodoo0 " + System.currentTimeMillis();
pageContext.setAttribute("var1", "Foodoo1 " + System.currentTimeMillis());
pageContext.setAttribute("var2", "Foodoo2 " + System.currentTimeMillis());

double distance = 1234.567;
pageContext.setAttribute("distance", distance);

%><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test page</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>

var0=<%= var0 %> <br/>
var1=${var1} <br/>
<c:out value="COut Foo" /> <br/>
var2=<c:out value="${var2}" /> <br/>

<br/>
Use locale fi_FI<br/>
<fmt:setLocale value="fi_FI" scope="page" />
<c:set var="distanceEL" value="${distance}" />
fmt0=${distance} | ${distanceEL} <br/>
fmt1=<fmt:formatNumber pattern="0.0" value="${distance}" /> <br/>
fmt2=<fmt:formatNumber pattern="0.00" value="${distanceEL}" /> <br/>
fmt3=<%= new DecimalFormat("0.0").format(distance) %>

<br/>
Use locale en_US<br/>
<fmt:setLocale value="en_US" scope="page" />
<c:set var="distanceEL" value="${distance+3456.78}" />
fmt0=${distance} | ${distanceEL} <br/>
fmt1=<fmt:formatNumber pattern="0.0" value="${distance}" /> <br/>
fmt2=<fmt:formatNumber pattern="0.00" value="${distanceEL}" /> <br/>
fmt3=<%= new DecimalFormat("0.0").format(distance) %>

</body>
</html>

I have used these libraries in Tomcat6 server, but they are a bit old. Have not bothered to update cause Tomcat6 itself is a legacy anyway.

  • lib/jstl-api-1.2.jar
  • lib/jstl-impl-1.2.jar
  • lib/el-api.jar_OLD (renamed old tomcat .jar to disable it)
  • lib/el-api-1.1.jar
  • lib/el-impl-1.1.jar

These are the files I use in Tomcat7 server, Tomcat7 has lib/el-api.jar and lib/jasper-el.jar which are fine. No need to update them.

  • javax.servlet.jsp.jstl-api-1.2.1.jar
  • javax.servlet.jsp.jstl-1.2.2.jar

Download fresh jars from http://search.maven.org/#browse|707331597 and http://search.maven.org/#browse|-1308691387 links.

Make sure use proper webapp specs version attribute in mywebapp/WEB-INF/web.xml file. This is Tomcat6 webapp.

<?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"
>
  <description>My webapp</description>
  <display-name>My webapp</display-name>
</web-app>

This it Tomcat7 webapp.

<?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" 
>
  <description>My webapp</description>
  <display-name>My webapp</display-name>
</web-app>
0
On

The provided jspx works fine in my environment, Tomcat 6.0 and with the following:

<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl-api</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>jstl-impl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>el-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.1</version>
    <scope>provided</scope>
</dependency>

According to Apache, 1.1.2 taglibs should work on Tomcat 5.0 or later, but since you included the JSP version 2.1 in the head of the jspx, you need to run on a Servlet spec 2.5 or later server, such as Tomcat 6.

Your best bet is to use the dependencies i've provided and make sure your servlet container is recent.