0

Why are ALL the jsp Java expressions evaluated to empty values if not all contain null/empty values? The expressions ARE evaluated, but not to the correct values.

This was working at some point, then I had to do some other changes and only now I noticed that the expressions are not working anymore. I tried to revert my other changes, to bring the code to a a minimum as shown below, but it's still not working. Any idea what I'm doing wrong/missing below? I'm running the webapp in Tomcat 8.5.

This is what's in index.jsp:

<%@ page isELIgnored="false" %> 
<%@ page import="com.aif.User" %>

<html>
    <head>
    </head>

    <%
    User user = new User();
    String emptyString = "";
    String nonEmptyString = "NonEmptyStringValue";
    %>

    <body>
        Java scriptlet with null value: '<%=user.getName()%>' <br /><br />

        Java expression with null value: '${user.getName()}' should display '' <br /><br />

        Java expression with empty value: '${emptyString}' should display '' <br /><br />

        Java expression with non-empty non-null value: '${nonEmptyString}' should display 'NonEmptyStringValue' <br /><br />

    </body>
</html>

The above evaluates to this in the browser:

Java scriptlet with null value: 'null' 

Java expression with null value: '' should display '' 

Java expression with empty value: '' should display '' 

Java expression with non-empty non-null value: '' should display 'NonEmptyStringValue' 

What's in User.java

package com.aif;

public class User {
    private String name;

    public String getName() {
        return name;
    }
}

What's in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
</web-app>

What's in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aif</groupId>
    <artifactId>xyz2</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

</project>
AIF
  • 61
  • 9
  • `user.getName` is null because you never set it – tima Jul 21 '17 at 13:06
  • **user.getName** of course should be null. My question is why are **ALL** Java expression ${} evaluated to empty values even if they are not all null/empty values. – AIF Jul 21 '17 at 13:10
  • is this line your only problem: Java expression with non-empty non-null value: '${nonEmptyString}' should display 'NonEmptyStringValue'

    ??
    – Ankush G Jul 21 '17 at 13:28
  • looking at browser output, everything looks expected except this line, is that the problem you have? – Ankush G Jul 21 '17 at 13:28
  • @vvtx yes, that line is the problem: **'${nonEmptyString}'** should display **'NonEmptyStringValue'** but instead it displays **''** – AIF Jul 21 '17 at 13:33
  • 1
    for that you need to attach the property 'nonEmptyString' to some jsp context. e.g. pagecontext, then you can access this in el expression. e.g. : <% pageContext.setAttribute("nonEmptyString", "NonEmptyStringValue"); %> – Ankush G Jul 21 '17 at 13:36
  • @vvtx, can't that be done automcatically somehow? – AIF Jul 21 '17 at 13:37
  • 1
    No. Sorry. EL need properties in page/request/session/application scope only – Ankush G Jul 21 '17 at 13:40
  • The problem we are having is that we moved from Weblogic to Tomcat and all variables that are set to **null** are now displayed with a value of **null** in the browser. Weblogic was displaying all null variables as empty values in the browser. We're trying to do the same in Tomcat now. That's why I was trying to just replace all scriptlets of this type **<%= =>** with Java expressions **${ }**, but if that's not enough and we'd have to also add all the values manually in the jsp context, that will require too much manual work and we'll have to find a different solution. What else can we try? – AIF Jul 21 '17 at 13:45
  • I am not sure about Weblogic but tomcat does not show null if datatype is string, it shows blank string. And yes for EL you have to have contexted property at least for tomcat. – Ankush G Jul 21 '17 at 13:51
  • In the code above is running in Tomcat 8.5 and when **'<%=user.getName()%>'** receives a **null String** to display, it's displaying it in the browser as **'null'** not **''**. Printing **null** values for null variables seems to be the expected behavior in Tomcat: https://stackoverflow.com/questions/3095419/how-can-i-get-tomcat-to-output-empty-string-instead-of-null – AIF Jul 21 '17 at 14:01
  • <%=%> this is expression tag which will show "null" for null stringtype values, I was talking about null stringtype values inside EL {} , which will always show blank string for null string. – Ankush G Jul 21 '17 at 14:19
  • @vvtx Yes that is happening: variables set to null or empty values are displayed by ${} as empty values. So **the expressions are evaluated**. I don't understand why PM77-1 and BlausC consider that the Java expressions above are **not** evaluated. The problem is that when **${}** receive a non-empty non-null value, they are still displayed as an empty value. – AIF Jul 21 '17 at 14:23
  • 1
    In the end I used this solution with our existing Java scriptlets because it's the one involving the least amount of code changes: I overwrote JspWriter, JspFactory and PageContext: https://stackoverflow.com/questions/29508245/jsp-using-a-delegate-for-out-jspwriter-with-jsp-includes-to-change-the-beh – AIF Jul 22 '17 at 16:06

0 Answers0