Click to See Complete Forum and Search --> : JSP newbie - if else statement syntax
Relaxasaurus
03-08-2006, 05:16 PM
This was so easy in PHP & ASP, argh!!
Anyway I have an include navigation file that needs to have a different CSS class per each link when it's on that page. Here's the Nav code:
<% if (thisPage = about_us) { %>
<a href="about_us.jsp" class="selected">About Us</a>
<% } else { %>
<a href="about_us.jsp">About Us</a>
<% } %>
<% if (thisPage = contact) { %>
<a href="contact.jsp" class="selected">Contact</a>
<% } else { %>
<a href="contact.jsp">Contact</a>
<% } %>
And here the code on the About Us page:
<%! int thisPage = about_us; %>
Getting errors abound of course, just wondering if I'm headed in the right direction or even if there's an easier way to do this. Thanks for any help.
BigDog
03-08-2006, 05:24 PM
= is NOT a comparison, it is an ASSIGNMENT.
== is comparison.
Use == to compare and = to assign. here is you code below with comparison instead of assignment.
<% if (thisPage == about_us) { %>
<a href="about_us.jsp" class="selected">About Us</a>
<% } else { %>
<a href="about_us.jsp">About Us</a>
<% } %>
<% if (thisPage == contact) { %>
<a href="contact.jsp" class="selected">Contact</a>
<% } else { %>
<a href="contact.jsp">Contact</a>
<% } %>
Relaxasaurus
03-08-2006, 05:34 PM
ahh thanks! A couple of error messages disappeared. I still have one more:
500 Servlet Exception
Note: sun.tools.javac.Main has been deprecated.
/home/tpgdev22/public_html/about_us.jsp:8: Identifier expected.
thisPage = about_us;
^
1 error, 1 warning
*The carrot mark is between the e (in thisPage) and the = sign
I tried putting "about_us" in quotes but get the same thing. :\
Khalid Ali
03-08-2006, 09:12 PM
== sign is used for integers, floats etc comparision..if you are comparing 2 strings then it will have to be
firstString.equals("secondString");
BigDog
03-09-2006, 09:25 AM
what is "thisPage" ? Is it a String?
Remember that Java is type-safe. You MUST declare all variables. so if "thisPage" is a String, then it must have been declared as:
String thisPage = "some string";
or
String thisPage = new String();
or one of the other constructors for a String.
Also, if it is a string, pay attention to what Khalid said. Is "about_us" an actual string or a variable that holds a string?
If it is a variable that holds a string, you would need to do the following:
if(thisPage.equals(about_us)) {
....
}
if it is itself a string then you would need to do the following:
if(thisPage.equals("about_us")) {
...
}
When you are comparing objects == just compares the memory reference. == works for primitives for comparison though. the .equals() method is used to compare object value. In custom objects you would need to write your own .equals() and .hashcode() methods to get comparrison to work right.
If you are running into these types of issues, you should really pick up a basic book on Java. My recommendation is Head First Java by O'Reilly
Relaxasaurus
03-09-2006, 11:34 AM
Thanks for all your help. To be honest I don't like coding in Java, much prefer scripting in PHP/ASP (based on what I've been doing anyway). It's just that the client needed JSP so we had to do it.
I'll look into that book though, especially if we need to do JSP in the future.
Relaxasaurus
03-16-2006, 10:31 AM
Hi guys, me again ;>
The nav.jsp include holds the if else statement:
<% if(thisPage.equals("home")) { %>
<a href="/" style="color:#1B2C4B; background-color:#C3DCEB">Home</a>
<% } else { %>
<a href="/">Home</a>
<% } %>
The actual page has this code:
<% String thisPage = "home"; %>
First of all it works perfectly fine when I upload it to my JSP server (hosted by Lunarpages), but the client gets errors when he compiles it:
http://www.bluesurfdesigns.com/pics/jsp_error.jpg
It looks like his compiler wants the string declared in each include, when I want to declare it within the actual page that calls on the include (which makes more sense). Is there a reason why it compiles fine for me but not for him? Is there simply a better way to achieve what we want?
Thanks for any help!