• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If loop in Sriptlet

 
Dinesh Remje
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

now the if expression which i have written in Scriptlet is right ?. Is it the correct way of comparing the String in Scriptlet?
 
satheesh subramani
Greenhorn
Posts: 29
Fedora Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scriplet means with in the jsp page java coding
so use the java coding but this is not the prepared method
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are few things.

1. If is not a loop! It is a control statement.
2. The way you wrote the if statement is right. But it is missing the ending curley brace "}".
3. The scriptlets are all gone away long back. Use EL (Expression Language) instead.
 
Dinesh Remje
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghavan,

Actually i have not posted the whole code, so i missed the ending curly brace also.
Any way now my problem is solved for now.
Actually i compared the String values using "String.valueOf(headname[0]).equals("Name") ".
After doing this mine code started working.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats great Dinesh! Congratulations
 
Dinesh Remje
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghvan,
But why cant we compare the string values as i was doing it previously, I mean
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because the way you compare, the results vary!

If you compare the strings with "==" (double equals operator), it checks for the references of the String objects. As both of them may not be really pointing to the same String object, it may return false!

However, the most appropriate way is to use "equals" method which just checks for the content equivalent. That is, the value contained in the String object/literal is what being compared instead of its memory. Hence you get it as true!

Hope this helps!
 
Dinesh Remje
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghavan,
Thanks Bro you are really making my basic concepts very clear. I have read about it previously in Java Interview question, but have forgotten.
Thank you once again.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh Remje wrote:Hi Raghvan,
But why cant we compare the string values as i was doing it previously, I mean

This is very basic Java that you should know well before trying to use JSP and Servlets.

And, there should be no Java code in JSP pages! That's a very poor practice.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh Remje wrote:Hi Raghavan,
Thanks Bro you are really making my basic concepts very clear. I have read about it previously in Java Interview question, but have forgotten.
Thank you once again.


Glad indeed! But you should NOT forget these basics!!
 
Dinesh Remje
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bear Bibeault,
Actually sir i know i should know all this basic things but actually i am doing both the things as of now i mean Java and JSP side by side.
I know its not a good practise but for my project i am doing JSP also.
Sorry sir i should really know all the basic this basic stuff.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic