0

Im having trouble understanding exactly how JSP works in terms of sessions...basically I am setting a session in a different JSP as follows:

<%
String category = request.getParameter("category");
session.setAttribute("category", category); %>

then in another page I am using if conditions to generate HTML based on which category has been posted e.g.:

 <% String category = (String) session.getAttribute("category");

if(category == "movie") { 
out.println("Movie Details"); 
} else if (category == "music") { 
out.println("Music Details"); 
} %>

But it seems neither of the two if statements are being hit but if I actually print out the category variable it is printed out correctly i.e. movie or music is being displayed. Is there some concept of sessions which I have not grasped? I have searched endless pages trying to find an answer for this :/ Thanks in advance.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Steven
  • 3
  • 1
  • 2

2 Answers2

2

You are comparing strings the wrong way. You should use equals(..) instead of ==

== checks whether the instances are identical, while .equals(..) checks if the two strings have the same contents. You are very rarely interested in the former.

So, for objects (unlike primitives, where == is the way to go), use:

if (foo.equals(bar)) { .. }

When you have some experience with java and the servlet API, you should consider some best practices with JSPs. The most important one is not to write java code within JSPs. Here is an extensive explanation of how and why to do that.

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    Not to mention the evilness that are scriptlets... :( – Matt Ball Apr 11 '11 at 21:08
  • I couldn't have asked for a better explanation to what I was doing wrong...I feel so dumb after reading your explanation. Also relating to the topic on sessions...I am trying to pass that variable (containing the value retrieved from the session) into a useBean method e.g: <% String[] something = myclass.doSomething(category); %> But the variable something is failing to be populated this way but if I had to specify the value "movie" in the doSomething() method it would populate the array... is there something I am vaguely missing? – Steven Apr 11 '11 at 21:38
  • @Steven you can ask a new question for that. – Bozho Apr 12 '11 at 08:18
1

Bozho already answered the real cause of the problem. In Java, object values are to be compared by equals() method. Strings are objects, not primitives. Please note that this problem is not related to JSP, but to basic Java.

I just wanted to point out the correct way to go about this: use taglibs/EL. First install if necessary JSTL (Tomcat for example doesn't ship with it out the box) and then declare it in top of your JSP.

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

Your first code snippet can be achieved as follows:

<c:set scope="session" var="category" value="${param.category}" />

Your second code snippet can be achieved as follows:

<c:if test="${category == 'movie'}">Movie details</c:if>
<c:if test="${category == 'music'}">Music details</c:if>

or

<c:choose>
    <c:when test="${category == 'movie'}">Movie details</c:when>
    <c:when test="${category == 'music'}">Music details</c:when>
    <c:otherwise>Unknown category</c:otherwise>
</c:choose>

Yes, comparing strings by == is valid in EL. It will under the covers use equals() for that.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555