0

I've got an if statement in a bean that seems to be processing fine when I create a test java class, but doesn't work fine when the bean is invoked by a jsp.

My code, let me shows you it:

First, the test class:

package com.serco.inquire;
import java.util.*;
import java.text.*;

public class TestCollection {
    public static void main(String[] args) {
        IrCollection myCollection = new IrCollection();
        myCollection.setSort("none");
        myCollection.setMgrid("none");
        int endpoint = myCollection.getSize();
        for (int i=0;i<endpoint;i++) {
            InquireRecord curRec = myCollection.getCurRecords(i);
            Long milis = new Long(curRec.getSubmitDate());
            Date theDate = new Date(milis);
            Format formatter = new SimpleDateFormat("dd MMM yyyy");
            String s = formatter.format(theDate);
            System.out.println("ID: " + curRec.getID() + " | Subject: " + curRec.getSubject());
        }
    }
}

a snippit from the IrCollection class it calls:

private void processSort(String datum) {
    int LastChar = datum.length()-1;
    String colName = datum.substring(0, LastChar);
    if (datum=="none") {
        this.fullSort  = " ORDER BY lastUpdated DESC";
    } else {
        if (datum.endsWith("2")) {
            this.fullSort = " ORDER BY " + colName + " ASC";
        } else {
            this.fullSort = " ORDER BY " + colName + " DESC";
        }
    }
}

There's more code in another method that calls this particular method using:

this.processSort(this.sort);

But the problem is the if (datum=="none") portion in the second code sample. Given that line 10 of the first class sets the member variable sort to "none", that processSort() method should set the member variable fullSort to " ORDER BY lastUpdated DESC".

And if I use the class in the first sample, it does that.

HOWEVER

I have this custom tag:

<%@ tag body-content="scriptless" import="com.serco.inquire.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="mgr" required="true" %>
<%@ attribute name="mkind" required="false" %>
<%@ attribute name="sort" required="false" %>
<c:if test="${empty mkind}">
    <c:set var="mkind" value="manager" />
</c:if>
<c:if test="${empty sort}">
    <c:set var="sort" value="none" />
</c:if>
<jsp:useBean id="irc" scope="page" class="com.serco.inquire.IrCollection">
    <jsp:setProperty name="irc" property="mgrtype" value="${mkind}" />
    <jsp:setProperty name="irc" property="sort" value="${sort}" />
    <jsp:setProperty name="irc" property="mgrid" value="${mgr}" />
</jsp:useBean>
${irc.fullsort}

Which the .jsp file invokes with this:

<c:set var="user" value="none" />    
<c:set var="sort" value="none" />
<inq:displayCollection>
    <jsp:attribute name="mgr">${user}</jsp:attribute>
    <jsp:attribute name="mkind">cotr</jsp:attribute>
    <jsp:attribute name="sort">${sort}</jsp:attribute>
</inq:displayCollection>

In other words, the exact same data is fed to the IrCollection bean. so I should get the same data, right?

Except I get this:

WHERE cotr = 'none' ORDER BY non DES

so when Java calls it, it thinks "none" == "none", but when jsp calls it, it thinks "none" != "none".

The E
  • 697
  • 1
  • 9
  • 23

5 Answers5

2

You have to use equals instead of ==:

datum.equals("none")

Refer to whats the difference between “.equals and ==”

Community
  • 1
  • 1
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • yeah... just noticed. that. COming back to acknowledge. Thanks for all the answers folks. – The E Jun 23 '11 at 18:16
0

datum == "none" is wrong. You want

datum.equals("none");

The string == operator compares only the pointer locations, not the actual value of the String. So it will work in some situations (if datum was set with a constant string) but not when dynamically created.

Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106
0

use equals for string comparsion

Cris
  • 4,947
  • 6
  • 44
  • 73
0

i will prefer to use, equals() method, instead of comparing the reference variables, it might give proper result in same cases, but one should check if the string is logically equal or not.

M.J.
  • 16,266
  • 28
  • 75
  • 97
0

I think your issue is with Object equality in Java. Like others have mentioned, you should use equals(). It is possible that the compiler for your class creates a shared string for each instance of a literal (like "none"). This would cause ==, which checks to see if two variables point to the same object, if the string was assigned from within the class. Then when you use your JSP page, the "none" string is provided by an external class, causing it to be a different object.

The moral of the story is to use 'equals()' to compare strings.

Ryan Gross
  • 6,423
  • 2
  • 32
  • 44