• Post Reply Bookmark Topic Watch Topic
  • New Topic

If ...else block for hidden field statement problem

 
sarah Marsh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:

Can someone tell me

If ...else block for hidden field statement has any problem? Thanks in advance!

...

<%! int i=0; %>

<html>
</head>
<body ...>
<form action = "abc.jsp">

<% if ( i = 0) %>
<input type=hidden name=newSearch value='true'>
<% else %>
<input type=hidden name=newSearch value=''>

</form>
</body>
</html>
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have used a JSP declaration to create an instance variable "i". What you probably want to do is create a local variable (see below). Since there is only one instance of your JSP (servlet) created by the container, all clients are effectively using the same variable "i".

The if-else block looks ok, but it is considered good programming practice to use braces even if there is only one statement in the if or else block.


Sheldon
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we declare some variable using the declarative JSP tag. it means that variable is not threadsafe, until you make it threadsafe. Behind the scene it would be an instance variable in your servlet.

sheldon suggested the right way to do this kind of stuff.
[ September 20, 2004: Message edited by: adeel ansari ]
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your "<% if ( i = 0) %>".. you r assigning 0 to i rather than comparing i with 0

expression needs to be evaluated to a boolean value..

you need to check like if(i==0)...

Hope it will work...

cheers
Neeraj.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!